Skip to content

ci: use github app to commit changes in workflow #46

ci: use github app to commit changes in workflow

ci: use github app to commit changes in workflow #46

name: Build and Push Docker Image Staging
'on':
workflow_dispatch:
push:
branches:
- release/*
- hotfix/*
- develop
jobs:
build_and_deploy_stage:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
actions: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: Extract branch name
id: extract_branch
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
- 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 branch name (for develop branche)
if: startsWith(steps.extract_branch.outputs.branch, 'develop')
run: |
BRANCH_NAME="${{ steps.extract_branch.outputs.branch }}"
VERSION=$(git describe --tags --always)
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 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
uses: docker/build-push-action@v6
with:
context: .
file: ./.docker/Dockerfile.stage
platforms: linux/amd64
push: true
tags: ghcr.io/${{ env.REPO_LC }}:${{ env.RELEASE_VERSION }},ghcr.io/${{ env.REPO_LC }}:${{ env.COMMIT_SHA }}
- name: 'Setup yq'
uses: dcarbone/install-yq-action@v1.3.1
with:
version: 'v4.44.3'
force: true
- name: Bump version in values/stage.yaml
run: yq -i '.deployment.image.tag=strenv(RELEASE_VERSION)' ./k8s/values/stage.yaml
- uses: actions/create-github-app-token@v2
id: app-token
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
permission-contents: write
- name: Get GitHub App User ID
id: get-user-id
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
- name: Initialize mandatory git config
run: |
git config --global user.name '${{ steps.app-token.outputs.app-slug }}[bot]'
git config --global user.email '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com'
- name: Commit k8s values and push changes
run: |
git add ./k8s/values/stage.yaml
git commit --message "chore: update stage image to version ${{ env.RELEASE_VERSION }}" \
&& git push origin ${{ steps.extract_branch.outputs.branch }} \
|| echo "No changes to commit"