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