From 09c24b19bebb08f052ae939eafc1afb9184fc41a Mon Sep 17 00:00:00 2001 From: Paul Bouwer Date: Mon, 5 Apr 2021 14:58:00 +1000 Subject: [PATCH] Create build-and-publish-container-image.yaml --- .../build-and-publish-container-image.yaml | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 .github/workflows/build-and-publish-container-image.yaml diff --git a/.github/workflows/build-and-publish-container-image.yaml b/.github/workflows/build-and-publish-container-image.yaml new file mode 100644 index 00000000..67fb06a9 --- /dev/null +++ b/.github/workflows/build-and-publish-container-image.yaml @@ -0,0 +1,55 @@ +name: "When a release tag is created, build and publish a new version of the container image" + +on: + + create + +jobs: + + build-and-push-container-image: + + runs-on: ubuntu-latest + + if: startsWith(github.ref, 'refs/tags/v') + + env: + CONTAINER_REGISTRY: docker.io + CONTAINER_IMAGE: paulbouwer/hello-kubernetes + + steps: + + - name: Checkout code + uses: actions/checkout@v2 + + - name: Build image and version variables + run: | + echo "IMAGE=$CONTAINER_REGISTRY/$CONTAINER_IMAGE" >> $GITHUB_ENV + echo "IMAGE_VERSION=$(cat src/app/package.json | jq -r .version)" >> $GITHUB_ENV + + - name: Build additional image tag variables + run: | + echo "IMAGE_MAJOR_VERSION=$(echo $IMAGE_VERSION | cut -d '.' -f1)" >> $GITHUB_ENV + echo "IMAGE_MINOR_VERSION=$(echo $IMAGE_VERSION | cut -d '.' -f2)" >> $GITHUB_ENV + + - name: Build image + run: | + docker build --tag "$IMAGE:$IMAGE_VERSION" \ + --build-arg IMAGE_VERSION="$IMAGE_VERSION" \ + --build-arg IMAGE_CREATE_DATE="`date -u +"%Y-%m-%dT%H:%M:%SZ"`" \ + --build-arg IMAGE_SOURCE_REVISION="`git rev-parse HEAD`" \ + --file src/app/Dockerfile src/app + + - name: Create additional image tags + run: | + docker tag $IMAGE:$IMAGE_VERSION $IMAGE:$IMAGE_MAJOR_VERSION + docker tag $IMAGE:$IMAGE_VERSION $IMAGE:$IMAGE_MAJOR_VERSION.$IMAGE_MINOR_VERSION + + - name: Log into registry + run: echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin + + - name: Push image and tags to registry + run: | + docker push $IMAGE:$IMAGE_VERSION + docker push $IMAGE:$IMAGE_MAJOR_VERSION + docker push $IMAGE:$IMAGE_MAJOR_VERSION.$IMAGE_MINOR_VERSION +