generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 2
174 lines (158 loc) · 6.29 KB
/
_build.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#test
name: Build Image
on:
workflow_call:
inputs:
component:
type: string
required: true
description: Component, e.g. backend, frontend
img_build:
type: string
required: true
description: Default zone, usually pr#
img_fallback:
type: string
required: true
description: Where to pull default images from, e.g. prod, test
force_build:
type: string
required: false
default: "true"
repository:
type: string
required: false
default: ${{ github.repository }}
description: Optional, specify a repo to clone and build from
triggers:
type: string
required: false
default: "('.')"
description: Optional, specify paths to conditionally trigger a build, e.g. ('./backend/', './frontend/)
secrets:
gh_token:
required: true
description: Default github token (secrets.GITHUB_TOKEN)
outputs:
build:
description: True = build has changed
value: ${{ jobs.build.outputs.build }}
env:
default_branch: ${{ github.event.repository.default_branch }}
jobs:
build:
name: ${{ inputs.component }}
runs-on: ubuntu-22.04
outputs:
build: ${{ steps.check.outputs.build }}
steps:
- uses: actions/checkout@v3
with:
# Check out source repo
repository: ${{ github.repository }}
# Check triggers to see if a build is required
# Returns jobs.check.outputs.build as a boolean (true/false)
- name: Check and process modified files
id: check
run: |
FORCE_BUILD=${{ inputs.force_build }}
if [[ $FORCE_BUILD == "true" ]]
then
# Output build=true for next steps
echo "build=true" >> $GITHUB_OUTPUT
exit 0
fi
# Fetch default branch to diff against
git fetch origin ${{ env.default_branch }}:refs/remotes/origin/main
# Trigger build if diff matches any triggers
TRIGGERS=${{ inputs.triggers }}
while read -r check
do
for t in "${TRIGGERS[@]}"; do
if [[ "${check}" =~ "${t}" ]]
then
# Output build=true for next steps
echo "build=true" >> $GITHUB_OUTPUT
echo -e "${t}\n --> ${check}\n"
exit 0
fi
done
done < <(git diff origin/${{ env.default_branch }} --name-only)
# Check that fallback tag exists, otherwise build
TOKEN=$(curl -s https://ghcr.io/token\?scope\="repository:${{ github.repository }}/${{ inputs.component }}:pull" | jq -r .token)
URL="https://ghcr.io/v2/${{ github.repository }}/${{ inputs.component }}/manifests/${{ inputs.img_fallback }}"
if [ $(curl -ILso /dev/null -w "%{http_code}" -H "Authorization: Bearer ${TOKEN}" "${URL}") -ne 200 ]
then
# Output build=true for next steps
echo "build=true" >> $GITHUB_OUTPUT
echo -e "\nBuilding image, since no fallback is available!"
exit 0
fi
# If at this point, no build is required
echo "build=false" >> $GITHUB_OUTPUT
echo "Container build not required"
# If a build is not required, reuse a previous image
- name: Recycle/retag Previous Images
uses: shrink/actions-docker-registry-tag@v3
if: steps.check.outputs.build != 'true'
with:
registry: ghcr.io
repository: ${{ github.repository }}/${{ inputs.component }}
target: ${{ inputs.img_fallback }}
tags: ${{ inputs.img_build }}
# If a build is required, then create a new image
- uses: actions/checkout@v3
with:
# Check out build repo
repository: ${{ inputs.repository }}
- name: Set up Docker Buildx
if: steps.check.outputs.build == 'true'
uses: docker/setup-buildx-action@v2
- name: Log in to the Container registry
if: steps.check.outputs.build == 'true'
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.gh_token }}
- name: Build and push ${{ inputs.component }} Docker image
if: steps.check.outputs.build == 'true'
uses: docker/[email protected]
with:
context: ./${{ inputs.component }}/
push: true
tags: ghcr.io/${{ github.repository }}/${{ inputs.component }}:${{ inputs.img_build }}
cache-from: type=gha
cache-to: type=gha,mode=max
# if: inputs.component == 'backend/users-migration'
# file: ./backend/users/Dockerfile-migrations-openshift
- name: Build and push Migration image for users
if: steps.check.outputs.build == 'true' && inputs.component == 'backend/users'
uses: docker/[email protected]
with:
context: ./${{ inputs.component }}/
push: true
tags: ghcr.io/${{ github.repository }}/backend/users-migration:${{ inputs.img_build }}
cache-from: type=gha
cache-to: type=gha,mode=max
file: ./backend/users/Dockerfile-migrations-openshift
- name: Build and push Migration image for applications
if: steps.check.outputs.build == 'true' && inputs.component == 'backend/applications'
uses: docker/[email protected]
with:
context: ./${{ inputs.component }}/
push: true
tags: ghcr.io/${{ github.repository }}/backend/applications-migration:${{ inputs.img_build }}
cache-from: type=gha
cache-to: type=gha,mode=max
file: ./backend/applications/Dockerfile-migrations-openshift
- name: Build and push Migration image for Sites
if: steps.check.outputs.build == 'true' && inputs.component == 'backend/sites'
uses: docker/[email protected]
with:
context: ./${{ inputs.component }}/
push: true
tags: ghcr.io/${{ github.repository }}/backend/sites-migration:${{ inputs.img_build }}
cache-from: type=gha
cache-to: type=gha,mode=max
file: ./backend/sites/Dockerfile-migrations-openshift