Skip to content

Commit 092ab68

Browse files
committed
Add GitHub workflow file to build/tag/push custom docker images
This allows docker images with a custom tag/name to be created on demand for any branch containing this workflow and pushed to the specified docker repository. Two versions of the docker image will always be pushed, one ending with -latest and one ending with the workflow run number, e.g. my-custom-image-name-latest and my-custom-image-name-42. The details required for the workflow to run can either be provided by predefined repository secrets/variables or specified manually when invoking the workflow. This should help make it easier to customize Community Edition instances, test pull requests/development branches, and to provide docker images of long running branches (large alpha/beta features) for community testing.
1 parent 7fb7db5 commit 092ab68

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Based on the workflow by Brandon Patterson from https://github.com/mikemorran/hubs/blob/master/.github/workflows/ce-build.yml
2+
# Input masking referenced from https://dev.to/leading-edje/masking-input-parameters-in-github-actions-1ci
3+
4+
# Common registry base URLs:
5+
# Docker Hub: docker.io
6+
# GitHub: ghcr.io
7+
8+
name: custom-docker-build-push
9+
10+
on:
11+
workflow_dispatch:
12+
inputs:
13+
Override_Registry_Base_URL:
14+
type: string
15+
Override_Registry_Username:
16+
type: string
17+
Override_Registry_Password:
18+
type: string
19+
Override_Registry_Namespace:
20+
type: string
21+
Override_Image_Tag:
22+
type: string
23+
Override_Dockerfile:
24+
type: string
25+
Override_Code_Path:
26+
type: string
27+
Use_Build_Cache:
28+
type: boolean
29+
default: true
30+
31+
# Add in default values for the inputs plus define any missing variables we need.
32+
# Everything should take their values from env rather than inputs.
33+
env:
34+
Registry_Base_URL: ${{ inputs.Override_Registry_Base_URL || vars.REGISTRY_BASE_URL }}
35+
# Registry_Username: This must be added in each job that needs it.
36+
# Registry_Password: This must be added in each job that needs it.
37+
Registry_Namespace: ${{ inputs.Override_Registry_Namespace || vars.REGISTRY_NAMESPACE }}
38+
Image_Tag: ${{ inputs.Override_Image_Tag || github.ref_name }}
39+
Dockerfile: ${{ inputs.Override_Dockerfile || 'RetPageOriginDockerfile' }}
40+
Code_Path: ${{ inputs.Override_Code_Path }}
41+
Use_Build_Cache: ${{ inputs.Use_Build_Cache }}
42+
# repo_name: This must be added in each job that needs it.
43+
44+
jobs:
45+
build:
46+
runs-on: ubuntu-latest
47+
48+
steps:
49+
# Env variables
50+
- name: Assign username from secret
51+
if: ${{ inputs.Override_Registry_Username == ''}}
52+
run: |
53+
echo "Registry_Username=${{ secrets.REGISTRY_USERNAME }}" >> "$GITHUB_ENV"
54+
55+
- name: Assign username from input
56+
if: ${{ inputs.Override_Registry_Username != ''}}
57+
run: |
58+
USERNAME=$(jq -r '.inputs.Override_Registry_Username' $GITHUB_EVENT_PATH)
59+
echo ::add-mask::$USERNAME
60+
echo Registry_Username=$USERNAME >> $GITHUB_ENV
61+
62+
- name: Assign password from secret
63+
if: ${{ inputs.Override_Registry_Password == ''}}
64+
run: |
65+
echo "Registry_Password=${{ secrets.REGISTRY_PASSWORD }}" >> "$GITHUB_ENV"
66+
67+
- name: Assign password from input
68+
if: ${{ inputs.Override_Registry_Password != ''}}
69+
run: |
70+
PASSWORD=$(jq -r '.inputs.Override_Registry_Password' $GITHUB_EVENT_PATH)
71+
echo ::add-mask::$PASSWORD
72+
echo Registry_Password=$PASSWORD >> $GITHUB_ENV
73+
74+
- name: Add the repository name as an env variable # Lowercase is forced to prevent errors.
75+
run: |
76+
echo "repo_name=${GITHUB_REPOSITORY#*/}" | tr "[:upper:]" "[:lower:]" >> "$GITHUB_ENV"
77+
78+
# Code
79+
- name: Checkout repository
80+
uses: actions/checkout@v4
81+
with:
82+
path: "./repo"
83+
84+
- name: Use Code_Path for multirepo
85+
if: ${{ env.Code_Path != ''}}
86+
run: |
87+
mkdir ./_repo
88+
cp -rf ./repo/${{ env.Code_Path }}/* ./_repo
89+
rm -rf ./repo
90+
mv ./_repo ./repo
91+
ls ./repo
92+
93+
# Docker
94+
- name: Set up Docker Buildx
95+
uses: docker/setup-buildx-action@v3
96+
with:
97+
install: true
98+
99+
- name: Login to container registry
100+
uses: docker/login-action@v3
101+
with:
102+
registry: ${{ env.Registry_Base_URL }}
103+
username: ${{ env.Registry_Username }}
104+
password: ${{ env.Registry_Password }}
105+
106+
- name: Docker Build and Push (with cache)
107+
if: ${{ fromJSON(env.Use_Build_Cache) == true }}
108+
uses: docker/build-push-action@v6
109+
with:
110+
context: repo/
111+
file: repo/${{ env.Dockerfile }}
112+
tags: ${{ env.Registry_Base_URL }}/${{ env.Registry_Namespace }}/${{ env.repo_name }}:${{ env.Image_Tag }}-latest,${{ env.Registry_Base_URL }}/${{ env.Registry_Namespace }}/${{ env.repo_name }}:${{ env.Image_Tag }}-${{ github.run_number }}
113+
cache-from: type=registry,ref=${{ env.Registry_Base_URL }}/${{ env.Registry_Namespace }}/${{ env.repo_name }}:buildcache
114+
cache-to: type=registry,ref=${{ env.Registry_Base_URL }}/${{ env.Registry_Namespace }}/${{ env.repo_name }}:buildcache,mode=max,image-manifest=true,oci-mediatypes=true
115+
push: true
116+
117+
- name: Docker Build and Push (no cache)
118+
if: ${{ fromJSON(env.Use_Build_Cache) == false }}
119+
uses: docker/build-push-action@v6
120+
with:
121+
context: repo/
122+
file: repo/${{ env.Dockerfile }}
123+
tags: ${{ env.Registry_Base_URL }}/${{ env.Registry_Namespace }}/${{ env.repo_name }}:${{ env.Image_Tag }}-latest,${{ env.Registry_Base_URL }}/${{ env.Registry_Namespace }}/${{ env.repo_name }}:${{ env.Image_Tag }}-${{ github.run_number }}
124+
push: true

0 commit comments

Comments
 (0)