-
Notifications
You must be signed in to change notification settings - Fork 2
179 lines (157 loc) · 7.06 KB
/
publish_master.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
name: Docker production
on:
push:
# Publish `master` as Docker `prod` image.
branches:
- master
# Publish `v1.2.3` tags as releases.
tags:
- v*
env:
IMAGE_NAME: sysblokbot
jobs:
# Run tests.
# See also https://docs.docker.com/docker-hub/builds/automated-testing/
test:
runs-on: ubuntu-latest
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.9.1
with:
access_token: ${{ github.token }}
- uses: actions/checkout@v2
with:
ref: master
- name: Python Style Checker
working-directory: ${{ github.workspace }}
run: |
pip3 install --upgrade pip
export PATH="$HOME/.local/bin:$PATH"
pip3 install pycodestyle
pycodestyle . --max-line-length 100
- name: Notify us about failure
if: ${{ failure() }}
env:
TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_PROD_TOKEN }}
TELEGRAM_ERROR_CHAT_ID: ${{ secrets.TELEGRAM_ERROR_CHAT_ID }}
run: |
curl -X POST \
-H 'Content-Type: application/json' \
-d '{"parse_mode": "markdown", "chat_id": "${{ env.TELEGRAM_ERROR_CHAT_ID }}", "text": "[github CI] pycodestyle [failed](https://github.com/sysblok/sysblokbot/actions/runs/${{github.run_id}})"}' \
https://api.telegram.org/bot${{env.TELEGRAM_TOKEN}}/sendMessage
# - name: Python Pytest Github Action
# uses: fylein/python-pytest-github-action@v2
# with:
# args: apt-get update && apt-get install -y git && pip3 install -e . && pip3 install -r requirements.txt && pytest tests/unit
- name: Notify us about failure
if: ${{ failure() }}
env:
TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_PROD_TOKEN }}
TELEGRAM_ERROR_CHAT_ID: ${{ secrets.TELEGRAM_ERROR_CHAT_ID }}
run: |
curl -X POST \
-H 'Content-Type: application/json' \
-d '{"parse_mode": "markdown", "chat_id": ${{ env.TELEGRAM_ERROR_CHAT_ID }}, "text": "[github CI] Unit tests [failed](https://github.com/sysblok/sysblokbot/actions/runs/${{github.run_id}})"}' \
https://api.telegram.org/bot${{env.TELEGRAM_TOKEN}}/sendMessage
# Push image to GitHub Packages.
# See also https://docs.docker.com/docker-hub/builds/
push:
# Ensure test job passes before pushing image.
needs: test
runs-on: ubuntu-latest
if: github.event_name == 'push'
steps:
- uses: actions/checkout@v2
with:
ref: master
- name: Generate build args
id: args
run: |
echo "::set-output name=commit_hash::$(git rev-parse HEAD)"
echo "::set-output name=commit_hash_short::$(git rev-parse --short HEAD)"
- name: Publish to Github Packages Registry with cache
uses: whoan/docker-build-with-cache-action@v5
env:
IMAGE_NAME: ${{ env.IMAGE_NAME }}
COMMIT_HASH: "${{ steps.args.outputs.commit_hash }}"
COMMIT_HASH_SHORT: "${{ steps.args.outputs.commit_hash_short }}"
with:
image_name: ${{ github.repository }}/${{ env.IMAGE_NAME }}
registry: docker.pkg.github.com
username: sysblok
password: ${{ secrets.GITHUB_TOKEN }}
dockerfile: Dockerfile
image_tag: "prod,latest"
build_extra_args: "--build-arg=COMMIT_HASH --build-arg=COMMIT_HASH_SHORT"
- name: Notify us about failure
if: ${{ failure() }}
env:
TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_PROD_TOKEN }}
TELEGRAM_ERROR_CHAT_ID: ${{ secrets.TELEGRAM_ERROR_CHAT_ID }}
run: |
curl -X POST \
-H 'Content-Type: application/json' \
-d '{"parse_mode": "markdown", "chat_id": "${{ env.TELEGRAM_ERROR_CHAT_ID }}", "text": "[github CI] build [failed](https://github.com/sysblok/sysblokbot/actions/runs/${{github.run_id}})" \
}' \
https://api.telegram.org/bot${{env.TELEGRAM_TOKEN}}/sendMessage
- name: Deploy package to digitalocean
uses: appleboy/ssh-action@master
env:
GITHUB_USERNAME: sysblok
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
IMAGE_NAME: ${{ env.IMAGE_NAME }}
ROOT_DIR: /home/${{ secrets.MASTER_DO_USER }}/prod
TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_PROD_TOKEN }}
TELEGRAM_ERROR_CHAT_ID: ${{ secrets.TELEGRAM_ERROR_CHAT_ID }}
with:
host: ${{ secrets.MASTER_HOST }}
username: ${{ secrets.MASTER_DO_USER }}
passphrase: ${{ secrets.MASTER_DO_SSH_KEY_PASSWORD }}
key: ${{ secrets.MASTER_DO_SSH_KEY }}
port: ${{ secrets.MASTER_PORT }}
envs: GITHUB_USERNAME, GITHUB_TOKEN, IMAGE_NAME, ROOT_DIR, TELEGRAM_TOKEN, TELEGRAM_ERROR_CHAT_ID
script: |
export CONTAINER_ID=$(docker ps -aq --filter name=prod)
export IMAGE_ID=$(docker images -aq --filter reference='docker.pkg.github.com/sysblok/sysblokbot/sysblokbot:prod')
docker stop --time=30 $CONTAINER_ID
docker rm $CONTAINER_ID
docker rmi $IMAGE_ID
docker login docker.pkg.github.com -u $GITHUB_USERNAME -p $GITHUB_TOKEN
touch ${{ env.ROOT_DIR }}/sysblokbot.sqlite
touch ${{ env.ROOT_DIR }}/strings.sqlite
docker run -dit --name sysblokbot-prod \
--env APP_SOURCE="github CI" --restart unless-stopped \
--env TELEGRAM_ERROR_CHAT_ID="${{ env.TELEGRAM_ERROR_CHAT_ID }}" \
--env TELEGRAM_TOKEN="${{ env.TELEGRAM_TOKEN }}" \
-v ${{ env.ROOT_DIR }}/config_override.json:/app/config_override.json \
-v ${{ env.ROOT_DIR }}/config_gs.json:/app/config_gs.json \
-v ${{ env.ROOT_DIR }}/sysblokbot.sqlite:/app/sysblokbot.sqlite \
-v ${{ env.ROOT_DIR }}/strings.sqlite:/app/strings.sqlite \
-v ${{ env.ROOT_DIR }}/persistent_storage.pickle:/app/persistent_storage.pickle \
docker.pkg.github.com/sysblok/sysblokbot/${{ env.IMAGE_NAME }}:prod
- name: Notify us about failure
if: ${{ failure() }}
env:
TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_PROD_TOKEN }}
TELEGRAM_ERROR_CHAT_ID: ${{ secrets.TELEGRAM_ERROR_CHAT_ID }}
run: |
curl -X POST \
-H 'Content-Type: application/json' \
-d '{"parse_mode": "markdown", "chat_id": ${{ env.TELEGRAM_ERROR_CHAT_ID }}, "text": "[github CI] deploy [failed](https://github.com/sysblok/sysblokbot/actions/runs/${{github.run_id}})"}' \
https://api.telegram.org/bot${{env.TELEGRAM_TOKEN}}/sendMessage
integration_tests:
needs: push
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: master
- name: Integration tests
uses: fylein/python-pytest-github-action@v2
env:
CONFIG_OVERRIDE: ${{ secrets.CONFIG_OVERRIDE_PROD }}
with:
args: |
apt-get update && apt-get install -y git && \
pip3 install -e . && pip3 install -r requirements.txt && \
pytest tests/integration -vs