-
Notifications
You must be signed in to change notification settings - Fork 1
123 lines (96 loc) · 6.32 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
name: Build and Deploy in Homology Server
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Define a variável application_name
run: |
echo "branch=${GITHUB_REF##*/}" >> $GITHUB_ENV
repo_full_name=$GITHUB_REPOSITORY
repo_name=${repo_full_name##*/}
echo "START_TIME=$(date -u +%s)" >> $GITHUB_ENV
echo "Nome do repositório: $repo_name"
echo "application_name=$repo_name" >> $GITHUB_ENV
echo "LAST_COMMIT_HASH=$(git log --pretty=format:%h -n 1)" >> $GITHUB_ENV
echo "LAPIG_AUTHOR_NAME=$(git show -s --pretty=%an)" >> $GITHUB_ENV
echo "LAPIG_AUTHOR_EMAIL=$(git show -s --pretty=%ae)" >> $GITHUB_ENV
echo "LAPIG_AUTHOR_DATE=$(git log -1 --format=%cd --date=local)" >> $GITHUB_ENV
echo "LAPIG_PROJECT_NAME=$(git config --local remote.origin.url)" >> $GITHUB_ENV
echo "LAPIG_LAST_COMMIT=$(git show --summary | grep 'commit' | awk '{print $2}')" >> $GITHUB_ENV
echo "LAPIG_COMMIT_COMMENT=$(git log -1 --pretty=%B)" >> $GITHUB_ENV
echo "LAPIG_COMMIT_DATE=$(git show -s --format=%ci)" >> $GITHUB_ENV
echo "LAPIG_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV
- name: Use Git outputs
run: |
echo "Author Name: $LAPIG_AUTHOR_NAME"
echo "Author Email: $LAPIG_AUTHOR_EMAIL"
echo "Author Date: $LAPIG_AUTHOR_DATE"
echo "Project Name: $LAPIG_PROJECT_NAME"
echo "Last Commit: $LAPIG_LAST_COMMIT"
echo "Commit Comment: $LAPIG_COMMIT_COMMENT"
echo "Commit Date: $LAPIG_COMMIT_DATE"
echo "Branch Name: $LAPIG_BRANCH_NAME"
- name: Create version.json
run: echo '{"commitId":"$LAST_COMMIT_HASH"}' > version.json
- name: Determine branch
id: get_branch
run: echo "::set-output name=branch::${GITHUB_REF##*/}"
- name: Validate and Build
run: |
git pull origin main
docker build -t ${{secrets.REGISTRYPROD}}/$application_name:${LAST_COMMIT_HASH} -t ${{secrets.REGISTRYPROD}}/$application_name:latest -f docker/homologation/Dockerfile . --no-cache
- name: Login to Docker Registry
run: echo "${{ secrets.HARBOR_PASS }}" | docker login -u "${{ secrets.HARBOR_USER }}" --password-stdin "${{ secrets.URL_PRIVATE_REGISTRY }}"
- name: Push Image to Registry
run: |
docker push ${{secrets.REGISTRYPROD}}/$application_name:${LAST_COMMIT_HASH}
docker push ${{secrets.REGISTRYPROD}}/$application_name:latest
- name: Remove image locally
run: |
docker rmi ${{secrets.REGISTRYPROD}}/$application_name:${LAST_COMMIT_HASH}
docker rmi ${{secrets.REGISTRYPROD}}/$application_name:latest
- name: Add SSH key to known_hosts
run: |
mkdir -p ~/.ssh
ssh-keyscan -t rsa ${{secrets.SERVER_PROD_KNOWN}} >> ~/.ssh/known_hosts 2>/dev/null
chmod 644 ~/.ssh/known_hosts
echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
- name: Pull Image on Server
run: |
ssh ${{secrets.SERVER_PROD_SSH}} "docker pull ${{secrets.REGISTRYPROD}}/$application_name:latest"
- name: Deploy Container
run: |
delete_status=$(ssh ${{secrets.SERVER_PROD_SSH}} "curl -v -X DELETE -w "%{http_code}" http://${{secrets.SERVER_PROD}}/containers/$application_name?force=true 2>/dev/null")
echo "Delete Status: $delete_status"
create_status=$(ssh ${{secrets.SERVER_PROD_SSH}} "curl -v -X POST -H 'Content-Type: application/json' -d @${{secrets.CONFIG_FILES}}$application_name.json -s -o /dev/null -w "%{http_code}" http://${{secrets.SERVER_PROD}}/containers/create?name=$application_name 2>/dev/null")
echo "Create Status: $create_status"
echo "CREATE_STATUS=$create_status" >> $GITHUB_ENV
echo "DELETE_STATUS=$delete_status" >> $GITHUB_ENV
- name: Start Container
run: |
start_status=$(ssh ${{secrets.SERVER_PROD_SSH}} "curl -v -X POST -s -o /dev/null -w "%{http_code}" http://${{secrets.SERVER_PROD}}/containers/$application_name/start 2>/dev/null")
echo "Start Status: $start_status"
echo "START_STATUS=$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${TOKEN}/sendMessage" -d "chat_id=${CHAT_ID}" -d "parse_mode=markdown" -d "text=*Project*: *${LAPIG_PROJECT_NAME}*\n*Branch*: ${LAPIG_BRANCH_NAME}\n*Author*: *${LAPIG_AUTHOR_NAME}*\n*Author_Email*: *${LAPIG_AUTHOR_EMAIL}*\n*Commit_ID*: *${LAPIG_LAST_COMMIT}*\n*Message_Commit*: *${LAPIG_COMMIT_COMMENT}*\n*Date_Commit*: *${LAPIG_COMMIT_DATE}*\n*Duration*: *${LAPIG_DURATION}*'"
- name: Send message to Discord
run: |
if [[ "${{ job.status }}" == "success" && "$START_STATUS" == "204" && "$DELETE_STATUS" == "204" && "$CREATE_STATUS" == "201" ]]; then
echo "avatar_url='${{vars.LAPIG_SUCCESS}}'" >> $GITHUB_ENV
else
echo "avatar_url='${{vars.LAPIG_ERROR}}'" >> $GITHUB_ENV
fi
echo "discordDesc='Result: ${{ job.status }}\n Project: $LAPIG_PROJECT_NAME\n Commit: $LAPIG_LAST_COMMIT\n Author: $LAPIG_AUTHOR_NAME\n Author_Email: $LAPIG_AUTHOR_EMAIL\n Message: $LAPIG_COMMIT_COMMENT\n Date: $LAPIG_COMMIT_DATE\n Delete Status: $delete_status\n Create Status: $create_status\n Start Status: $start_status\n Duration: $LAPIG_DURATION seconds'" >> $GITHUB_ENV
echo "discordFooter='${{ github.workflow }} (#${{ github.run_number }})'" >> $GITHUB_ENV
echo "discordTitle='${{ github.workflow }} (build #${{ github.run_number }})'" >> $GITHUB_ENV
curl -H "Content-Type: application/json" -X POST -d "{ \"content\":\"$discordTitle\", \"embeds": [{"title":"$discordTitle","description":"$discordDesc", "footer":{"text":"$discordFooter"}}], "username":"GitHub Actions", "avatar_url":"$($avatar_url)" }' https://discord.com/api/webhooks/${{ secrets.DISCORD_KEY }}