-
Notifications
You must be signed in to change notification settings - Fork 0
212 lines (188 loc) · 6.7 KB
/
ci-cd-kotlin.yml
File metadata and controls
212 lines (188 loc) · 6.7 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
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
name: ci-cd-kotlin.yml
permissions:
contents: read
packages: read
on:
workflow_call:
inputs:
uploadJarArtifact:
required: false
type: boolean
default: false
jarArtifactName:
required: false
type: string
jarArtifactPath:
required: false
type: string
performRelease:
required: false
type: boolean
default: false
runTestsInsideDocker:
required: false
type: boolean
default: true
hasIntegrationTests:
required: false
type: boolean
default: false
env:
IMAGE_NAME_MIXED_CASE: "${{ github.repository }}"
jobs:
build-check-test-push:
name: Build, check, test, push
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
clean: 'true'
fetch-depth: 2
# Required since custom scripts from /scripts are being used
- name: Resolve shared workflow ref
run: |
set -euo pipefail
SHARED_WORKFLOW_REF=$(grep -roh \
'transitdata-shared-workflows/.github/workflows/[^@]*@[^ "'\'']*' \
"${GITHUB_WORKSPACE}/.github/workflows/" 2>/dev/null \
| sed 's/.*@//' | head -1 || true)
if [[ -z "${SHARED_WORKFLOW_REF}" ]]; then
echo "::warning::Could not detect shared workflow ref from caller workflows; falling back to main"
SHARED_WORKFLOW_REF="main"
fi
echo "Resolved shared workflow ref: ${SHARED_WORKFLOW_REF}"
echo "SHARED_WORKFLOW_REF=${SHARED_WORKFLOW_REF}" >> "$GITHUB_ENV"
- name: Checkout shared workflow scripts
uses: actions/checkout@v4
with:
repository: HSLdevcom/transitdata-shared-workflows
ref: ${{ env.SHARED_WORKFLOW_REF }}
path: .shared-workflows
- name: Setup JDK
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '11'
cache: 'gradle'
- name: Validate Java version consistency
env:
JAVA_TOOL_OPTIONS: ""
run: python3 "${GITHUB_WORKSPACE}/.shared-workflows/scripts/validate_java_version_consistency.py"
- name: Check code format and lint
run: ./gradlew spotlessCheck
env:
GITHUB_ACTOR: ${{ github.actor }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Run unit tests inside Docker
if: ${{ inputs.runTestsInsideDocker }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_ACTOR_ARG: ${{ github.actor }}
DOCKER_BUILDKIT: "1"
run: |
cat > /tmp/Dockerfile.test << DOCKERFILE
# syntax=docker/dockerfile:1
# check=error=true
FROM ${TEST_BASE_IMAGE}
WORKDIR /usr/app
ARG GITHUB_ACTOR=github-actions
COPY . .
RUN --mount=type=secret,id=github_token \
export GITHUB_TOKEN="\$(cat /run/secrets/github_token)" && \
export GITHUB_ACTOR="\$GITHUB_ACTOR" && \
./gradlew test --stacktrace --no-daemon
DOCKERFILE
docker build \
--secret id=github_token,env=GITHUB_TOKEN \
--build-arg "GITHUB_ACTOR=${GITHUB_ACTOR_ARG}" \
-f /tmp/Dockerfile.test \
.
- name: Run unit tests
if: ${{ inputs.hasIntegrationTests == false }}
env:
GITHUB_ACTOR: ${{ github.actor }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./gradlew test jacocoTestReport --stacktrace
- name: Run unit tests and integration tests
if: ${{ inputs.hasIntegrationTests }}
env:
GITHUB_ACTOR: ${{ github.actor }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./gradlew test integrationTest jacocoTestReport --stacktrace
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
verbose: true
- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
report_type: test_results
- name: Upload .jar artifact
if: ${{ inputs.uploadJarArtifact }}
uses: actions/upload-artifact@v4
with:
name: ${{ inputs.jarArtifactName }}
path: ${{ inputs.jarArtifactPath }}
- name: Build artifact
run: ./gradlew build -x test
env:
GITHUB_ACTOR: ${{ github.actor }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Lowercase Docker Image Name
run: |
echo "IMAGE_NAME=${IMAGE_NAME_MIXED_CASE,,}" >> "${GITHUB_ENV}"
- name: Build Docker Image
uses: docker/build-push-action@v6
with:
context: .
push: 'false'
tags: 'hsldevcom/${{ env.IMAGE_NAME }}:${{ github.sha }}'
- name: Check if perform release
id: perform_release
run: |
PERFORM_RELEASE=false
if [[ "${GITHUB_REF}" == "refs/heads/main" || "${GITHUB_REF}" == "refs/heads/develop" || "${GITHUB_REF}" == "refs/heads/aks-dev" ]]; then
PERFORM_RELEASE=true
elif [[ "${GITHUB_REF}" == refs/tags/* ]]; then
PERFORM_RELEASE=true
elif [[ "${{ inputs.performRelease }}" == "true" ]]; then
PERFORM_RELEASE=true
fi
echo "PERFORM_RELEASE=${PERFORM_RELEASE}" >> $GITHUB_ENV
echo "Perform release: ${PERFORM_RELEASE}"
- name: Setup Docker Buildx
if: env.PERFORM_RELEASE == 'true'
uses: docker/setup-buildx-action@v3
- name: Extract Docker metadata
if: env.PERFORM_RELEASE == 'true'
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=sha
type=semver,pattern={{version}}
labels: |
org.opencontainers.image.title=${{ env.IMAGE_NAME }}
org.opencontainers.image.vendor=hsldevcom
- name: Login to Docker Hub
if: env.PERFORM_RELEASE == 'true'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_INFODEVOPS_USERNAME }}
password: ${{ secrets.DOCKER_HUB_INFODEVOPS_TOKEN }}
- name: Build & Push Docker image
if: env.PERFORM_RELEASE == 'true'
uses: docker/build-push-action@v6
with:
context: .
push: ${{ env.PERFORM_RELEASE }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}