-
Notifications
You must be signed in to change notification settings - Fork 0
87 lines (73 loc) · 3.12 KB
/
deploy-server-image.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
---
name: Deploy server image
on:
workflow_dispatch:
inputs:
env:
description: "Deployment environment"
required: true
default: "staging"
jobs:
validate:
runs-on: ubuntu-latest
outputs:
env: ${{ steps.setenv.outputs.env }}
steps:
- name: Validate environment
id: setenv
run: |
if [[ "${{ github.event.inputs.env }}" != "prod" && "${{ github.event.inputs.env }}" != "staging" ]]; then
echo "Error: Invalid environment specified. Can only be 'prod' or 'staging'."
exit 1
fi
deploy_to_server:
needs: validate
runs-on: ubuntu-latest
environment: ${{ github.event.inputs.env }}
steps:
- name: Setup SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/private_key.pem
chmod 600 ~/.ssh/private_key.pem
ssh-keyscan -H ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts
- name: Checkout code
uses: actions/checkout@v4
- name: Copy docker compose files
run: |
scp -i ~/.ssh/private_key.pem -r ./docker/* ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }}:/home/${{ secrets.SERVER_USER }}/track/
echo "creating alias script"
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} <<"ENDSSH"
echo "dc() {" > track/alias
echo " APP_ENV=${{ vars.APP_ENV }} HOST_PORT=${{ vars.HOST_PORT }} POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} docker compose -f track/base.yml -f track/prod.yml \"\$@\"" >> track/alias
echo "}" >> track/alias
chmod +x track/alias
ENDSSH
echo "created alias script"
# - id: "auth"
# uses: "google-github-actions/auth@v1"
# with:
# credentials_json: "${{ secrets.GCP_SA_KEY }}"
# - name: "Set up Cloud SDK"
# uses: "google-github-actions/setup-gcloud@v1"
# - name: Configure Docker to use Google Artifact Registry
# run: gcloud auth configure-docker us-central1-docker.pkg.dev
- name: Prepare and Deploy
run: |
cat <<EOM >/tmp/gcp-sa-key.json
${{ secrets.GCP_SA_KEY }}
EOM
scp -i ~/.ssh/private_key.pem /tmp/gcp-sa-key.json ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }}:/tmp/gcp-sa-key.json
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} <<"ENDSSH"
gcloud auth activate-service-account --key-file=/tmp/gcp-sa-key.json
gcloud auth configure-docker us-central1-docker.pkg.dev
rm /tmp/gcp-sa-key.json
export APP_ENV=${{ vars.APP_ENV }}
export HOST_PORT=${{ vars.HOST_PORT }}
export POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}
docker compose -f track/base.yml -f track/prod.yml pull
docker compose -f track/base.yml -f track/prod.yml down
docker compose -f track/base.yml -f track/prod.yml up -d
echo "Status:"
docker compose -f track/base.yml -f track/prod.yml ps
ENDSSH