Skip to content

Update gcp-setup-and-deployment.yaml #32

Update gcp-setup-and-deployment.yaml

Update gcp-setup-and-deployment.yaml #32

name: GCP Setup and Deployment
on:
push:
branches:
- main
- '*'
env:
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
GAR_REPOSITORY: docker-repo
GAR_LOCATION: europe-west1
GAR_URL: europe-west1-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/docker-repo
IMAGE_NAME: "cookeo-a-retro"
WIP_POOL: ${{ secrets.GCP_WIP_POOL }}
jobs:
gcp-setup:
runs-on: ubuntu-latest
# Add "id-token" with the intended permissions.
permissions:
contents: 'read'
id-token: 'write'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Authenticate to Google Cloud
uses: google-github-actions/[email protected]
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
workload_identity_provider: ${{ secrets.GOOGLE_WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GOOGLE_SERVICE_ACCOUNT }}
- name: Configure Docker to use Artifact Registry
run: |
gcloud auth configure-docker ${{ secrets.GOOGLE_REPO_DOCKER }}
build-and-deploy:
runs-on: ubuntu-latest
# Add "id-token" with the intended permissions.
permissions:
contents: 'read'
id-token: 'write'
needs: gcp-setup
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Build Docker Image
run: |
docker build -t "{{ env.GAR_URL }}/${{ env.IMAGE_NAME }}${{ github.ref == 'refs/heads/main' && '' || format('-{0}', github.ref_name) }}" .
- name: Push Docker Image to Artifact Registry
run: |
docker push "{{ env.GAR_URL }}/${{ env.IMAGE_NAME }}${{ github.ref == 'refs/heads/main' && '' || format('-{0}', github.ref_name) }}"
- name: Deploy to Cloud Run
uses: google-github-actions/deploy-cloudrun@v1
with:
image: "{{ env.GAR_URL }}/${{ env.IMAGE_NAME }}${{ github.ref == 'refs/heads/main' && '' || format('-{0}', github.ref_name) }}"
tag: latest
region: europe-west1 # Replace with your desired region
platform: managed
timeout: 120 # Optional: Set a timeout for the deployment
gcp-cleanup:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'deleted' && github.ref_type == 'branch' && !contains(github.ref, 'main') }}
steps:
- name: Set up gcloud
uses: google-github-actions/setup-gcloud@v1
with:
service_account_key: ${{ secrets.GCP_SA_KEY }}
project_id: ${{ secrets.GCP_PROJECT_ID }}
- name: Delete Docker Image from Artifact Registry
run: |
docker login -u _token -p ${{ secrets.GCP_SA_KEY }} gcr.io
docker pull "{{ env.GAR_URL }}/${{ env.IMAGE_NAME }}${{ github.ref == 'refs/heads/main' && '' || format('-{0}', github.ref_name) }}"
docker rmi "{{ env.GAR_URL }}/${{ env.IMAGE_NAME }}${{ github.ref == 'refs/heads/main' && '' || format('-{0}', github.ref_name) }}"