This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
219 lines (216 loc) · 9.49 KB
/
cicd.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
---
name: cicd
on: [push]
jobs:
build:
runs-on: ubuntu-20.04
outputs:
tag: ${{ steps.output.outputs.tag }}
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: update joj-elephant in poetry.lock
run: |
ELEPHANT_REF=$(git ls-remote --heads https://github.com/joint-online-judge/elephant.git refs/heads/master | awk '{print $1}')
echo $ELEPHANT_REF
ELEPHANT_URL_LINE_NO=$(awk '/joint-online-judge\/elephant.git/{ print NR; exit }' poetry.lock)
echo $ELEPHANT_URL_LINE_NO
ELEPHANT_REF_LINE_NO=$(($ELEPHANT_URL_LINE_NO-2))
echo $ELEPHANT_REF_LINE_NO
sed -i "${ELEPHANT_REF_LINE_NO}s/.*/resolved_reference = \"$ELEPHANT_REF\"/" poetry.lock
git --no-pager diff
- name: commit and push updated poetry.lock
uses: EndBug/add-and-commit@v9
with:
default_author: github_actions
message: 'chore: update joj-elephant in poetry.lock'
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
uses: docker/metadata-action@v3
id: meta
with:
images: ghcr.io/joint-online-judge/horse
flavor: |
latest=${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
- name: Set output
id: output
run: |
TAGS="${{ steps.meta.outputs.tags }}"
echo "$TAGS"
TAG="${TAGS%%$'\n'*}"
echo "$TAG"
echo "::set-output name=tag::$TAG"
- name: Build and push Docker image
id: docker_build
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Extract metadata (tags, labels) for Docker (PyTest)
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
uses: docker/metadata-action@v3
id: meta_pytest
with:
images: ghcr.io/joint-online-judge/horse
flavor: |
latest=false
- name: Build and push Docker image with PyTest
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
id: docker_build_pytest
uses: docker/build-push-action@v2
with:
context: .
push: true
build-args: |
PYTEST=1
tags: ghcr.io/joint-online-judge/horse:test
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Git setup
uses: fregante/setup-git-user@v1
- name: Generate openapi.json
run: |
mkdir -p build
docker run --rm ${{ steps.output.outputs.tag }} python3 -m joj.horse openapi --pretty > build/openapi.json
- name: Upload openapi.json as artifact
uses: actions/upload-artifact@v2
with:
name: openapi-json
path: build/openapi.json
- name: Git push openapi.json
if: success() && github.event_name == 'push' && github.ref == 'refs/heads/master'
uses: crazy-max/ghaction-github-pages@v2
with:
target_branch: openapi
build_dir: build
keep_history: true
commit_message: Generate openapi.json
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
test:
needs: build
runs-on: ubuntu-20.04
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Checkout joj-deploy-lite repository
uses: actions/checkout@v2
with:
repository: joint-online-judge/joj-deploy-lite
path: joj-deploy-lite
ref: master
- name: Start docker compose
env:
HORSE_IMAGE: ${{ needs.build.outputs.tag }}
run: |
echo $HORSE_IMAGE
export HORSE_TAG=${HORSE_IMAGE##*:}
echo $HORSE_TAG
cd joj-deploy-lite
bash ./start.sh lite
docker ps
- name: Test and Generate coverage report
if: success() && github.event_name == 'push' && github.ref == 'refs/heads/master'
run: |
docker exec joj2-horse /bin/bash -c "pytest -svv --cov=./ --cov-report=xml"
docker cp joj2-horse:/root/coverage.xml .
- name: Upload coverage to Codacy
uses: codacy/codacy-coverage-reporter-action@v1
with:
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
coverage-reports: coverage.xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
swagger-codegen:
if: github.event_name == 'push' && (github.ref == 'refs/heads/master')
needs: test
runs-on: ubuntu-20.04
env:
JAR_VERSION: 3.0.30
steps:
- uses: actions/checkout@v2
- uses: actions/download-artifact@v2
with:
name: openapi-json
path: build/openapi
- name: Set up OpenJDK 11
uses: actions/setup-java@v2
with:
distribution: adopt
java-version: '11'
check-latest: true
- name: Cache swagger-codegen
id: cache-swagger-codegen
uses: actions/cache@v2
with:
path: swagger-codegen-cli.jar
key: ${{ runner.os }}-swagger-codegen-${{ env.JAR_VERSION }}
- name: Download swagger-codegen
if: steps.cache-swagger-codegen.outputs.cache-hit != 'true'
run: |
wget https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/${{ env.JAR_VERSION }}/swagger-codegen-cli-${{ env.JAR_VERSION }}.jar -O swagger-codegen-cli.jar
- name: Create config file
run: |
echo '{"packageName": "joj.horse_client", "projectName": "horse-python-client", "library": "asyncio"}' > build/python-client.json
- name: Generate client
run: |
java -jar swagger-codegen-cli.jar generate \
-i build/openapi/openapi.json \
--git-user-id "joint-online-judge" \
--git-repo-id "horse-python-client" \
-l python \
-o build/client/python \
-c build/python-client.json
- name: Monkeypatch namespace package & requires
run: |
sed -i 's/find_packages/find_namespace_packages/g' build/client/python/setup.py
sed -i 's/dateutil\"]/dateutil\", \"aiohttp\"]/g' build/client/python/setup.py
sed -i '/REQUIRES.append(\"aiohttp\")/d' build/client/python/setup.py
- name: Find joj/horse_client
run: ls -lha build/client/python/
- name: Push to client repo
id: push_directory
uses: cpina/github-action-push-to-another-repository@main
env:
API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
BRANCH_NAME: ${{ github.ref == 'refs/heads/master' && 'master' }}
with:
source-directory: build/client/python
destination-github-username: joint-online-judge
destination-repository-name: horse-python-client
user-name: github-actions[bot]
user-email: 41898282+github-actions[bot]@users.noreply.github.com
commit-message: See ORIGIN_COMMIT from ${{ env.BRANCH_NAME }}
target-branch: ${{ env.BRANCH_NAME }}
remote-ssh-command:
if: github.event_name == 'push' && (github.ref == 'refs/heads/master')
needs: test
runs-on: ubuntu-20.04
steps:
- name: executing remote ssh commands using ssh key
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.KEY }}
port: ${{ secrets.PORT }}
script: |
cd ~/joj-deploy
git pull
./start.sh prod
docker image prune -f