Skip to content

Merge pull request #94 from esc-chula/hackathon-community-ticket #50

Merge pull request #94 from esc-chula/hackathon-community-ticket

Merge pull request #94 from esc-chula/hackathon-community-ticket #50

name: Build and Deploy Staging
on:
workflow_dispatch:
push:
branches:
- develop
env:
GITOPS_REPO: esc-chula/tech-website-gitops
STAGING_DEPLOYMENT_PATH: overlays/staging/kustomization.yaml
IMAGE_NAME: ghcr.io/${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
outputs:
image_sha: ${{ steps.set-tag.outputs.sha_tag }}
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Set Image Tags
id: set-tag
run: |
echo "sha_tag=${{ github.sha }}" >> "$GITHUB_OUTPUT"
echo "IMAGE_NAME=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"
echo "SHA_TAG=${{ github.sha }}" >> "$GITHUB_ENV"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and Push Docker Image
uses: docker/build-push-action@v3
with:
file: Dockerfile
push: true
tags: ${{ env.IMAGE_NAME }}:${{ env.SHA_TAG }}
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache
cache-to: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache,mode=max
deploy-staging:
needs: build-and-push
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout GitOps Repository
uses: actions/checkout@v4
with:
repository: ${{ env.GITOPS_REPO }}
token: ${{ secrets.GITOPS_TOKEN }}
path: gitops
- name: Update Image Tag in GitOps Repository
env:
IMAGE_TAG: ${{ needs.build-and-push.outputs.image_sha }}
run: |
DEPLOYMENT_PATH="gitops/${{ env.STAGING_DEPLOYMENT_PATH }}"
# Update the `newTag` field inside `images` list
yq -i '(.images[] | select(.name == "ghcr.io/esc-chula/tech-website")).newTag = "${{ env.IMAGE_TAG }}"' "$DEPLOYMENT_PATH"
- name: Commit and Push Changes to GitOps Repo
run: |
cd gitops
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add .
git commit -m "build(deploy): Update staging deployment for ${{ needs.build-and-push.outputs.image_sha }}"
git push