-
Notifications
You must be signed in to change notification settings - Fork 0
133 lines (120 loc) · 4.56 KB
/
api-cd.yml
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
name: Api Production deploy
on:
push:
branches:
- "main"
paths:
- "api/**"
- ".github/workflows/api-cd.yml"
workflow_dispatch:
jobs:
build-api:
permissions:
id-token: write
contents: read
name: Build and push to Google ACR
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- name: Checkout
uses: actions/checkout@v4
- id: 'setup-qemu'
name: Set up QEMU
uses: docker/setup-qemu-action@v3
- id: 'docker-buildx-setup'
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@v2'
with:
create_credentials_file: true
token_format: access_token
workload_identity_provider: 'projects/5685154754/locations/global/workloadIdentityPools/cd-beerpong/providers/github-actions'
service_account: 'cd-beerpong@beer-pong-441815.iam.gserviceaccount.com'
- id: 'login-gar'
name: "Login to GAR"
uses: docker/login-action@v3
with:
registry: europe-west10-docker.pkg.dev/beer-pong-441815/api-beerpong
username: oauth2accesstoken
password: ${{ steps.auth.outputs.access_token }}
- id: 'build-and-push'
name: 'Build and Push docker Image'
uses: docker/build-push-action@v6
with:
push: true
context: api
file: ./api/Dockerfile
platforms: linux/amd64
tags: europe-west10-docker.pkg.dev/beer-pong-441815/api-beerpong/api:${{ github.sha }}
needs: build-api
steps:
- name: Checkout
uses: actions/checkout@v4
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@v2'
with:
create_credentials_file: true
token_format: access_token
workload_identity_provider: 'projects/5685154754/locations/global/workloadIdentityPools/cd-beerpong/providers/github-actions'
service_account: 'cd-beerpong@beer-pong-441815.iam.gserviceaccount.com'
- id: 'deploy'
uses: 'google-github-actions/deploy-cloudrun@v2'
with:
service: 'api-springboot'
image: 'europe-west10-docker.pkg.dev/beer-pong-441815/api-beerpong/api:${{ github.sha }}'
region: europe-west10
flags: '--port=8080 --add-cloudsql-instances=beer-pong-441815:europe-west10:api-beerpong-pg --no-cpu-throttling --min-instances 0 --max-instances 3 --allow-unauthenticated'
env_vars: |
POSTGRES_USER=postgres
POSTGRES_URL=jdbc:postgresql:///beerpong?cloudSqlInstance=beer-pong-441815:europe-west10:api-beerpong-pg&socketFactory=com.google.cloud.sql.postgres.SocketFactory
API_BASE_URL=https://beerpong.lb.laurinnotemann.dev
secrets: |-
POSTGRES_PASSWORD=api-pg-password:latest
deploy-api:
permissions:
id-token: write
contents: read
name: "Deploy staging api on a bare metal server"
runs-on: ubuntu-latest
defaults:
run:
shell: bash
needs: build-api
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up SSH key
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.SSH_SECRET }}
- name: Add server to known hosts
run: |
mkdir -p ~/.ssh
ssh-keyscan -H ${{ secrets.SERVER_IP }} >> ~/.ssh/known_hosts
- name: Deploy to Bare Metal Server
env:
SERVER_IP: ${{ secrets.SERVER_IP }}
SERVER_USER: ${{ secrets.SERVER_USER }}
FILE_PATH: ${{ secrets.PATH_TO_SERVICE_ACC_JSON }}
DOCKER_IMAGE: europe-west10-docker.pkg.dev/beer-pong-441815/api-beerpong/api:${{ github.sha }}
run: |
ssh $SERVER_USER@$SERVER_IP << EOF
# Navigate to the Docker Compose directory
cd ~/docker/beerpong-api-prod
# Authenticate with Google Cloud (assuming gcloud is installed on the server)
gcloud auth activate-service-account --key-file=$FILE_PATH
gcloud auth configure-docker europe-west10-docker.pkg.dev
# Update the image in the docker-compose.yml file
sed -i 's|image: .*|image: $DOCKER_IMAGE|' docker-compose.yml
# Pull the latest image
docker compose pull
# Restart the service
docker compose up -d --force-recreate beerpong-api
# Clean up old images
docker image prune -af
EOF