-
Notifications
You must be signed in to change notification settings - Fork 0
178 lines (159 loc) · 6.85 KB
/
vtd-trigger.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
name: vtd trigger
on:
repository_dispatch:
types:
- vtd-trigger
- refs/remotes/**
env:
REF_NAME: ${{ github.event.client_payload.ref_name}}
TRIGGERING_ACTOR: ${{ github.event.client_payload.triggering_actor}}
jobs:
build-and-check:
name: ${{ matrix.name }}
timeout-minutes: 60
runs-on: ubuntu-latest
outputs:
env: ${{ steps.target-branch.outputs.value == 'main' && 'dev' || 'stg' }}
strategy:
matrix:
include: ${{ fromJson(vars.STRATEGY_MATRIX_SERVICES) }}
steps:
- name: Checkout source
uses: actions/checkout@v4
with:
repository: ${{ secrets.VTD_REPOSITORY_SOURCE }}
token: ${{ secrets.GH_PAT }}
ref: ${{ env.REF_NAME }}
path: source
- name: Check target source
id: target-branch
shell: bash
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
run: |
HASH_REF=$(git ls-remote --heads --quiet --exit-code https://${{ secrets.GH_PAT }}@github.com/${{ secrets.VTD_REPOSITORY_TARGET }}${{ matrix.service }}.git ${{ env.REF_NAME }} || echo '' )
if [ -z "${HASH_REF}" ]; then
echo "value=main" >> $GITHUB_OUTPUT
else
echo "value=${{ env.REF_NAME }}" >> $GITHUB_OUTPUT
fi
- name: Checkout target
uses: actions/checkout@v4
with:
repository: ${{ secrets.VTD_REPOSITORY_TARGET }}${{ matrix.service }}
token: ${{ secrets.GH_PAT }}
ref: ${{ steps.target-branch.outputs.value }}
path: target
- name: Clone properties file
uses: actions/checkout@v4
with:
repository: ${{ secrets.VTD_REPOSITORY_ENV }}
token: ${{ secrets.GH_PAT }}
path: env
- name: Connect to VPN
uses: Boostport/setup-cloudflare-warp@v1
with:
organization: tesoglobal
auth_client_id: ${{ secrets.CF_ACCESS_CLIENT_ID }}
auth_client_secret: ${{ secrets.CF_ACCESS_CLIENT_SECRET }}
- name: Switch profile
run: |
sed -i "s|spring.profiles.active=local|spring.profiles.active=ci|g" source/src/main/resources/application.properties
mv env/v4-java/application-ci.properties source/src/main/resources/application-ci.properties
- name: Switch branch
if: steps.target-branch.outputs.value == 'main'
working-directory: target
run: |
git branch --delete -D ${{ env.REF_NAME }} || echo 'done!!!'
git fetch --all --quiet
git switch -c ${{ env.REF_NAME }} || echo 'already!!!'
git reset --hard ${{ env.REF_NAME }}
git pull origin ${{ env.REF_NAME }} || echo 'done!!!'
- name: Setup java 21
uses: actions/setup-java@v4
with:
distribution: "temurin"
java-version: "21"
- name: Remove the rest code
run: |
mkdir code_temp
mv -v source/${{ secrets.SERVICE_PACKAGE_PATH }}/* code_temp
mv code_temp/main source/${{ secrets.SERVICE_PACKAGE_PATH }}/
mv code_temp/auth source/${{ secrets.SERVICE_PACKAGE_PATH }}/
mv code_temp/zoo source/${{ secrets.SERVICE_PACKAGE_PATH }}/
mv code_temp/Application.java source/${{ secrets.SERVICE_PACKAGE_PATH }}/
mv code_temp/ConfigProperties.java source/${{ secrets.SERVICE_PACKAGE_PATH }}/
mv code_temp/${{ matrix.service }} source/${{ secrets.SERVICE_PACKAGE_PATH }}/
- name: cleanup dbchange
run: |
if [ -d "source/src/main/resources/dbchange" ]; then
mkdir dbchange_temp
mv -v source/src/main/resources/dbchange/* dbchange_temp
mv dbchange_temp/${{ matrix.service }} source/src/main/resources/dbchange/
fi
- name: Remove the rest overlays
run: |
mkdir overlay_temp
mv -v source/overlays/* overlay_temp
mv overlay_temp/${{ matrix.service }} source/overlays/
- name: Change micro.service.context-path
working-directory: source
run: perl -i -pe "s/^micro.service.context-path=\/api/'micro.service.context-path=${{ matrix.path }}'/e" src/main/resources/application.properties
- name: Build and package project
working-directory: source
run: mvn -f pom.xml clean install package -DskipTests -q
- name: Move code to the result
run: |
mkdir result
mv target/.git result/
rm -rf source/.git
shopt -s dotglob
mv -v source/* result/
sed -i "s|spring.profiles.active=ci|spring.profiles.active=local|g" result/src/main/resources/application.properties
- name: Check changes
working-directory: result
id: is-change-code
run: |
if [[ `git status --porcelain` ]]; then
echo "value=true" >> $GITHUB_OUTPUT
else
echo "value=false" >> $GITHUB_OUTPUT
fi
- name: Push result
# if: steps.is-change-code.outputs.value == 'true'
working-directory: result
run: |
git config user.name "GitHub Actions" --local
git config user.email "41898282+github-actions[bot]@users.noreply.github.com" --local
git add -A
git diff-index --quiet HEAD || git commit -m ':white_check_mark: update code'
git push origin ${{ env.REF_NAME }} -f
- name: Trigger build ${{ matrix.service }}
# if: steps.is-change-code.outputs.value == 'true'
working-directory: result
run: |
TRIGGERING_ACTOR=${{ env.TRIGGERING_ACTOR }}
TRIGGERING_REF=${{ env.REF_NAME }}
TRIGGERING_SERVICE_NAME=${{ matrix.service }}
TRIGGERING_REPOSITORY=${{ secrets.VTD_REPOSITORY_TARGET }}${{ matrix.service }}
TRIGGERING_ENV=${{ steps.target-branch.outputs.value == 'main' && 'dev' || 'stg' }}
TRIGHERING_VERSION=$(git rev-parse HEAD)
TRIGHERING_HASH=$(git rev-parse HEAD)
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.LIBERTY_PPAP_PAT }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/VitaDairy/vtd-be-ppap/dispatches \
-d "{ \
\"event_type\": \"vtd-be-java-${{ matrix.service }}/refs/remotes/$TRIGGERING_REF~$TRIGGERING_ACTOR\", \
\"client_payload\": { \
\"triggering_actor\": \"$TRIGGERING_ACTOR\", \
\"triggering_ref\": \"$TRIGGERING_REF\", \
\"triggering_service_name\": \"$TRIGGERING_SERVICE_NAME\", \
\"triggering_repo\": \"$TRIGGERING_REPOSITORY\", \
\"triggering_env\": \"$TRIGGERING_ENV\", \
\"triggering_version\": \"$TRIGHERING_VERSION\", \
\"triggering_hash\": \"$TRIGHERING_HASH\" \
} \
}"