-
Notifications
You must be signed in to change notification settings - Fork 0
85 lines (70 loc) · 2.88 KB
/
gcp-setup-and-deployment.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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) }}"