Skip to content

Commit f9b50bc

Browse files
DEV-457: Adds build/release actions (#1)
1 parent 87b6a7a commit f9b50bc

File tree

3 files changed

+139
-1
lines changed

3 files changed

+139
-1
lines changed

.github/workflows/build.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: Build
2+
on: [ push, pull_request, workflow_dispatch ]
3+
env:
4+
REGISTRY: ghcr.io
5+
6+
jobs:
7+
# TODO: DRY w/release.yml
8+
setup:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
# See https://github.com/docker/build-push-action/blob/v2.10.0/TROUBLESHOOTING.md#repository-name-must-be-lowercase
13+
- name: Sanitize image name
14+
uses: actions/github-script@v6
15+
id: image-name
16+
with:
17+
result-encoding: string
18+
script: return '${{ env.REGISTRY }}/${{ github.repository }}'.toLowerCase()
19+
20+
- name: Get short SHA
21+
run: |
22+
echo SHORT_SHA="${GITHUB_SHA:0:7}" >> $GITHUB_ENV
23+
24+
- name: Get build start time
25+
run: |
26+
echo BUILD_TIMESTAMP="$(date --utc --iso-8601=seconds)" >> $GITHUB_ENV
27+
28+
outputs:
29+
base_image_name: ${{ steps.image-name.outputs.result }}
30+
build_image: ${{ steps.image-name.outputs.result }}:${{ env.SHORT_SHA }}
31+
32+
test:
33+
if: github.event_name != 'release'
34+
needs: [ setup ]
35+
runs-on: ubuntu-latest
36+
permissions:
37+
packages: write
38+
env:
39+
BUILD_IMAGE: ${{ needs.setup.outputs.build_image }}
40+
steps:
41+
- name: Checkout repository
42+
uses: actions/checkout@v3
43+
44+
- name: Run tests
45+
run: ./test.sh
46+
47+
- name: Log in to the Container registry
48+
uses: docker/login-action@v2
49+
with:
50+
registry: ${{ env.REGISTRY }}
51+
username: ${{ github.actor }}
52+
password: ${{ secrets.GITHUB_TOKEN }}
53+
54+
- name: Build and push Docker image
55+
uses: docker/build-push-action@v3
56+
with:
57+
context: .
58+
push: true
59+
tags: ${{ env.BUILD_IMAGE }}
60+
build-args: |
61+
BUILD_TIMESTAMP=${{ env.BUILD_TIMESTAMP }}
62+
BUILD_URL=https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
63+
DOCKER_TAG=${{ env.BUILD_IMAGE }}
64+
GIT_BRANCH=${{ github.ref_name }}
65+
GIT_COMMIT=${{ github.sha }}
66+
GIT_URL=${{ github.repositoryUrl }}
67+
68+
outputs:
69+
build_image: ${{ env.BUILD_IMAGE }}

.github/workflows/release.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: Release
2+
3+
on:
4+
release:
5+
types:
6+
- published
7+
workflow_dispatch:
8+
workflow_run:
9+
workflows: ["Build"]
10+
types: ["completed"]
11+
branches:
12+
- main
13+
14+
env:
15+
REGISTRY: ghcr.io
16+
17+
jobs:
18+
setup:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Sanitize image name
22+
uses: actions/github-script@v6
23+
id: image-name
24+
with:
25+
result-encoding: string
26+
script: return '${{ env.REGISTRY }}/${{ github.repository }}'.toLowerCase()
27+
28+
- name: Get short SHA
29+
run: |
30+
echo SHORT_SHA="${GITHUB_SHA:0:7}" >> $GITHUB_ENV
31+
32+
outputs:
33+
base_image_name: ${{ steps.image-name.outputs.result }}
34+
build_image: ${{ steps.image-name.outputs.result }}:${{ env.SHORT_SHA }}
35+
36+
push:
37+
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
38+
runs-on: ubuntu-latest
39+
needs: setup
40+
permissions:
41+
packages: write
42+
env:
43+
BASE_IMAGE_NAME: ${{ needs.setup.outputs.base_image_name }}
44+
BUILD_IMAGE: ${{ needs.setup.outputs.build_image }}
45+
steps:
46+
- name: Extract metadata (tags, labels) for Docker
47+
id: meta
48+
uses: docker/metadata-action@v4
49+
with:
50+
images: ${{ env.BASE_IMAGE_NAME }}
51+
tags: |
52+
type=raw,value=latest,enable={{is_default_branch}}
53+
type=semver,pattern={{major}}
54+
type=semver,pattern={{major}}.{{minor}}
55+
type=semver,pattern={{version}}
56+
57+
- name: Log in to the Container registry
58+
uses: docker/login-action@v2
59+
with:
60+
registry: ${{ env.REGISTRY }}
61+
username: ${{ github.actor }}
62+
password: ${{ secrets.GITHUB_TOKEN }}
63+
64+
- name: Tag and push image
65+
uses: akhilerm/tag-push-action@v2.1.0
66+
with:
67+
src: ${{ env.BUILD_IMAGE }}
68+
dst: |
69+
${{ steps.meta.outputs.tags }}

test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh -e
1+
#!/bin/bash -e
22

33
function cleanup {
44
docker compose down -v --remove-orphans > /dev/null 2>&1

0 commit comments

Comments
 (0)