Skip to content

Commit d7ba381

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. 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 b0cff4d commit d7ba381

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Based on the workflow by BrandonP 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+
- name: Assign username from secret
50+
if: ${{ inputs.Override_Registry_Username == ''}}
51+
run: |
52+
echo "Registry_Username=${{ secrets.REGISTRY_USERNAME }}" >> "$GITHUB_ENV"
53+
54+
- name: Assign username from input
55+
if: ${{ inputs.Override_Registry_Username != ''}}
56+
run: |
57+
USERNAME=$(jq -r '.inputs.Override_Registry_Username' $GITHUB_EVENT_PATH)
58+
echo ::add-mask::$USERNAME
59+
echo Registry_Username=$USERNAME >> $GITHUB_ENV
60+
61+
- name: Assign password from secret
62+
if: ${{ inputs.Override_Registry_Password == ''}}
63+
run: |
64+
echo "Registry_Password=${{ secrets.REGISTRY_PASSWORD }}" >> "$GITHUB_ENV"
65+
66+
- name: Assign password from input
67+
if: ${{ inputs.Override_Registry_Password != ''}}
68+
run: |
69+
PASSWORD=$(jq -r '.inputs.Override_Registry_Password' $GITHUB_EVENT_PATH)
70+
echo ::add-mask::$PASSWORD
71+
echo Registry_Password=$PASSWORD >> $GITHUB_ENV
72+
73+
- name: Add the repository name as an env variable
74+
run: |
75+
echo "repo_name=${GITHUB_REPOSITORY#*/}" >> "$GITHUB_ENV"
76+
77+
- name: Checkout code
78+
uses: actions/checkout@v2
79+
80+
- name: Set up Docker Buildx
81+
uses: docker/setup-buildx-action@v3
82+
with:
83+
install: true
84+
85+
- name: Login to container registry
86+
uses: docker/login-action@v3
87+
with:
88+
registry: ${{ env.Registry_Base_URL }}
89+
username: ${{ env.Registry_Username }}
90+
password: ${{ env.Registry_Password }}
91+
92+
- name: Checkout repository
93+
uses: actions/checkout@v2
94+
with:
95+
path: "./repo"
96+
97+
- name: Use Code_Path for multirepo
98+
if: ${{ env.Code_Path != ''}}
99+
run: |
100+
mkdir ./_repo
101+
cp -rf ./repo/${{ env.Code_Path }}/* ./_repo
102+
rm -rf ./repo
103+
mv ./_repo ./repo
104+
ls ./repo
105+
106+
- name: Docker Buildx setup
107+
uses: docker/setup-buildx-action@v3
108+
with:
109+
install: true
110+
111+
- name: Docker Build and Push (with cache)
112+
if: ${{ fromJSON(env.Use_Build_Cache) == true }}
113+
uses: docker/build-push-action@v6
114+
with:
115+
context: repo/
116+
file: repo/${{ env.Dockerfile }}
117+
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 }}
118+
cache-from: type=registry,ref=${{ env.Registry_Base_URL }}/${{ env.Registry_Namespace }}/${{ env.repo_name }}:buildcache
119+
cache-to: type=registry,ref=${{ env.Registry_Base_URL }}/${{ env.Registry_Namespace }}/${{ env.repo_name }}:buildcache,mode=max,image-manifest=true,oci-mediatypes=true
120+
push: true
121+
122+
- name: Docker Build and Push (no cache)
123+
if: ${{ fromJSON(env.Use_Build_Cache) == false }}
124+
uses: docker/build-push-action@v6
125+
with:
126+
context: repo/
127+
file: repo/${{ env.Dockerfile }}
128+
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 }}
129+
push: true

0 commit comments

Comments
 (0)