-
Notifications
You must be signed in to change notification settings - Fork 0
186 lines (164 loc) · 5.6 KB
/
ci-cd-java.yml
File metadata and controls
186 lines (164 loc) · 5.6 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
name: ci-cd-java.yml
permissions:
contents: read
packages: read
on:
workflow_call:
inputs:
# it is required for backwards compatibility with CI/CD pipelines that have not been yet fully migrated to shared workflows
uploadJarArtifact:
required: false
type: boolean
default: false
performRelease:
required: false
type: boolean
default: false
workingDirectory:
required: false
type: string
default: .
imageName:
required: false
type: string
runTestsInsideDocker:
required: false
type: boolean
default: false
env:
IMAGE_NAME_MIXED_CASE: "${{ github.repository }}"
TEST_STAGE: test
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
- name: Setup JDK
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '25'
cache: 'maven'
- name: Check code format and lint
working-directory: ${{ inputs.workingDirectory }}
run: |
mvn spotless:check
- name: Run tests outside Docker
working-directory: ${{ inputs.workingDirectory }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
JAVA_TOOL_OPTIONS: ""
MAVEN_OPTS: ""
run: mvn -B test
- name: Run Integration tests (Testcontainers)
working-directory: ${{ inputs.workingDirectory }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DOCKER_HOST: unix:///var/run/docker.sock
TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE: /var/run/docker.sock
JAVA_TOOL_OPTIONS: ""
MAVEN_OPTS: ""
run: mvn -B verify
- 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: Build artifact
working-directory: ${{ inputs.workingDirectory }}
run: mvn package -Dmaven.test.skip
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload .jar artifact
if: ${{ inputs.uploadJarArtifact }}
uses: actions/upload-artifact@v4
with:
name: 'app.jar'
path: '/app/app.jar'
- name: Set Docker Image Name
run: |
OWNER="${GITHUB_REPOSITORY%%/*}"
if [[ -n "${{ inputs.imageName }}" ]]; then
IMAGE_NAME="${OWNER,,}/${{ inputs.imageName }}"
else
IMAGE_NAME="${GITHUB_REPOSITORY,,}"
fi
echo "IMAGE_NAME=${IMAGE_NAME}" >> "$GITHUB_ENV"
- name: Build & run tests inside Docker
if: ${{ inputs.runTestsInsideDocker }}
uses: docker/build-push-action@v6
with:
context: ${{ inputs.workingDirectory }}
load: true
target: "${{ env.TEST_STAGE }}"
tags: "${{ env.IMAGE_NAME }}:${{ env.TEST_STAGE }}"
secrets:
github_token=${{ secrets.GITHUB_TOKEN }}
build-args:
GITHUB_ACTOR=${{ github.actor }}
- name: Build Docker Image
uses: docker/build-push-action@v6
with:
context: ${{ inputs.workingDirectory }}
push: 'false'
tags: 'hsldevcom/${{ env.IMAGE_NAME }}:${{ github.sha }}'
secrets: |
github_token=${{ secrets.GITHUB_TOKEN }}
build-args:
GITHUB_ACTOR=${{ github.actor }}
- 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: 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: Setup Docker Buildx
if: ${{ env.PERFORM_RELEASE == 'true' }}
uses: docker/setup-buildx-action@v3
- 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: ${{ inputs.workingDirectory }}
push: ${{ env.PERFORM_RELEASE }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}