1
1
name : Build
2
- on : [ push, pull_request, workflow_dispatch ]
2
+ on : [ push, pull_request, release, workflow_dispatch ]
3
3
env :
4
4
REGISTRY : ghcr.io
5
- BUILD_TAG : sha-${{ github.sha }}
6
5
jobs :
7
- build :
6
+ setup :
8
7
runs-on : ubuntu-latest
9
8
10
- permissions :
11
- packages : write
12
-
13
9
steps :
14
10
# See https://github.com/docker/build-push-action/blob/v2.10.0/TROUBLESHOOTING.md#repository-name-must-be-lowercase
15
- - name : Sanitize repo slug
11
+ - name : Sanitize image name
16
12
uses : actions/github-script@v6
17
- id : slug
13
+ id : image-name
18
14
with :
19
15
result-encoding : string
20
- script : return '${{ github.repository }}'.toLowerCase()
16
+ script : return '${{ env.REGISTRY }}/${{ github.repository }}'.toLowerCase()
17
+
18
+ - name : Get short SHA
19
+ run : |
20
+ echo SHORT_SHA="${GITHUB_SHA:0:7}" >> $GITHUB_ENV
21
+
22
+ outputs :
23
+ base_image_name : ${{ steps.image-name.outputs.result }}
24
+ build_image : ${{ steps.image-name.outputs.result }}:${{ env.SHORT_SHA }}
25
+
26
+ build :
27
+ if : github.event_name != 'release'
28
+ needs : setup
29
+ env :
30
+ BUILD_IMAGE : ${{ needs.setup.outputs.build_image }}
31
+
32
+ runs-on : ubuntu-latest
33
+
34
+ permissions :
35
+ packages : write
21
36
37
+ steps :
22
38
- name : Checkout repository
23
39
uses : actions/checkout@v2
24
40
25
41
- name : Log in to the Container registry
26
- uses : docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
42
+ uses : docker/login-action@v2
27
43
with :
28
44
registry : ${{ env.REGISTRY }}
29
45
username : ${{ github.actor }}
30
46
password : ${{ secrets.GITHUB_TOKEN }}
31
47
32
48
- name : Build and push Docker image
33
- uses : docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
49
+ uses : docker/build-push-action@v3
34
50
with :
35
51
context : .
36
52
push : true
37
- tags : ${{ env.REGISTRY }}/${{ steps.slug.outputs.result }}:${{ env.BUILD_TAG }}
53
+ tags : ${{ env.BUILD_IMAGE }}
38
54
39
55
outputs :
40
- # TODO: Figure out how to DRY this
41
- image_path : ${{ env.REGISTRY }}/${{ steps.slug.outputs.result }}
42
- tagged_image : ${{ env.REGISTRY }}/${{ steps.slug.outputs.result }}:${{ env.BUILD_TAG }}
56
+ build_image : ${{ env.BUILD_IMAGE }}
43
57
44
58
test :
59
+ if : github.event_name != 'release'
45
60
needs : build
61
+
46
62
runs-on : ubuntu-latest
47
63
48
64
container :
49
- image : ${{ needs.build.outputs.tagged_image }}
65
+ image : ${{ needs.build.outputs.build_image }}
50
66
51
67
defaults :
52
68
run :
@@ -75,26 +91,68 @@ jobs:
75
91
name : artifacts
76
92
path : /opt/app/artifacts/**
77
93
94
+ # TODO: DRY push and push-release
78
95
push :
79
- needs : [ build, test ]
96
+ if : github.event_name != 'release'
97
+
98
+ needs : [ setup, build, test ]
99
+ env :
100
+ BASE_IMAGE_NAME : ${{ needs.setup.outputs.base_image_name }}
101
+ BUILD_IMAGE : ${{ needs.build.outputs.build_image }}
102
+
80
103
runs-on : ubuntu-latest
81
104
82
105
permissions :
83
106
packages : write
84
107
108
+ steps :
109
+ - name : Extract metadata (tags, labels) for Docker
110
+ id : meta
111
+ uses : docker/metadata-action@v3
112
+ with :
113
+ images : ${{ env.BASE_IMAGE_NAME }}
114
+
115
+ - name : Log in to the Container registry
116
+ uses : docker/login-action@v2
117
+ with :
118
+ registry : ${{ env.REGISTRY }}
119
+ username : ${{ github.actor }}
120
+ password : ${{ secrets.GITHUB_TOKEN }}
121
+
122
+ - name : Tag and push image
123
+
124
+ with :
125
+ src : ${{ env.BUILD_IMAGE }}
126
+ dst : |
127
+ ${{ steps.meta.outputs.tags }}
128
+
129
+ # TODO: DRY push and push-release
130
+ push-release :
131
+ if : github.event_name == 'release' && github.event.action == 'published'
132
+
133
+ needs : [setup]
85
134
env :
86
- IMAGE_PATH : ${{ needs.build.outputs.image_path }}
87
- TAGGED_IMAGE : ${{ needs.build.outputs.tagged_image }}
135
+ BASE_IMAGE_NAME : ${{ needs.setup.outputs.base_image_name }}
136
+ BUILD_IMAGE : ${{ needs.setup.outputs.build_image }}
137
+
138
+ runs-on : ubuntu-latest
139
+
140
+ permissions :
141
+ packages : write
88
142
89
143
steps :
90
144
- name : Extract metadata (tags, labels) for Docker
91
145
id : meta
92
- uses : docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
146
+ uses : docker/metadata-action@v3
93
147
with :
94
- images : ${{ env.IMAGE_PATH }}
148
+ images : ${{ env.BASE_IMAGE_NAME }}
149
+ tags :
150
+ type=semver,pattern={{major}}
151
+ type=semver,pattern={{major}}.{{minor}}
152
+ type=semver,pattern={{version}}
95
153
96
154
- name : Log in to the Container registry
97
- uses : docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
155
+ uses : docker/login-action@v2
98
156
with :
99
157
registry : ${{ env.REGISTRY }}
100
158
username : ${{ github.actor }}
@@ -103,6 +161,6 @@ jobs:
103
161
- name : Tag and push image
104
162
105
163
with :
106
- src : ${{ env.TAGGED_IMAGE }}
164
+ src : ${{ env.BUILD_IMAGE }}
107
165
dst : |
108
166
${{ steps.meta.outputs.tags }}
0 commit comments