-
Notifications
You must be signed in to change notification settings - Fork 0
88 lines (71 loc) Β· 2.48 KB
/
deploy.yml
File metadata and controls
88 lines (71 loc) Β· 2.48 KB
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
name: Deploy to GCP GCE
on:
push:
branches: [ "release" ]
workflow_dispatch:
env:
IMAGE_NAME: resttest-be
GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
GCP_REGION: ${{ secrets.GCP_REGION }}
ARTIFACT_REPO: ${{ secrets.ARTIFACT_REPO }}
ARTIFACT_REGISTRY: ${{ secrets.GCP_REGION }}-docker.pkg.dev
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v3
- name: Auth to GCP
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2
with:
project_id: ${{ env.GCP_PROJECT_ID }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Configure Docker for Artifact Registry
run: |
gcloud auth configure-docker ${{ env.ARTIFACT_REGISTRY }} --quiet
- name: Build and Push Docker Image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: |
${{ env.ARTIFACT_REGISTRY }}/${{ env.GCP_PROJECT_ID }}/${{ env.ARTIFACT_REPO }}/${{ env.IMAGE_NAME }}:latest
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
- name: Copy Docker Compose file via SCP
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.GCE_HOST }}
username: ${{ secrets.GCE_SSH_USER }}
key: ${{ secrets.GCE_SSH_KEY }}
source: "docker-compose.prod.yml"
target: "~/app-path"
- name: Deploy to GCE via SSH
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.GCE_HOST }}
username: ${{ secrets.GCE_SSH_USER }}
key: ${{ secrets.GCE_SSH_KEY }}
script: |
mkdir -p ~/app-path
cd ~/app-path
cat << 'EOF' > .env
${{ secrets.ENV_PROD }}
EOF
gcloud auth configure-docker ${{ env.ARTIFACT_REGISTRY }} --quiet
docker compose -f docker-compose.prod.yml down
docker compose -f docker-compose.prod.yml pull
docker compose -f docker-compose.prod.yml run --rm -e MODE=prod fastapi alembic upgrade head
docker compose -f docker-compose.prod.yml up -d