From d916361596fc9502bdf98aee3d450a4fa8aa7a67 Mon Sep 17 00:00:00 2001 From: kentsanggds Date: Thu, 16 Nov 2023 11:30:54 +0000 Subject: [PATCH] Add build-image.sh and GHA build-image.yaml --- .github/workflows/build-image.yaml | 45 ++++++++++++++++++++++++++++++ docker/build-image.sh | 21 ++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 .github/workflows/build-image.yaml create mode 100755 docker/build-image.sh diff --git a/.github/workflows/build-image.yaml b/.github/workflows/build-image.yaml new file mode 100644 index 00000000..e2766939 --- /dev/null +++ b/.github/workflows/build-image.yaml @@ -0,0 +1,45 @@ +name: Build and push images + +on: + workflow_dispatch: + inputs: + buildType: + description: Decide on build type + required: true + type: choice + options: + - build_push + - build_only + push: + branches: + - main + +jobs: + build_and_push: + name: datagovuk_publish + runs-on: ubuntu-latest + permissions: + packages: write + steps: + - name: Login to GHCR + uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a # v2.1.0 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - uses: actions/checkout@v4 + with: + ref: ${{ inputs.gitRef }} + - name: Build images (without pushing to registry) + if: ${{ inputs.buildType == 'build_only' }} + env: + DRY_RUN: "1" + APP: datagovuk_find + ARCH: amd64 + run: ./docker/build-image.sh + - name: Build and push images + if: ${{ inputs.buildType == 'build_push' || github.ref == 'refs/heads/main' }} + env: + APP: datagovuk_find + ARCH: amd64 + run: ./docker/build-image.sh diff --git a/docker/build-image.sh b/docker/build-image.sh new file mode 100755 index 00000000..131443df --- /dev/null +++ b/docker/build-image.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +set -eux + +build () { + if [ "${ARCH}" = "amd64" ]; then + docker build . -t "ghcr.io/alphagov/${APP}:${1}" -f "docker/Dockerfile" + else + docker buildx build --platform "linux/${ARCH}" . -t "ghcr.io/alphagov/${APP}:${1}" -f "docker/Dockerfile" + fi +} + +DOCKER_TAG="${GITHUB_SHA}" + +build "${DOCKER_TAG}" + +if [[ -n ${DRY_RUN:-} ]]; then + echo "Dry run; not pushing to registry" +else + docker push "ghcr.io/alphagov/${APP}:${DOCKER_TAG}" +fi