Build and Push Docker Image Staging #26
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Push Docker Image Staging | |
"on": | |
workflow_dispatch: | |
push: | |
branches: | |
- release/* | |
- hotfix/* | |
jobs: | |
build_and_deploy_stage: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
packages: write | |
actions: write | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Extract branch name | |
shell: bash | |
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT | |
id: extract_branch | |
- name: Extract version from branch name (for release branches) | |
if: startsWith(steps.extract_branch.outputs.branch, 'release/') | |
run: | | |
BRANCH_NAME="${{ steps.extract_branch.outputs.branch }}" | |
VERSION=${BRANCH_NAME#release/} | |
echo "RELEASE_VERSION=$VERSION" >> $GITHUB_ENV | |
- name: Extract version from branch name (for hotfix branches) | |
if: startsWith(steps.extract_branch.outputs.branch, 'hotfix/') | |
run: | | |
BRANCH_NAME="${{ steps.extract_branch.outputs.branch }}" | |
VERSION=${BRANCH_NAME#hotfix/} | |
echo "RELEASE_VERSION=$VERSION" >> $GITHUB_ENV | |
- name: Extract version from input (for manual workflow dispatch) | |
if: github.event_name == 'workflow_dispatch' | |
run: | | |
echo "RELEASE_VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Set lower case owner name | |
run: | | |
echo "REPO_LC=${REPO,,}" >>${GITHUB_ENV} | |
env: | |
REPO: "${{ github.repository }}" | |
- name: Set commit sha | |
run: | | |
echo "COMMIT_SHA=$(git rev-parse --short $GITHUB_SHA)" >> $GITHUB_ENV | |
- name: Build and push Version | |
if: github.event_name == 'push' | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
file: ./.docker/Dockerfile.stage | |
platforms: linux/amd64,linux/arm64 | |
push: true | |
tags: ghcr.io/${{ env.REPO_LC }}-stage:${{ env.RELEASE_VERSION }}-preview, ghcr.io/${{ env.REPO_LC }}-stage:latest, ghcr.io/${{ env.REPO_LC }}-stage:${{ env.COMMIT_SHA }} | |
- name: Build and push Version | |
if: github.event_name == 'workflow_dispatch' | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
file: ./.docker/Dockerfile.stage | |
platforms: linux/amd64,linux/arm64 | |
push: true | |
tags: ghcr.io/${{ env.REPO_LC }}-stage:latest, ghcr.io/${{ env.REPO_LC }}-stage:${{ env.COMMIT_SHA }} | |
- name: "Setup yq" | |
uses: dcarbone/[email protected] | |
with: | |
version: "v4.42.1" | |
force: true | |
- name: Initialize mandatory git config | |
run: | | |
git config --global user.name 'GitHub Actions' | |
git config --global user.email '[email protected]' | |
- name: Bump version in values/stage.yaml | |
run: yq -i '.deployment.image.tag=strenv(COMMIT_SHA)' ./k8s/values/stage.yaml | |
- name: Commit k8s values files | |
id: make-commit | |
run: | | |
git add ./k8s/values/stage.yaml | |
git commit --message "Update stage image to commit ${{ env.COMMIT_SHA }}" | |
echo "::set-output name=commit::$(git rev-parse HEAD)" | |
- name: Push changes | |
uses: CasperWA/push-protected@v2 | |
if: github.event_name == 'push' | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
branch: release/${{ github.event.inputs.version }} | |
- name: Push changes | |
uses: CasperWA/push-protected@v2 | |
if: github.event_name == 'workflow_dispatch' | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
branch: develop | |
- name: Delete tag | |
uses: dev-drprasad/[email protected] | |
with: | |
tag_name: stage-deployment | |
delete_release: false | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Create tag | |
run: git tag -fa 'stage-deployment' -m "Update tag to commit ${{ env.COMMIT_SHA }}" | |
- name: Push tag | |
run: git push origin stage-deployment |