-
Notifications
You must be signed in to change notification settings - Fork 0
98 lines (78 loc) · 3.12 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
86
87
88
89
90
91
92
93
94
95
96
97
98
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"
SERVICE_ACCOUNT: ${{ secrets.GCP_SERVICE_ACCOUNT }}
WORKLOAD_IDENTITY_PROVIDER: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
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: Google Auth
id: auth
uses: google-github-actions/auth@v2
with:
token_format: 'access_token'
project_id: ${{ env.PROJECT_ID }}
workload_identity_provider: ${{ env.WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ env.SERVICE_ACCOUNT }}
- name: GAR login
id: gar-auth
uses: docker/login-action@v3
with:
registry: ${{ env.GAR_LOCATION }}-docker.pkg.dev
username: 'oauth2accesstoken'
password: ${{ steps.auth.outputs.access_token }}
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) }}"