Skip to content

Commit ac9f68f

Browse files
committed
ft: add automated deploy job
1 parent 8f6613e commit ac9f68f

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

.github/workflows/build-push-gcloud.yaml

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,49 +34,57 @@ on:
3434
tag:
3535
description: "Image tag"
3636
value: ${{ jobs.push.outputs.tag }}
37+
full_tag:
38+
description: "Full image tag"
39+
value: ${{ jobs.push.outputs.full_tag }}
3740
jobs:
3841
push:
3942
name: "Build & Push"
4043
runs-on: ubuntu-latest
4144
outputs:
4245
imageid: ${{ steps.build.outputs.imageid }}
43-
tag: ${{ steps.vars.outputs.branch }}-${{ steps.vars.outputs.sha_short }}
46+
tag: ${{ steps.vars.outputs.tag }}
47+
full_tag: ${{ steps.vars.outputs.full_tag }}
4448
steps:
4549
- uses: actions/checkout@v4
4650
- name: Docker login to Google Cloud
4751
id: vars
4852
run: |
4953
echo "$B64_GOOGLE_KEY" | docker login -u _json_key_base64 --password-stdin https://$REGISTRY
50-
echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
54+
sha_short=$(git rev-parse --short HEAD)
5155
branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
52-
echo "branch=$(echo "$branch" | tr '/\' '-')" >> $GITHUB_OUTPUT
56+
tag="${branch}-${sha_short}"
57+
echo "tag=$tag" >> $GITHUB_OUTPUT
58+
echo "full_tag=$REGISTRY/$GCP_PROJECT/$REPO:$tag" >> $GITHUB_OUTPUT
5359
shell: bash
5460
env:
61+
GCP_PROJECT: ${{ secrets.GCLOUD_PROJECT }}
5562
B64_GOOGLE_KEY: ${{ secrets.GCLOUD_SECRET_KEY }}
5663
REGISTRY: ${{ inputs.registry }}
57-
# - name: Check image
58-
# id: image_exists
59-
# continue-on-error: true
60-
# uses: cloudposse/github-action-docker-image-exists@main
61-
# with:
62-
# registry: ${{ inputs.registry }}
63-
# image_name: ${{ secrets.GCLOUD_PROJECT }}/${{ inputs.repo }}
64-
# tag: ${{ steps.vars.outputs.branch }}-${{ steps.vars.outputs.sha_short }}
64+
REPO: ${{ inputs.repo }}
65+
- name: Check image
66+
id: image_exists
67+
continue-on-error: true
68+
uses: cloudposse/github-action-docker-image-exists@main
69+
with:
70+
registry: ${{ inputs.registry }}
71+
image_name: ${{ secrets.GCLOUD_PROJECT }}/${{ inputs.repo }}
72+
tag: ${{ steps.vars.outputs.branch }}-${{ steps.vars.outputs.sha_short }}
6573
- uses: docker/setup-buildx-action@v3
6674
if: steps.image_exists.outcome == 'failure'
6775
- uses: docker/build-push-action@v5
68-
# if: steps.image_exists.outcome == 'failure'
76+
if: steps.image_exists.outcome == 'failure'
6977
id: build
7078
with:
7179
context: ${{ inputs.context }}
7280
file: ${{ inputs.dockerfile }}
73-
# cache-from: type=gha
74-
# cache-to: type=gha,mode=max
81+
cache-from: type=gha
82+
cache-to: type=gha,mode=max
7583
push: true
7684
labels: |
7785
ci.run_id=${{ github.run_id }}
7886
build-args: |
7987
${{ vars.BUILD_ARGS }}
8088
${{ inputs.build_args }}
8189
tags: |
82-
${{ inputs.registry }}/${{ secrets.GCLOUD_PROJECT }}/${{ inputs.repo }}:${{ steps.vars.outputs.branch }}-${{ steps.vars.outputs.sha_short }}
90+
${{ steps.vars.outputs.full_tag }}

.github/workflows/cd-build.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,27 @@ jobs:
2727
build_args: |
2828
VITE_GRPC_BACKEND=https://whisper-notes.shivanshs9.me/api
2929
secrets: inherit
30+
deploy:
31+
name: Deploy
32+
runs-on: ubuntu-latest
33+
needs: [push-backend, push-frontend]
34+
steps:
35+
- uses: actions/checkout@v4
36+
- uses: fregante/setup-git-user@v2
37+
- name: "Update Deployment Image Tag"
38+
working-directory: "iac/services"
39+
run: |
40+
# Write the new image tags to the kustomization files
41+
cd backend
42+
kustomize edit set image grpc-backend=$BACKEND_IMAGE
43+
cd ../frontend
44+
kustomize edit set image frontend=$FRONTEND_IMAGE
45+
46+
# Commit the changes
47+
git add -u
48+
sha_short=$(git rev-parse --short HEAD)
49+
git commit -m "cd: Update image to main-$sha_short"
50+
git push
51+
env:
52+
BACKEND_IMAGE: ${{ needs.push-backend.outputs.full_tag }}
53+
FRONTEND_IMAGE: ${{ needs.push-frontend.outputs.full_tag }}

0 commit comments

Comments
 (0)