11name : Build
2- on : [ push, pull_request, workflow_dispatch ]
2+ on : [ push, pull_request, release, workflow_dispatch ]
33env :
44 REGISTRY : ghcr.io
5- BUILD_TAG : sha-${{ github.sha }}
65jobs :
7- build :
6+ setup :
87 runs-on : ubuntu-latest
98
10- permissions :
11- packages : write
12-
139 steps :
1410 # 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
1612 uses : actions/github-script@v6
17- id : slug
13+ id : image-name
1814 with :
1915 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
2136
37+ steps :
2238 - name : Checkout repository
2339 uses : actions/checkout@v2
2440
2541 - name : Log in to the Container registry
26- uses : docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
42+ uses : docker/login-action@v2
2743 with :
2844 registry : ${{ env.REGISTRY }}
2945 username : ${{ github.actor }}
3046 password : ${{ secrets.GITHUB_TOKEN }}
3147
3248 - name : Build and push Docker image
33- uses : docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
49+ uses : docker/build-push-action@v3
3450 with :
3551 context : .
3652 push : true
37- tags : ${{ env.REGISTRY }}/${{ steps.slug.outputs.result }}:${{ env.BUILD_TAG }}
53+ tags : ${{ env.BUILD_IMAGE }}
3854
3955 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 }}
4357
4458 test :
59+ if : github.event_name != 'release'
4560 needs : build
61+
4662 runs-on : ubuntu-latest
4763
4864 container :
49- image : ${{ needs.build.outputs.tagged_image }}
65+ image : ${{ needs.build.outputs.build_image }}
5066
5167 defaults :
5268 run :
@@ -75,26 +91,68 @@ jobs:
7591 name : artifacts
7692 path : /opt/app/artifacts/**
7793
94+ # TODO: DRY push and push-release
7895 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+
80103 runs-on : ubuntu-latest
81104
82105 permissions :
83106 packages : write
84107
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]
85134 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
88142
89143 steps :
90144 - name : Extract metadata (tags, labels) for Docker
91145 id : meta
92- uses : docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
146+ uses : docker/metadata-action@v3
93147 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}}
95153
96154 - name : Log in to the Container registry
97- uses : docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
155+ uses : docker/login-action@v2
98156 with :
99157 registry : ${{ env.REGISTRY }}
100158 username : ${{ github.actor }}
@@ -103,6 +161,6 @@ jobs:
103161 - name : Tag and push image
104162105163 with :
106- src : ${{ env.TAGGED_IMAGE }}
164+ src : ${{ env.BUILD_IMAGE }}
107165 dst : |
108166 ${{ steps.meta.outputs.tags }}
0 commit comments