From ee1a41bd479b61eb479d289ccbc1efd1bd1a24f7 Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Tue, 2 Jan 2024 16:09:40 +0000 Subject: [PATCH] Add Docker build GitHub Actions --- .github/actions/docker-build/action.yml | 44 ++++++++++++++++++++++ .github/actions/get-version-tag/action.yml | 21 +++++++++++ .github/workflows/cd.yml | 29 ++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 .github/actions/docker-build/action.yml create mode 100644 .github/actions/get-version-tag/action.yml create mode 100644 .github/workflows/cd.yml diff --git a/.github/actions/docker-build/action.yml b/.github/actions/docker-build/action.yml new file mode 100644 index 0000000..b42b17d --- /dev/null +++ b/.github/actions/docker-build/action.yml @@ -0,0 +1,44 @@ +name: Build Docker image + +inputs: + version: + required: true + type: string + latest: + required: false + type: boolean + default: false + github-token: + required: true + type: string + docker-image-name: + required: true + type: string + +runs: + using: "composite" + steps: + - uses: actions/checkout@v3 + - name: Log in to registry + run: echo "${{ inputs.github-token }}" | docker login ghcr.io -u $ --password-stdin + shell: bash + - uses: hadolint/hadolint-action@v3.1.0 + with: + dockerfile: Dockerfile + ignore: DL3045,DL3007 + - name: Build and push image + run: | + IMAGE_ID=$(echo "ghcr.io/${{ github.repository_owner }}/${{ inputs.docker-image-name }}" | tr '[A-Z]' '[a-z]') + echo "IMAGE_ID=$IMAGE_ID" >> "$GITHUB_OUTPUT" + echo $IMAGE_ID + docker build --tag $IMAGE_ID:${{ inputs.version }} --platform=linux/amd64 --no-cache . + docker push $IMAGE_ID:${{ inputs.version }} + shell: bash + - name: Tag latest + if: inputs.latest + run: | + IMAGE_ID=$(echo "ghcr.io/${{ github.repository_owner }}/${{ inputs.docker-image-name }}" | tr '[A-Z]' '[a-z]') + echo $IMAGE_ID + docker tag $IMAGE_ID:${{ inputs.version }} $IMAGE_ID:latest + docker push $IMAGE_ID:latest + shell: bash \ No newline at end of file diff --git a/.github/actions/get-version-tag/action.yml b/.github/actions/get-version-tag/action.yml new file mode 100644 index 0000000..acddf7b --- /dev/null +++ b/.github/actions/get-version-tag/action.yml @@ -0,0 +1,21 @@ +name: Get version tag + +outputs: + version-tag: + description: "Version tag" + value: ${{ steps.version-tag.outputs.VERSION }} + +runs: + using: "composite" + steps: + - id: version-tag + name: Get tag from branch, tag or release + run: | + VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') + # Strip "v" prefix from tag name + [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') + [ "$VERSION" == "main" ] && VERSION=develop + [ "$VERSION" == "develop" ] && VERSION=develop + echo "Version: $VERSION" + echo "VERSION=$VERSION" >> "$GITHUB_OUTPUT" + shell: bash \ No newline at end of file diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..27c4e59 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,29 @@ +name: CD + +on: + workflow_dispatch: + push: + branches: + - proof-of-concept + +concurrency: + group: cd + +jobs: + build: + runs-on: ubuntu-latest + permissions: + packages: write + contents: read + steps: + - uses: actions/checkout@v3 + - name: Get tag + id: version-tag + uses: ./.github/actions/get-version-tag + - name: Build Docker image + uses: ./.github/actions/docker-build + with: + version: ${{ steps.version-tag.outputs.version-tag }} + latest: true + github-token: ${{ secrets.GITHUB_TOKEN }} + docker-image-name: ${{ vars.DOCKER_IMAGE_NAME }}