-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy path.gitlab-ci.yml
More file actions
171 lines (162 loc) · 4.69 KB
/
.gitlab-ci.yml
File metadata and controls
171 lines (162 loc) · 4.69 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
variables:
DOCKER_REGISTRY: "registry.gitlab.com"
DOCKER_IMAGE_NAME: "${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA}"
MAVEN_OPTS: >-
-Dhttps.protocols=TLSv1.2
-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository
-Dorg.slf4j.simpleLogger.showDateTime=true
-Djava.awt.headless=true
-Dmaven.install.skip=true
MAVEN_CLI_OPTS: >-
--batch-mode
--errors
--fail-at-end
--show-version
--no-transfer-progress
-DinstallAtEnd=true
-DdeployAtEnd=true
BUILD_IMAGE: maven:3.8-openjdk-8
stages:
- .pre
- build
- test
- package
# Detect Java version from pom.xml
detect-java-version:
image: alpine:latest
stage: .pre
script:
- apk add --no-cache grep
- |
if grep -q "<java.version>17</java.version>\|<maven.compiler.source>17</maven.compiler.source>\|<maven.compiler.target>17</maven.compiler.target>" pom.xml; then
echo "BUILD_IMAGE=maven:3.8-openjdk-17" > build.env
echo "Detected Java 17 in pom.xml - Post-transformation"
else
echo "BUILD_IMAGE=maven:3.8-openjdk-8" > build.env
echo "Detected Java 8 in pom.xml - Pre-transformation"
fi
- cat build.env
artifacts:
reports:
dotenv: build.env
expire_in: 1 hour
# Install dependency in .pre stage
install-dependency:
image: $BUILD_IMAGE
stage: .pre
needs:
- detect-java-version
script:
- mvn install:install-file
-Dfile=./movie-service-utils/built-library/0_1_0/movie-service-utils-0.1.0.jar
-DgroupId=com.amazonaws.samples
-DartifactId=movie-service-utils
-Dversion=0.1.0
-Dpackaging=jar
cache:
paths:
- .m2/repository/
key: ${CI_COMMIT_REF_SLUG}
artifacts:
paths:
- .m2/repository/org/amazonaws/samples/
expire_in: 1 week
# Build stage
build:
image: $BUILD_IMAGE
stage: build
needs:
- detect-java-version
- install-dependency
script:
- echo "Building the application..."
- mvn $MAVEN_CLI_OPTS clean compile
cache:
paths:
- .m2/repository/
key: ${CI_COMMIT_REF_SLUG}
artifacts:
paths:
- target/
expire_in: 1 week
# Test stage
test:
image: $BUILD_IMAGE
stage: test
needs:
- detect-java-version
- build
script:
- echo "Running tests..."
- mvn $MAVEN_CLI_OPTS test
cache:
paths:
- .m2/repository/
key: ${CI_COMMIT_REF_SLUG}
artifacts:
reports:
junit: target/surefire-reports/*.xml
expire_in: 1 week
# Package stage with Kaniko
package:
stage: package
needs:
- detect-java-version
- test
- install-dependency
- build
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
script:
- mkdir -p /kaniko/.docker
- echo "{\"auths\":{\"$DOCKER_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
- |
if grep -q "<java.version>17</java.version>\|<maven.compiler.source>17</maven.compiler.source>\|<maven.compiler.target>17</maven.compiler.target>" pom.xml; then
echo "Detected Java 17 in pom.xml - Generating Dockerfile for Java 17"
sed 's/maven:3.6.1-amazoncorretto-8/maven:3.8-openjdk-17/g' Dockerfile > Dockerfile.dynamic
else
echo "Detected Java 8 in pom.xml - Using original Dockerfile for Java 8"
cp Dockerfile Dockerfile.dynamic
fi
- /kaniko/executor
--context "${CI_PROJECT_DIR}"
--dockerfile "${CI_PROJECT_DIR}/Dockerfile.dynamic"
--destination "${DOCKER_IMAGE_NAME}"
dependencies:
- install-dependency
- build
cache:
paths:
- .m2/repository/
key: ${CI_COMMIT_REF_SLUG}
# Q Code Transformation job - Required by Q Developer (only for pre-transformation)
q-code-transformation:
image: $BUILD_IMAGE
stage: build
needs:
- detect-java-version
- install-dependency
script:
- |
if grep -q "<java.version>17</java.version>\|<maven.compiler.source>17</maven.compiler.source>\|<maven.compiler.target>17</maven.compiler.target>" pom.xml; then
echo "Post-transformation detected - skipping dependency copy"
mvn $MAVEN_CLI_OPTS verify
else
echo "Pre-transformation detected - running full Q transformation"
mvn $MAVEN_CLI_OPTS verify
mvn $MAVEN_CLI_OPTS dependency:copy-dependencies -DoutputDirectory=dependencies -Dmdep.useRepositoryLayout=true -Dmdep.copyPom=true -Dmdep.addParentPoms=true
fi
cache:
paths:
- .m2/repository/
key: ${CI_COMMIT_REF_SLUG}
artifacts:
name: q-code-transformation-dependencies
paths:
- dependencies/*
when: on_success
rules:
- if: $CI_COMMIT_REF_NAME =~ /^q\/transform-/ && $CI_PIPELINE_SOURCE == 'push' && $BUILD_IMAGE !~ /openjdk-17/
when: always
- when: never