-
Notifications
You must be signed in to change notification settings - Fork 0
191 lines (151 loc) · 9.92 KB
/
prod.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
180
181
182
183
184
185
186
187
188
189
190
191
name: Build and Deploy in Server Lapig Task
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
outputs:
APPLICATION_NAME: ${{ steps.creat_env.outputs.APPLICATION_NAME }}
DOCKER_PATH: ${{ steps.creat_env.outputs.DOCKER_PATH }}
START_TIME: ${{ steps.creat_env.outputs.START_TIME }}
LAST_COMMIT_HASH: ${{ steps.creat_env.outputs.LAST_COMMIT_HASH }}
LAPIG_AUTHOR_NAME: ${{ steps.creat_env.outputs.LAPIG_AUTHOR_NAME }}
LAPIG_AUTHOR_EMAIL: ${{ steps.creat_env.outputs.LAPIG_AUTHOR_EMAIL }}
LAPIG_AUTHOR_DATE: ${{ steps.creat_env.outputs.LAPIG_AUTHOR_DATE }}
LAPIG_PROJECT_NAME: ${{ steps.creat_env.outputs.LAPIG_PROJECT_NAME }}
LAPIG_COMMIT_DATE: ${{ steps.creat_env.outputs.LAPIG_COMMIT_DATE }}
CREATE_STATUS: ${{steps.deploy_container.outputs.CREATE_STATUS}}
DELETE_STATUS: ${{steps.delete_container.outputs.DELETE_STATUS}}
START_STATUS: ${{steps.start_container.outputs.START_STATUS}}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Define a variável application_name
id: creat_env
run: |
echo "START_TIME=$(date -u +%s)" >> "$GITHUB_OUTPUT"
echo "DOCKER_PATH=prod" >> $GITHUB_ENV
echo "DOCKER_PATH=prod" >> $GITHUB_OUTPUT
repo_full_name=$GITHUB_REPOSITORY
repo_name=${repo_full_name##*/}
echo "Nome do repositório: $repo_name"
echo "application_name=app_task" >> $GITHUB_ENV
echo "APPLICATION_NAME=app_task" >> $GITHUB_OUTPUT
echo "LAST_COMMIT_HASH=$(git log --pretty=format:%h -n 1)" >> $GITHUB_ENV
echo "LAST_COMMIT_HASH=$(git log --pretty=format:%h -n 1)" >> "$GITHUB_OUTPUT"
echo "LAPIG_AUTHOR_NAME=$(git show -s --pretty=%an)" >> "$GITHUB_OUTPUT"
echo "LAPIG_AUTHOR_EMAIL=$(git show -s --pretty=%ae)" >> "$GITHUB_OUTPUT"
echo "LAPIG_AUTHOR_DATE=$(git log -1 --format=%cd --date=local)" >> "$GITHUB_OUTPUT"
echo "LAPIG_PROJECT_NAME=$repo_name" >> "$GITHUB_OUTPUT"
echo "LAPIG_COMMIT_DATE=$(git show -s --format=%ci)" >> "$GITHUB_OUTPUT"
- name: Create version.json
run: echo "{\"commitId\":\"$LAST_COMMIT_HASH\"}" > version.json
- name: Build Docker
run: |
docker build -t lapig/$application_name:${DOCKER_PATH}_${LAST_COMMIT_HASH} -t lapig/$application_name:${DOCKER_PATH}_latest -f docker/$DOCKER_PATH/Dockerfile . --no-cache
- name: Login to Docker Registry
run: echo "${{ secrets.DOCKERHUB_PASS }}" | docker login -u "lapig" --password-stdin
- name: Push Image to Registry
run: |
docker push lapig/$application_name:${DOCKER_PATH}_${LAST_COMMIT_HASH}
docker push lapig/$application_name:${DOCKER_PATH}_latest
- name: Remove image locally
run: |
docker rmi lapig/$application_name:${DOCKER_PATH}_${LAST_COMMIT_HASH}
docker rmi lapig/$application_name:${DOCKER_PATH}_latest
deploy:
needs: build
runs-on: self-hosted
steps:
- name: Load env
run: |
echo "application_name=${{needs.build.outputs.APPLICATION_NAME}}" >> $GITHUB_ENV
echo "DOCKER_PATH=${{needs.build.outputs.DOCKER_PATH}}" >> $GITHUB_ENV
- name: Pull Image on Server
run: |
docker pull lapig/$application_name:${DOCKER_PATH}_latest
docker pull redis:7
- name: Delete Container
id: delete_container
continue-on-error: true
run: |
delete_status_api=$(curl -v -X DELETE -w "%{http_code}" http://$DOCKER_HOST/containers/task_api?force=true 2>/dev/null)
delete_status_dash=$(curl -v -X DELETE -w "%{http_code}" http://$DOCKER_HOST/containers/task_dashboard?force=true 2>/dev/null)
delete_status_redis=$(curl -v -X DELETE -w "%{http_code}" http://$DOCKER_HOST/containers/task_redis?force=true 2>/dev/null)
delete_status_work=$(curl -v -X DELETE -w "%{http_code}" http://$DOCKER_HOST/containers/task_worker?force=true 2>/dev/null)
echo $delete_status_api
- name: Deploy Container
id: deploy_container
run: |
create_status_api=$(curl -v -X POST -H 'Content-Type: application/json' -d @/compose/$application_name/api.json -s -o /dev/null -w "%{http_code}" http://$DOCKER_HOST/containers/create?name=task_api 2>/dev/null)
create_status_dash=$(curl -v -X POST -H 'Content-Type: application/json' -d @/compose/$application_name/dashboard.json -s -o /dev/null -w "%{http_code}" http://$DOCKER_HOST/containers/create?name=task_dashboard 2>/dev/null)
create_status_redis=$(curl -v -X POST -H 'Content-Type: application/json' -d @/compose/$application_name/redis.json -s -o /dev/null -w "%{http_code}" http://$DOCKER_HOST/containers/create?name=task_redis 2>/dev/null)
create_status_work=$(curl -v -X POST -H 'Content-Type: application/json' -d @/compose/$application_name/worker.json -s -o /dev/null -w "%{http_code}" http://$DOCKER_HOST/containers/create?name=task_worker 2>/dev/null)
echo $create_status_api
- name: Start Container
id: start_container
run: |
start_status_redis=$(curl -v -X POST -s -o /dev/null -w "%{http_code}" http://$DOCKER_HOST/containers/task_redis/start 2>/dev/null)
start_status_api=$(curl -v -X POST -s -o /dev/null -w "%{http_code}" http://$DOCKER_HOST/containers/task_api/start 2>/dev/null)
start_status_dash=$(curl -v -X POST -s -o /dev/null -w "%{http_code}" http://$DOCKER_HOST/containers/task_dashboard/start 2>/dev/null)
start_status_work=$(curl -v -X POST -s -o /dev/null -w "%{http_code}" http://$DOCKER_HOST/containers/task_worker/start 2>/dev/null)
echo $start_status_api
post-deploy:
runs-on: ubuntu-latest
needs: deploy
if: ${{ always() || needs.build.result == 'failure' ||needs.deploy.result == 'failure' }}
steps:
- name: Load env
run: |
echo "LAST_COMMIT_HASH=${{needs.build.outputs.LAST_COMMIT_HASH}}" >> $GITHUB_ENV
echo "LAPIG_AUTHOR_NAME=${{needs.build.outputs.LAPIG_AUTHOR_NAME}}" >> $GITHUB_ENV
echo "LAPIG_AUTHOR_EMAIL=${{needs.build.outputs.LAPIG_AUTHOR_EMAIL}}" >> $GITHUB_ENV
echo "LAPIG_AUTHOR_DATE=${{needs.build.outputs.LAPIG_AUTHOR_DATE}}" >> $GITHUB_ENV
echo "LAPIG_PROJECT_NAME=${{needs.build.outputs.LAPIG_PROJECT_NAME}}" >> $GITHUB_ENV
echo "LAPIG_COMMIT_DATE=${{needs.build.outputs.LAPIG_COMMIT_DATE}}" >> $GITHUB_ENV
echo "CREATE_STATUS=${{needs.deploy.outputs.CREATE_STATUS}}" >> $GITHUB_ENV
echo "DELETE_STATUS=${{needs.deploy.outputs.DELETE_STATUS}}" >> $GITHUB_ENV
echo "START_STATUS=${{needs.deploy.outputs.START_STATUS}}" >> $GITHUB_ENV
- name: Send message to Telegram
run: |
end_time=$(date -u +%s)
echo "LAPIG_DURATION=$((end_time - $START_TIME))" >> $GITHUB_ENV
curl -s -X POST "https://api.telegram.org/bot${{ secrets.TELEGRAM_API_KEY }}/sendMessage" -d "chat_id=${{ secrets.CHAT_ID }}" -d "parse_mode=markdown" -d "text=*Project*: *${LAPIG_PROJECT_NAME}*\
*Status:*${{ needs.deploy.result }}\
*Branch*: ${LAPIG_BRANCH_NAME}\
*Author*: *${LAPIG_AUTHOR_NAME}*\
*Author_Email*: *${LAPIG_AUTHOR_EMAIL}*\
*Commit_ID*: *${LAST_COMMIT_HASH}*\
*Date_Commit*: *${LAPIG_COMMIT_DATE}*\
*Duration*: *${LAPIG_DURATION}*"
- name: creat message to Discord
run: |
if [[ "${{ needs.deploy.result }}" == "success" && "$START_STATUS" == "OK" && "$DELETE_STATUS" == "OK" && "$CREATE_STATUS" == "OK" ]]; then
echo "avatar_url=${{vars.LAPIG_SUCCESS}}" >> $GITHUB_ENV
status_geral="Sucesso"
else
echo "avatar_url=${{vars.LAPIG_ERROR}}" >> $GITHUB_ENV
status_geral="Falhou"
fi
case $START_STATUS in
"OK") START_STATUS_D="{ \"title\": \"DOCKER START OK!\",\"color\": 65280},";;
*) START_STATUS_D="{ \"title\": \"DOCKER START ERROR!\",\"color\": 16711680},";;
esac
case $DELETE_STATUS in
"OK") DELETE_STATUS_D="{ \"title\": \"DOCKER DELETE OK!\",\"color\": 65280},";;
*) DELETE_STATUS_D="{ \"title\": \"DOCKER DELETE ERROR!\",\"color\": 16711680},";;
esac
case $CREATE_STATUS in
"OK") CREATE_STATUS_D="{ \"title\": \"DOCKER CREATE OK!\",\"color\": 65280},";;
*) CREATE_STATUS_D="{ \"title\": \"DOCKER CREATE ERROR!\",\"color\": 16711680},";;
esac
echo "EMBEDS_STATUS=$DELETE_STATUS_D $CREATE_STATUS_D $START_STATUS_D" >> $GITHUB_ENV
echo "discordDesc=Result: $status_geral\nProject: $LAPIG_PROJECT_NAME\nCommit: $LAST_COMMIT_HASH\nAuthor: $LAPIG_AUTHOR_NAME\nAuthor_Email: $LAPIG_AUTHOR_EMAIL\nDate: $LAPIG_COMMIT_DATE\nDuration: $LAPIG_DURATION seconds" >> $GITHUB_ENV
echo "discordFooter=${{ github.workflow }} (#${{ github.run_number }})" >> $GITHUB_ENV
echo "discordTitle=${{ github.workflow }} (build #${{ github.run_number }})" >> $GITHUB_ENV
- name: Send menssage to Discord
run: |
echo "{\"content\": \"$discordTitle\", \"embeds\": [ { \"title\": \"$discordTitle\", \"description\": \"$discordDesc\", \"footer\": { \"text\": \"$discordFooter\" }, \"thumbnail\": { \"url\":\"$avatar_url\"} }, $EMBEDS_STATUS] ], \"username\": \"GitHub Actions\", \"avatar_url\": \"$avatar_url\" }"
curl -H "Content-Type: application/json" -X POST -d "{\"content\": \"$discordTitle\", \"embeds\": [ $EMBEDS_STATUS { \"title\": \"$discordTitle\", \"description\": \"$discordDesc\", \"footer\": { \"text\": \"$discordFooter\" }, \"thumbnail\": { \"url\":\"$avatar_url\"} }], \"username\": \"GitHub Actions\", \"avatar_url\": \"$avatar_url\" }" https://discord.com/api/webhooks/${{ secrets.DISCORD_KEY }}