ci: fix stage image in k8s deployment #52
This file contains hidden or 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/* | |
- develop | |
jobs: | |
build_and_push_docker: | |
runs-on: ubuntu-latest | |
permissions: | |
packages: write | |
actions: write | |
outputs: | |
branch: ${{ steps.extract_branch.outputs.branch }} | |
version: ${{ env.RELEASE_VERSION }} | |
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 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/${{ github.repository }}:${{ env.RELEASE_VERSION }},ghcr.io/${{ github.repository }}:${{ env.COMMIT_SHA }} | |
update_deployment: | |
runs-on: ubuntu-latest | |
needs: build_and_push_docker | |
permissions: | |
contents: write | |
env: | |
branch: ${{ needs.build_and_push_docker.outputs.branch }} | |
version: ${{ needs.build_and_push_docker.outputs.version }} | |
steps: | |
- name: Get Green Ecolution App Token | |
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: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ steps.app-token.outputs.token }} | |
fetch-depth: 0 | |
fetch-tags: true | |
- 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(version)' ./k8s/values/stage.yaml | |
- name: Get Green Ecolution 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 | |
env: | |
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} | |
run: | | |
git add ./k8s/values/stage.yaml | |
git commit --message "chore: update stage image to version ${{ env.version }} [skip ci]" \ | |
&& git push origin ${{ env.branch }} \ | |
|| echo "No changes to commit" |