|
| 1 | +version: 2.1 |
| 2 | + |
| 3 | +# Define some constants |
| 4 | +working_dir_name: &workingDirName ~/workspace |
| 5 | +java_docker_image: &javaMavenDockerImage circleci/openjdk:11.0.3-stretch |
| 6 | + |
| 7 | +# Define some configuration |
| 8 | +working_dir: &workingDirectory |
| 9 | + working_directory: *workingDirName |
| 10 | +java_runtime: &javaRuntime |
| 11 | + docker: |
| 12 | + - image: *javaMavenDockerImage |
| 13 | + |
| 14 | +# Define some reusable parameters names and types |
| 15 | +docker_namespace_parameter_definition: &dockerNamespaceParameterDefinition |
| 16 | + namespace: |
| 17 | + type: string |
| 18 | +docker_repository_parameter_definition: &dockerRepositoryParameterDefinition |
| 19 | + repository: |
| 20 | + type: string |
| 21 | +docker_tag_parameter_definition: &dockerTagParameterDefinition |
| 22 | + tag: |
| 23 | + type: string |
| 24 | +docker_username_parameter_definition: &dockerUsernameParameterDefinition |
| 25 | + username: |
| 26 | + type: string |
| 27 | +docker_password_parameter_definition: &dockerPasswordParameterDefinition |
| 28 | + password: |
| 29 | + type: string |
| 30 | + |
| 31 | +# Composition of parameters |
| 32 | +docker_image_stuff_parameters: &dockerImageStuffParametersDefinitions |
| 33 | + <<: *dockerNamespaceParameterDefinition |
| 34 | + <<: *dockerRepositoryParameterDefinition |
| 35 | + <<: *dockerTagParameterDefinition |
| 36 | +docker_login_stuff_parameters: &dockerLoginStuffParametersDefinitions |
| 37 | + <<: *dockerUsernameParameterDefinition |
| 38 | + <<: *dockerPasswordParameterDefinition |
| 39 | + |
| 40 | + |
| 41 | +commands: |
| 42 | + retrieve_workspace: |
| 43 | + description: "Retrieves the working directory in the workspace" |
| 44 | + steps: |
| 45 | + - attach_workspace: |
| 46 | + at: *workingDirName |
| 47 | + |
| 48 | + save_workspace: |
| 49 | + description: "Saves the working directory as the workspace" |
| 50 | + steps: |
| 51 | + - persist_to_workspace: |
| 52 | + root: *workingDirName |
| 53 | + paths: |
| 54 | + - . |
| 55 | + |
| 56 | + retrieve_maven_local_repository_from_cache: |
| 57 | + description: "Retrieves the Maven's Local Repository from Cache, based on poms' checksum" |
| 58 | + steps: |
| 59 | + - run: |
| 60 | + name: Aggregating all project's pom files in a single file |
| 61 | + command: | |
| 62 | + rm -f .all-poms |
| 63 | + find . -type f -regex ".*/pom.xml" | sort -n | xargs cat >> .all-poms ; |
| 64 | + - restore_cache: |
| 65 | + # First try with the pom's aggregation checksum. If not available, get most recent |
| 66 | + keys: |
| 67 | + - cache-{{ .Environment.SERVICE_NAME }}-{{ .Environment.CACHE_VERSION }}-{{ checksum ".all-poms" }} |
| 68 | + - cache-{{ .Environment.SERVICE_NAME }}-{{ .Environment.CACHE_VERSION }}- |
| 69 | + |
| 70 | + generate_new_maven_local_repository: |
| 71 | + description: "Generates a new Maven local repository (when checksum is different only)" |
| 72 | + steps: |
| 73 | + - retrieve_maven_local_repository_from_cache |
| 74 | + - run: |
| 75 | + name: Downloading required dependencies and plugins |
| 76 | + command: mvn de.qaware.maven:go-offline-maven-plugin:resolve-dependencies -P ci -P docker-build |
| 77 | + - save_cache: |
| 78 | + key: cache-{{ .Environment.SERVICE_NAME }}-{{ .Environment.CACHE_VERSION }}-{{ checksum ".all-poms" }} |
| 79 | + paths: |
| 80 | + - ~/.m2 |
| 81 | + |
| 82 | + compile_code: |
| 83 | + description: "Compiles sources of the Maven's project" |
| 84 | + steps: |
| 85 | + - run: |
| 86 | + name: Compiling source code |
| 87 | + command: mvn clean compile -P ci -B -V |
| 88 | + |
| 89 | + test_code: |
| 90 | + description: "Performs tests using the Maven's test phase. Note that it will skip the compiler:compile goal" |
| 91 | + steps: |
| 92 | + - run: |
| 93 | + name: Running tests |
| 94 | + command: mvn test -P ci -B -DforkCount=0 -Dmaven.main.skip --fail-at-end |
| 95 | + |
| 96 | + save_test_results: |
| 97 | + description: "Aggregates the test results and stores them." |
| 98 | + steps: |
| 99 | + - run: |
| 100 | + name: Aggregating all project's tests results |
| 101 | + command: | |
| 102 | + mkdir -p ~/test-results/junit/ |
| 103 | + find . -type f -regex ".*/target/surefire-reports/.*xml" -exec cp {} ~/test-results/junit/ \; |
| 104 | + when: always |
| 105 | + - store_test_results: |
| 106 | + path: ~/test-results |
| 107 | + - store_artifacts: |
| 108 | + path: ~/test-results/junit |
| 109 | + |
| 110 | + docker_job: |
| 111 | + description: "Setups stuff for build and pushing a Docker image" |
| 112 | + steps: |
| 113 | + - retrieve_workspace |
| 114 | + - retrieve_maven_local_repository_from_cache |
| 115 | + - setup_remote_docker |
| 116 | + |
| 117 | + create_docker_image: |
| 118 | + description: "Creates a Docker Image using Maven" |
| 119 | + parameters: |
| 120 | + <<: *dockerImageStuffParametersDefinitions |
| 121 | + steps: |
| 122 | + - run: |
| 123 | + name: Creating Docker Image |
| 124 | + command: | |
| 125 | + mvn package -DskipTests -P ci -P docker-build -B \ |
| 126 | + -Ddocker.image.prefix=<<parameters.namespace>> \ |
| 127 | + -Ddocker.image.name=<<parameters.repository>> \ |
| 128 | + -Ddocker.image.tag=<<parameters.tag>> |
| 129 | +
|
| 130 | + push_docker_image: |
| 131 | + description: "Pushes the Docker Image to DockerHub." |
| 132 | + parameters: |
| 133 | + <<: *dockerImageStuffParametersDefinitions |
| 134 | + <<: *dockerLoginStuffParametersDefinitions |
| 135 | + steps: |
| 136 | + - run: |
| 137 | + name: Performing login to DockerHub |
| 138 | + command: echo <<parameters.password>> | docker login -u <<parameters.username>> --password-stdin |
| 139 | + - run: |
| 140 | + name: Pushing Docker Image |
| 141 | + command: | |
| 142 | + mvn dockerfile:push -DskipTests -P ci -P docker-build \ |
| 143 | + -Ddocker.image.prefix=<<parameters.namespace>> \ |
| 144 | + -Ddocker.image.name=<<parameters.repository>> \ |
| 145 | + -Ddocker.image.tag=<<parameters.tag>> |
| 146 | +
|
| 147 | + notify_docker_build_via_slack: |
| 148 | + description: "Sends a notification via Slack indicating that a Docker image has being built" |
| 149 | + parameters: |
| 150 | + <<: *dockerImageStuffParametersDefinitions |
| 151 | + webhook: |
| 152 | + type: string |
| 153 | + steps: |
| 154 | + - slack/notify: |
| 155 | + author_name: "CircleCI Robot" |
| 156 | + title: A new Docker image is available! |
| 157 | + title_link: "https://hub.docker.com/r/<<parameters.namespace>>/<<parameters.repository>>" |
| 158 | + message: > |
| 159 | + A new docker image for |
| 160 | + <<parameters.namespace>>/<<parameters.repository>> is available with tag <<parameters.tag>>.\n |
| 161 | + Check https://hub.docker.com/r/<<parameters.namespace>>/<<parameters.repository>> |
| 162 | + color: "#30cc30" |
| 163 | + include_visit_job_action: false |
| 164 | + include_project_field: false |
| 165 | + include_job_number_field: false |
| 166 | + webhook: <<parameters.webhook>> |
| 167 | + |
| 168 | + |
| 169 | +jobs: |
| 170 | + compile_and_test: |
| 171 | + # Apply working directory and docker image |
| 172 | + <<: *workingDirectory |
| 173 | + <<: *javaRuntime |
| 174 | + |
| 175 | + steps: |
| 176 | + - checkout |
| 177 | + - generate_new_maven_local_repository |
| 178 | + - compile_code |
| 179 | + - test_code |
| 180 | + - save_test_results |
| 181 | + - save_workspace |
| 182 | + |
| 183 | + build_and_push_snapshot_docker_image: |
| 184 | + # Apply working directory and docker image |
| 185 | + <<: *workingDirectory |
| 186 | + <<: *javaRuntime |
| 187 | + |
| 188 | + steps: |
| 189 | + - docker_job |
| 190 | + - create_docker_image: |
| 191 | + namespace: ${DOCKER_NAMESPACE} |
| 192 | + repository: ${SERVICE_NAME} |
| 193 | + tag: ${CIRCLE_SHA1} |
| 194 | + - push_docker_image: |
| 195 | + namespace: ${DOCKER_NAMESPACE} |
| 196 | + repository: ${SERVICE_NAME} |
| 197 | + tag: ${CIRCLE_SHA1} |
| 198 | + username: ${DOCKER_USERNAME} |
| 199 | + password: ${DOCKER_PASSWORD} |
| 200 | + - notify_docker_build_via_slack: |
| 201 | + namespace: ${DOCKER_NAMESPACE} |
| 202 | + repository: ${SERVICE_NAME} |
| 203 | + tag: ${CIRCLE_SHA1} |
| 204 | + webhook: ${SLACK_SNAPSHOT_BUILD_WEBHOOK} |
| 205 | + |
| 206 | + build_and_push_release_docker_image: |
| 207 | + # Apply working directory and docker image |
| 208 | + <<: *workingDirectory |
| 209 | + <<: *javaRuntime |
| 210 | + |
| 211 | + steps: |
| 212 | + - docker_job |
| 213 | + - create_docker_image: |
| 214 | + namespace: ${DOCKER_NAMESPACE} |
| 215 | + repository: ${SERVICE_NAME} |
| 216 | + tag: ${CIRCLE_TAG} |
| 217 | + - push_docker_image: |
| 218 | + namespace: ${DOCKER_NAMESPACE} |
| 219 | + repository: ${SERVICE_NAME} |
| 220 | + tag: ${CIRCLE_TAG} |
| 221 | + username: ${DOCKER_USERNAME} |
| 222 | + password: ${DOCKER_PASSWORD} |
| 223 | + - notify_docker_build_via_slack: |
| 224 | + namespace: ${DOCKER_NAMESPACE} |
| 225 | + repository: ${SERVICE_NAME} |
| 226 | + tag: ${CIRCLE_TAG} |
| 227 | + webhook: ${SLACK_RELEASE_BUILD_WEBHOOK} |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | +workflows: |
| 233 | + version: 2.1 |
| 234 | + snapshots: |
| 235 | + jobs: |
| 236 | + - compile_and_test |
| 237 | + - build_and_push_snapshot_docker_image: |
| 238 | + context: itbacep |
| 239 | + requires: |
| 240 | + - compile_and_test |
| 241 | + filters: |
| 242 | + branches: |
| 243 | + only: master |
| 244 | + |
| 245 | + releases: |
| 246 | + jobs: |
| 247 | + - compile_and_test: |
| 248 | + filters: |
| 249 | + tags: |
| 250 | + only: /.*/ |
| 251 | + branches: |
| 252 | + ignore: /.*/ |
| 253 | + - build_and_push_release_docker_image: |
| 254 | + context: itbacep |
| 255 | + requires: |
| 256 | + - compile_and_test |
| 257 | + filters: |
| 258 | + tags: |
| 259 | + only: /.*/ |
| 260 | + |
| 261 | +orbs: |
| 262 | + |
0 commit comments