-
Notifications
You must be signed in to change notification settings - Fork 0
135 lines (119 loc) · 4.68 KB
/
deploy_remote_server.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
134
135
name: Deploy to Ubuntu Server, build and run docker
on:
push:
branches:
- main
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
# 1. Checkout repository
checkout:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
# 2. Get Docker container ID
get_container_id:
runs-on: ubuntu-latest
needs: checkout
outputs:
container_id: ${{ steps.get_container_id.outputs.container_id }}
steps:
- name: Setup SSH
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Get Docker container ID
id: get_container_id
run: |
CONTAINER_ID=$(ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} 'docker ps -qf name=${{ secrets.CONTAINER_NAME }}')
echo "container_id=$CONTAINER_ID" >> "$GITHUB_OUTPUT"
# 3. Update code
update_code:
runs-on: ubuntu-latest
needs: get_container_id
outputs:
container_id: ${{ needs.get_container_id.outputs.container_id }}
steps:
- name: Setup SSH
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Extract branch name
id: extract_branch
run: |
BRANCH_NAME=$(echo "${GITHUB_REF#refs/heads/}")
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
echo "container_id=${{ needs.get_container_id.outputs.container_id }}" >> "$GITHUB_OUTPUT"
- name: Update code
run: |
BRANCH_NAME="${{ steps.extract_branch.outputs.branch }}"
ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} 'cd /root/MEOWLSServer/MEOWLSServer/ && git pull origin $BRANCH_NAME'
# 4. Build Docker image
build_docker_image:
runs-on: ubuntu-latest
needs: update_code
outputs:
container_id: ${{ needs.update_code.outputs.container_id }}
steps:
- name: Setup SSH
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Build Docker image
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} 'cd /root/MEOWLSServer/MEOWLSServer/ && docker build -t ${{ secrets.CONTAINER_NAME }} .'
echo "container_id=${{ needs.update_code.outputs.container_id }}" >> "$GITHUB_OUTPUT"
# 5. Stop and remove old container
stop_remove_old_container:
runs-on: ubuntu-latest
needs: build_docker_image
if: ${{ needs.build_docker_image.outputs.container_id != '' }}
steps:
- name: Setup SSH
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Stop and remove old container
run: |
echo "Remove container with ID ${{ needs.build_docker_image.outputs.container_id }}"
ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} 'docker container stop ${{ needs.build_docker_image.outputs.container_id }} && docker container rm ${{ needs.build_docker_image.outputs.container_id }}'
# 6. Run new container
run_new_container:
runs-on: ubuntu-latest
needs:
- stop_remove_old_container
- build_docker_image
if: ${{ always() }}
steps:
- name: Setup SSH
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Run new container
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} '
docker run --restart=always \
--name "${{ secrets.CONTAINER_NAME }}" \
--network meowls_network \
-e DATABASE_HOST="${{ secrets.DATABASE_HOST }}" \
-e DATABASE_NAME="${{ secrets.DATABASE_NAME }}" \
-e DATABASE_PASSWORD="${{ secrets.DATABASE_PASSWORD }}" \
-e DATABASE_USERNAME="${{ secrets.DATABASE_USERNAME }}" \
-e DADATA_TOKEN="${{ secrets.DADATA_TOKEN }}" \
-p "${{ secrets.DATABASE_PORT }}" \
-d "${{ secrets.CONTAINER_NAME }}:latest"'
# 6. Clean up unused images and containers
clean_up:
runs-on: ubuntu-latest
needs: run_new_container
if: ${{ always() }}
steps:
- name: Setup SSH
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Clean up unused images and containers
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} 'docker system prune -a -f'