Skip to content

Commit 45bde90

Browse files
authored
Integrate with CircleCI
Integrate with CircleCI for CI/CD workflows
1 parent 3e89a1b commit 45bde90

File tree

5 files changed

+309
-23
lines changed

5 files changed

+309
-23
lines changed

.circleci/config.yml

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
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+
slack: circleci/[email protected]

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
language: java
2-
install: mvn clean install -DskipTests=true -P travis-ci -B -V
3-
script: mvn test -P travis-ci -B
2+
install: mvn clean install -DskipTests=true -P ci -B -V
3+
script: mvn test -P ci -B
44
cache:
55
directories:
6-
- $HOME/.m2
6+
- "$HOME/.m2"
77

88
jdk:
99
- openjdk11

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
# Evaluations Service [![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0) [![Build Status](https://travis-ci.org/coding-eval-platform/evaluations-service.svg?branch=master)](https://travis-ci.org/coding-eval-platform/evaluations-service)
2+
# Evaluations Service [![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0) [![Build Status](https://img.shields.io/circleci/project/github/coding-eval-platform/evaluations-service/master.svg)](https://circleci.com/gh/coding-eval-platform/evaluations-service/tree/master) ![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/coding-eval-platform/evaluations-service.svg)
33

44
Service in charge of evaluations management
55

@@ -284,6 +284,21 @@ $ docker run -p 8000:8000 itbacep/eval-service:$EVAL_SERVICE_VERSION
284284
Note that you will have to link the container with another container (or the host machine)
285285
in which a PostgreSQL server is running.
286286
287+
## CI/CD Workflow
288+
289+
This project is integrated with [CircleCI](https://circleci.com/).
290+
291+
### Pull requests
292+
293+
When a pull request is created, a build will be triggered in CircleCI, which must succeed in order to merge the pull request. This build will just **compile the source code and run tests**.
294+
Note that if still committing to a branch with an open pull request, each push to the said branch will trigger a build.
295+
296+
### Pushes and merges into master
297+
Pushing or merging into ```master``` will also trigger the **compile** and **test** build in CircleCI. If the build succeeds, this will be followed by a Docker phase: it will build a Docker image and push it into DockerHub. This images will be tagged with the commit's hash.
298+
299+
### Releases
300+
A release is performed by tagging in git. Pushing a tag will also trigger the **compile** and **test** build in CircleCI. If the build succeeds, this will be followed by a Docker phase: it will build a Docker image and push it into DockerHub. This images will be tagged with the git's tag.
301+
287302
288303
## License
289304

evaluations-service-application/pom.xml

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020

2121
<!-- Bootstrap class -->
2222
<bootstrapClass>ar.edu.itba.cep.evaluations_service.application.Application</bootstrapClass>
23-
24-
<!-- Docker -->
25-
<docker.image.prefix>itbacep</docker.image.prefix>
26-
<docker.image.name>${project.parent.artifactId}</docker.image.name>
2723
</properties>
2824

2925
<dependencies>
@@ -67,11 +63,35 @@
6763
<profiles>
6864
<profile>
6965
<id>docker-build</id>
66+
<properties>
67+
<!-- Timestamp format to be included in Docker image's tag -->
68+
<maven.build.timestamp.format>yyyy-MM-dd'T'HH-mm-ss</maven.build.timestamp.format>
69+
<!-- Docker Image name -->
70+
<docker.image.prefix>itbacep</docker.image.prefix>
71+
<docker.image.name>${project.parent.artifactId}</docker.image.name>
72+
<docker.image.tag-id>${maven.build.timestamp}</docker.image.tag-id>
73+
<docker.image.tag>${project.parent.version}-${docker.image.tag-id}</docker.image.tag>
74+
</properties>
7075
<build>
7176
<plugins>
7277
<plugin>
7378
<groupId>com.spotify</groupId>
7479
<artifactId>dockerfile-maven-plugin</artifactId>
80+
<executions>
81+
<execution>
82+
<id>default-dockerfile</id>
83+
<goals>
84+
<goal>build</goal>
85+
<goal>tag</goal>
86+
<goal>push</goal>
87+
</goals>
88+
</execution>
89+
</executions>
90+
<configuration>
91+
<repository>${docker.image.prefix}/${docker.image.name}</repository>
92+
<tag>${docker.image.tag}</tag>
93+
<skip>false</skip>
94+
</configuration>
7595
</plugin>
7696
</plugins>
7797
</build>
@@ -84,27 +104,14 @@
84104
<plugin>
85105
<groupId>com.spotify</groupId>
86106
<artifactId>dockerfile-maven-plugin</artifactId>
87-
<executions>
88-
<execution>
89-
<id>default-dockerfile</id>
90-
<goals>
91-
<goal>build</goal>
92-
<goal>tag</goal>
93-
<goal>push</goal>
94-
</goals>
95-
</execution>
96-
</executions>
97107
<configuration>
98-
<repository>${docker.image.prefix}/${docker.image.name}</repository>
99-
<tag>${project.parent.version}</tag>
100108
<buildArgs>
101109
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
102110
</buildArgs>
103111
</configuration>
104112
</plugin>
105113
</plugins>
106114
</pluginManagement>
107-
108115
<plugins>
109116
<plugin>
110117
<groupId>org.springframework.boot</groupId>

0 commit comments

Comments
 (0)