-
Notifications
You must be signed in to change notification settings - Fork 1
121 lines (107 loc) · 3.54 KB
/
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
name: CI/CD Pipeline
# run it locally:
# act \
# --secret DOCKER_USERNAME=$DOCKER_USERNAME \
# --secret DOCKER_PASSWORD=$DOCKER_PASSWORD \
# --secret SERVER_IP=$SERVER_IP \
# --secret SERVER_USERNAME=$SERVER_USERNAME \
# --secret SERVER_SSH_KEY="$SERVER_SSH_KEY" \
# --env MEDIACLOUD_API_TOKEN=$MEDIACLOUD_API_TOKEN \
# --env ACLED_EMAIL=$ACLED_EMAIL \
# --env ACLED_KEY=$ACLED_KEY
on:
push:
branches:
- main
- dev
paths:
# only run on backend or ci-cd changes
- 'backend-*/**'
- '.github/workflows/**'
pull_request:
branches:
- main
- dev
paths:
# only run on backend or ci-cd changes
- 'backend-*/**'
- '.github/workflows/**'
jobs:
test:
runs-on: ubuntu-22.04
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.10'
- name: Install Poetry
run: |
pip install poetry
- name: Install dependencies
run: |
cd backend-python
poetry install
- name: Run pytest
run: |
cd backend-python
poetry run pytest
env:
MEDIACLOUD_API_TOKEN: ${{ secrets.MEDIACLOUD_API_TOKEN }}
ACLED_EMAIL: ${{ secrets.ACLED_EMAIL }}
ACLED_KEY: ${{ secrets.ACLED_KEY }}
ZENROWS_API_KEY: ${{ secrets.ZENROWS_API_KEY }}
build-and-push:
needs: test
runs-on: ubuntu-22.04
outputs:
docker_tag: ${{ steps.set_vars.outputs.TAG }}
port: ${{ steps.set_vars.outputs.PORT }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup tag name and port based on branch
id: set_vars
run: |
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "::set-output name=TAG::latest"
echo "::set-output name=PORT::8000"
else
echo "::set-output name=TAG::dev"
echo "::set-output name=PORT::8001"
fi
- name: Login to Docker Registry
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build Docker image
run: |
docker build \
-t socialchangelab/media-impact-monitor:${{ github.sha }} \
-t socialchangelab/media-impact-monitor:${{ steps.set_vars.outputs.TAG }} \
.
- name: Push Docker image
run: docker push --all-tags socialchangelab/media-impact-monitor
deploy:
needs: build-and-push
runs-on: ubuntu-22.04
steps:
- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USERNAME }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
sudo docker pull socialchangelab/media-impact-monitor:${{ needs.build-and-push.outputs.docker_tag }}
sudo docker stop media-impact-monitor-${{ needs.build-and-push.outputs.docker_tag }} || true
sudo docker rm media-impact-monitor-${{ needs.build-and-push.outputs.docker_tag }} || true
sudo docker run -d \
-p ${{ needs.build-and-push.outputs.port }}:8000 \
-e MEDIACLOUD_API_TOKEN=${{ secrets.MEDIACLOUD_API_TOKEN }} \
-e ACLED_EMAIL=${{ secrets.ACLED_EMAIL }} \
-e ACLED_KEY=${{ secrets.ACLED_KEY }} \
--name media-impact-monitor-${{ needs.build-and-push.outputs.docker_tag }} \
socialchangelab/media-impact-monitor:${{ needs.build-and-push.outputs.docker_tag }}