Skip to content

Merge pull request #2 from aafre/frontend #2

Merge pull request #2 from aafre/frontend

Merge pull request #2 from aafre/frontend #2

name: Build, Push, and Deploy to Cloud Run
on:
push:
branches:
- main
workflow_dispatch: # Allows manual trigger
inputs:
deploy:
description: "Deploy to Cloud Run after building?"
required: false
default: "false"
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
# Checkout repository code
- name: Checkout
uses: actions/checkout@v4
# Generate a timestamp for tagging
- name: Generate timestamp for Docker tag
run: |
TIMESTAMP=$(date -u +"%Y.%m.%d.%H-%M-%S")
echo "DOCKER_TAG=$TIMESTAMP" >> $GITHUB_ENV
# Authenticate with GCP
- name: Authenticate with GCP
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}
# Build the Docker image
- name: Build Docker image
run: |
docker build -t ${{ secrets.GCP_ARTIFACT_REGISTRY_REPO }}:${{ env.DOCKER_TAG }} .
docker images
# Configure Docker to use the GCP Container Registry
- name: Configure Docker to use GCP Container Registry
run: |
echo ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }} | docker login ${{ secrets.GCP_DOCKER_LOGIN }} -u _json_key_base64 --password-stdin
# Push the Docker image to Container Registry
- name: Push Docker image
run: |
docker push ${{ secrets.GCP_ARTIFACT_REGISTRY_REPO }}:${{ env.DOCKER_TAG }}
echo "Pushed Docker Image: ${{ secrets.GCP_ARTIFACT_REGISTRY_REPO }}:${{ env.DOCKER_TAG }}"
deploy:
needs: build-and-push
if: ${{ github.event.inputs.deploy == 'true' }}
runs-on: ubuntu-latest
steps:
# Authenticate with GCP
- name: Authenticate with GCP
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}
# Deploy to Cloud Run
- id: deploy-cloudrun
uses: google-github-actions/deploy-cloudrun@v2
with:
service: ${{ secrets.GCP_CLOUD_RUN_SERVICE }}
image: "${{ secrets.GCP_ARTIFACT_REGISTRY_REPO }}:${{ env.DOCKER_TAG }}"
region: "europe-west2"
# Output deployed URL
- name: Display Deployed URL
run: echo "Deployed to ${{ steps.deploy-cloudrun.outputs.url }}"