-
Notifications
You must be signed in to change notification settings - Fork 0
168 lines (139 loc) · 6.23 KB
/
ci-cd-docker.yaml
File metadata and controls
168 lines (139 loc) · 6.23 KB
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
name: Docker Build and Run - CI/CD
on:
push:
branches: [ master ]
paths-ignore: [ README.md ]
workflow_dispatch:
env:
CI_REGISTRY_IMAGE: 'apiki'
CONTAINER_NAME: 'github-actions-runner'
jobs:
build:
if: "!contains(github.event.head_commit.message, 'skip ci')"
runs-on: self-hosted
steps:
- uses: actions/checkout@v4
- name: Load Centralized Configuration (Direct Git Clone with runner)
id: load-config
shell: bash
env:
CURRENT_REPO: ${{ github.repository }}
run: |
if [[ "${{ github.ref }}" == "refs/heads/apikidev" ]]; then
CTX="DEV"
elif [[ "${{ github.ref }}" == "refs/heads/apikistage" ]]; then
CTX="STAGE"
elif [[ "${{ github.ref }}" == "refs/heads/master" ]]; then
CTX="PROD"
elif [[ "${{ github.ref }}" == "refs/heads/homolog" ]]; then
CTX="HOMOLOG"
else
echo "::warning::Branch ${{ github.ref }} não mapeada. Pulando configuração."
exit 0
fi
echo "Ambiente detectado: $CTX"
TEMP_DIR=".centralized_vars_temp"
CONFIG_FILE_PATH="$TEMP_DIR/deploy-config.json"
rm -rf "$TEMP_DIR"
mkdir -p ~/.ssh
ssh-keyscan -H github.com >> ~/.ssh/known_hosts 2>/dev/null
git config --global --unset user.name || true
git config --global --unset user.email || true
git config --global user.name "${{ github.event.pusher.name || 'apiki-wp-care' }}"
git config --global user.email "${{ github.event.pusher.email || 'wpcare@apiki.com' }}"
git clone --depth 1 git@github.com:Apiki/centralized_variables.git "$TEMP_DIR"
if [ ! -f "$CONFIG_FILE_PATH" ]; then
echo "::error::Arquivo de configuração não encontrado após clone."
exit 1
fi
CONFIG=$(cat "$CONFIG_FILE_PATH")
echo "$CONFIG" | jq -r --arg repo "$CURRENT_REPO" --arg ctx "$CTX" \
'(.globals[$ctx] // {}) + (.repositories[$repo][$ctx] // {}) | to_entries | .[] | .key + "=" + .value' \
> env_vars.txt
echo "Limpando arquivos temporários..."
rm -rf "$TEMP_DIR"
if [ ! -s env_vars.txt ]; then
echo "::error::Nenhuma variável encontrada para $CURRENT_REPO ($CTX)."
exit 1
fi
cat env_vars.txt >> $GITHUB_ENV
rm env_vars.txt.1.0
- name: Define Production Variables
if: github.ref == 'refs/heads/master'
run: |
echo "SSH_USER=$SSH_USER_PROD" >> $GITHUB_ENV
echo "OWNER_USER=$OWNER_USER_PROD" >> $GITHUB_ENV
echo "OWNER_GROUP=$OWNER_GROUP_PROD" >> $GITHUB_ENV
echo "SSH_IP=$SSH_IP_PROD" >> $GITHUB_ENV
echo "SSH_PORT=$SSH_PORT_PROD" >> $GITHUB_ENV
echo "RUNNERS_PATH=$RUNNERS_PATH_PROD" >> $GITHUB_ENV
echo "RUNNER1_PATH=$RUNNER1_PATH_PROD" >> $GITHUB_ENV
echo "RUNNER2_PATH=$RUNNER2_PATH_PROD" >> $GITHUB_ENV
- name: Declare some variables
id: vars
shell: bash
run: |
BRANCH=$(echo ${GITHUB_REF#refs/heads/})
SHA_SHORT=$(git rev-parse --short HEAD)
echo "image_tag=$BRANCH-$SHA_SHORT" >> $GITHUB_OUTPUT
- name: Install SSH key
uses: shimataro/ssh-key-action@v2.4.0
with:
key: ${{ secrets.WP_CARE_KEY }}
name: id_rsa
known_hosts: unnecessary
config: StrictHostKeyChecking no
if_key_exists: replace
- name: Change docker-compose.yml in gh-runner-1
if: github.ref == 'refs/heads/master'
run: |
sed -i "s#\${CI_REGISTRY_IMAGE}#${{ env.CI_REGISTRY_IMAGE }}#" docker-compose.yaml
sed -i "s#\${CONTAINER_NAME}#${{ env.CONTAINER_NAME }}#" docker-compose.yaml
sed -i "s#\${CI_COMMIT_SHORT_SHA}#${{ steps.vars.outputs.image_tag }}#" docker-compose.yaml
working-directory: gh-runner-1
- name: Change docker-compose.yml in gh-runner-2
if: github.ref == 'refs/heads/master'
run: |
sed -i "s#\${CI_REGISTRY_IMAGE}#${{ env.CI_REGISTRY_IMAGE }}#" docker-compose.yaml
sed -i "s#\${CONTAINER_NAME}#${{ env.CONTAINER_NAME }}#" docker-compose.yaml
sed -i "s#\${CI_COMMIT_SHORT_SHA}#${{ steps.vars.outputs.image_tag }}#" docker-compose.yaml
working-directory: gh-runner-2
- name: Build Container
if: github.ref == 'refs/heads/master'
run: |
scp -P ${SSH_PORT} Dockerfile ${SSH_USER}@${SSH_IP}:${RUNNERS_PATH}
ssh -oStrictHostKeyChecking=no -p ${SSH_PORT} ${SSH_USER}@${SSH_IP} "sudo docker build -t ${{ env.CI_REGISTRY_IMAGE }}/${{ env.CONTAINER_NAME }}:${{ steps.vars.outputs.image_tag }} ${RUNNERS_PATH}/."
- name: Down Old Containers
if: github.ref == 'refs/heads/master'
run: |
ssh -oStrictHostKeyChecking=no -p ${SSH_PORT} ${SSH_USER}@${SSH_IP} "sudo docker compose -f ${RUNNER1_PATH}/docker-compose.yaml down"
ssh -oStrictHostKeyChecking=no -p ${SSH_PORT} ${SSH_USER}@${SSH_IP} "sudo docker compose -f ${RUNNER2_PATH}/docker-compose.yaml down"
- name: Simule rsync - MASTER
if: github.ref == 'refs/heads/master'
run: |
rsync -ahrvzKO --chown=$OWNER_USER:$OWNER_GROUP \
--dry-run --verbose --itemize-changes \
-e "ssh -p $SSH_PORT -oStrictHostKeyChecking=no" \
--chmod='D755,F644' \
./ $SSH_USER@$SSH_IP:$RUNNERS_PATH
rsync_status=$?
echo $rsync_status
if (($rsync_status != 0)); then
echo 'Ocorreu um erro na simulação!'
exit 1
else
echo '👽 Ocorreu tudo bem na simulação!'
fi
- name: Rsync - MASTER
if: github.ref == 'refs/heads/master'
run: |
rsync -ahrvzKO --chown=$OWNER_USER:$OWNER_GROUP \
-e "ssh -p $SSH_PORT -oStrictHostKeyChecking=no" \
--quiet --chmod='D755,F644' \
./ $SSH_USER@$SSH_IP:$RUNNERS_PATH
echo 'Tudo certo! 🍕'
- name: Deploy Containers
if: github.ref == 'refs/heads/master'
run: |
ssh -oStrictHostKeyChecking=no -p ${SSH_PORT} ${SSH_USER}@${SSH_IP} "sudo docker compose -f ${RUNNER1_PATH}/docker-compose.yaml up -d"
ssh -oStrictHostKeyChecking=no -p ${SSH_PORT} ${SSH_USER}@${SSH_IP} "sudo docker compose -f ${RUNNER2_PATH}/docker-compose.yaml up -d"