change to trigger new image build #54
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: cicd | |
on: | |
push: | |
branches: [ main ] | |
workflow_dispatch: | |
env: | |
GCP_CLUSTER_NAME: website-cluster | |
GCP_PROJECT_ID: personal-website-421100 | |
GCP_REGION: us-east1-d | |
DOCKER_TAG: ${{ github.sha }} | |
IMAGE_NAME: personal-site | |
jobs: | |
check-changed-files: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Get changed files for the site | |
id: changed-site-files | |
uses: tj-actions/changed-files@v41 | |
with: | |
files: | | |
app.js | |
routes/** | |
views/** | |
public/** | |
Dockerfile | |
- name: Get changed infrastructure files | |
id: changed-infra-files | |
uses: tj-actions/changed-files@v41 | |
with: | |
files: | | |
helm/** | |
outputs: | |
site-changed: ${{ steps.changed-site-files.outputs.any_changed }} | |
infra-changed: ${{ steps.changed-infra-files.outputs.any_changed }} | |
build-push-image: | |
runs-on: ubuntu-latest | |
needs: check-changed-files | |
if: | | |
needs.check-changed-files.outputs.site-changed == 'true' | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Build docker image | |
run: | | |
docker build -t gcr.io/${{ env.GCP_PROJECT_ID }}/${{ env.IMAGE_NAME }}:${{ env.DOCKER_TAG }} -f Dockerfile . | |
- name: Authenticate to Google Cloud | |
uses: google-github-actions/auth@v2 | |
with: | |
credentials_json: ${{ secrets.GCP_SA_KEY }} | |
- name: Setup GCloud CLI | |
uses: google-github-actions/setup-gcloud@v2 | |
with: | |
version: '>= 363.0.0' | |
project_id: ${{ env.GCP_PROJECT_ID }} | |
- name: Configure Docker with GCloud CLI | |
run: | | |
gcloud auth configure-docker --quiet | |
- name: Push docker image to GCR | |
run: | | |
docker push gcr.io/${{ env.GCP_PROJECT_ID }}/${{ env.IMAGE_NAME }}:${{ env.DOCKER_TAG }} | |
deploy-service-to-gcp: | |
runs-on: ubuntu-latest | |
needs: | |
- check-changed-files | |
- build-push-image | |
if: | | |
always() && | |
(needs.build-push-image.result == 'success' && needs.check-changed-files.outputs.site-changed == 'true') || | |
(needs.build-push-image.result == 'skipped' && needs.check-changed-files.outputs.infra-changed == 'true') | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Update deployment | |
if: needs.check-changed-files.outputs.site-changed == 'true' | |
run: | | |
sed -i 's|image:.*|image: gcr.io/${{ env.GCP_PROJECT_ID }}/${{ env.IMAGE_NAME }}:${{ env.DOCKER_TAG }}|' helm/templates/deployment.yaml | |
- name: Authenticate to Google Cloud | |
uses: google-github-actions/auth@v2 | |
with: | |
credentials_json: ${{ secrets.GCP_SA_KEY }} | |
- name: Get GKE Credentials | |
uses: google-github-actions/get-gke-credentials@v2 | |
with: | |
cluster_name: ${{ env.GCP_CLUSTER_NAME }} | |
location: ${{ env.GCP_REGION }} | |
- name: Create Gmail App Password Kubernetes Secret | |
run: | | |
kubectl create secret generic gmail-secret --from-literal=GMAIL_PASSWORD="${{ secrets.GOOGLE_APP_PASSWORD }}" --dry-run=client -o yaml | kubectl apply -f - | |
- name: Deploy the new site to the cluster with helm | |
if: | | |
needs.check-changed-files.outputs.site-changed == 'true' || | |
(needs.build-push-image.result == 'skipped' && needs.check-changed-files.outputs.infra-changed == 'true') | |
run: | | |
cd helm && helm upgrade --install website . --set image.tag=${{ env.DOCKER_TAG }} | |