-
Notifications
You must be signed in to change notification settings - Fork 2
98 lines (80 loc) · 3.73 KB
/
build_and_deploy.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
name: Build and deploy when pushing on staging or main
on:
push:
branches:
- main
- staging
- feat/github-ci
jobs:
build-and-deploy:
environment: ${{ github.ref_name == 'main' && 'prod' || 'staging' }}
name: Deploy from ${{ github.ref_name }}/${{ github.sha }}
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Get current CI job ID
env:
GH_TOKEN: ${{ github.token }}
run: |
jobs=$(gh api repos/${{ github.repository }}/actions/runs/${{ github.run_id}}/attempts/${{ github.run_attempt }}/jobs)
job_id=$(echo $jobs | jq -r '.jobs[] | select(.runner_name=="${{ runner.name }}") | .id')
echo "CI_JOB_ID=$job_id" >> $GITHUB_ENV
echo "This CI job ID is ${{ env.CI_JOB_ID }}"
- name: Install SSH key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.CI_DEPLOY_USER_SSH_PRIVATE_KEY }}
name: id_rsa
known_hosts: ${{ secrets.CI_DEPLOY_HOST }}
- name: Adding server to known hosts
run: ssh-keyscan -H ${{ secrets.CI_DEPLOY_HOST }} >> ~/.ssh/known_hosts
- name: Prepare deployment files on runner for this CI job ID
run: |
mkdir -p ./${{ env.CI_JOB_ID }}
cp -r ./app ./${{ env.CI_JOB_ID }}
cp ./docker-compose.yml ./${{ env.CI_JOB_ID }}/docker-compose.yml
cp ./Dockerfile ./${{ env.CI_JOB_ID }}/Dockerfile
cp ./pyproject.toml ./${{ env.CI_JOB_ID }}/pyproject.toml
- name: Create .env file with current variables and secrets
run: |
cd ./${{ env.CI_JOB_ID }}
touch .env
echo JOIN_ON_INVITE=${{ vars.JOIN_ON_INVITE }} >> .env
echo SALT=${{ secrets.SALT }} >> .env
echo MATRIX_HOME_SERVER=${{ secrets.MATRIX_HOME_SERVER }} >> .env
echo MATRIX_BOT_USERNAME=${{ secrets.MATRIX_BOT_USERNAME }} >> .env
echo MATRIX_BOT_PASSWORD=${{ secrets.MATRIX_BOT_PASSWORD }} >> .env
echo ERRORS_ROOM_ID=${{ secrets.ERRORS_ROOM_ID }} >> .env
echo USER_ALLOWED_DOMAINS=${{ secrets.USER_ALLOWED_DOMAINS }} >> .env
echo GROUPS_USED=${{ vars.GROUPS_USED }} >> .env
echo ALBERT_API_URL=${{ secrets.ALBERT_API_URL }} >> .env
echo ALBERT_API_TOKEN=${{ secrets.ALBERT_API_TOKEN }} >> .env
echo ALBERT_MODEL_NAME=${{ vars.ALBERT_MODEL_NAME }} >> .env
echo ALBERT_MODE=${{ vars.ALBERT_MODE }} >> .env
cat .env
- name: Copy deployment files to remote server via SCP
uses: appleboy/scp-action@master
with:
host: ${{ secrets.CI_DEPLOY_HOST }}
username: ${{ secrets.CI_DEPLOY_USER }}
key: ${{ secrets.CI_DEPLOY_USER_SSH_PRIVATE_KEY }}
source: "./${{ env.CI_JOB_ID }}"
target: "/home/${{ secrets.CI_DEPLOY_USER }}"
- name: Execute server commands for deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.CI_DEPLOY_HOST }}
username: ${{ secrets.CI_DEPLOY_USER }}
key: ${{ secrets.CI_DEPLOY_USER_SSH_PRIVATE_KEY }}
script: |
cd /home/${{ secrets.CI_DEPLOY_USER }}/${{ env.CI_JOB_ID }}
mkdir -p /data/tchapbot
export COMPOSE_FILE=/home/${{ secrets.CI_DEPLOY_USER }}/${{ env.CI_JOB_ID }}/docker-compose.yml
export COMPOSE_PROJECT_NAME=albert-tchapbot
docker compose down
docker tag albert/tchapbot:latest albert/tchapbot:old || true
docker image rm albert/tchapbot:latest || true
docker compose up --detach
docker image rm albert/tchapbot:old || true
rm -rf /home/${{ secrets.CI_DEPLOY_USER }}/${{ env.CI_JOB_ID }}