I couldn't find a sample that explained the automation of development / build with Java (Maven) with CircleCI, so I tried it.
I will not explain about having a GitHub or CircleCI account, installing and setting VS Code or git, etc. in this article. Regarding GitHub account settings and CircleCI account settings, there is an article I wrote earlier, so I will list it here.
-Sign up on GitHub --New user registration -Sign up to CircleCI-New user registration
I wrote
-[Create a Java (Maven) project with VS Code and develop it on a Docker container] (https://qiita.com/mfunaki/items/dc09764cb08bb787c261)
Let's use CircleCI to automate the build of the project mentioned in.
Using a web browser, click Go to App
at the top right of CircleCI's Home Page (https://circleci.com/ja/) or access https://app.circleci.com/ directly.
On the screen called Project, the Set Up Project button is displayed in the repository that is not registered as a CircleCI project even if it exists on GitHub, so the project being created this time (CCI-SMP-Java-Maven) Click the Set Up Project button next to it.
Then, the recommended configuration file (config.yml) template will be displayed. This time, instead of selecting ** Maven (Java) ** and clicking the ** Commit and Run ** button, ** ↓ Click the button (Download) **.
If your web browser asks where to save config.yml, create a .circleci
directory in your local CCI-SMP-Java-Maven repository and store config.yml
in it. I will. If you don't hear it, it's probably saved automatically in your download folder, so create a .circleci
directory in your local CCI-SMP-Java-Maven repository and put it in your download folder. Make sure to copy or move the config.yml
file in.
When the change notification (U) of the config.yml
file is displayed on Visual Studio Code, add, commit and push it.
After adding, committing and pushing, go back to your web browser and click the ** Use Existing Config ** button. You will be asked Have you added a config.yml file ?, but you can click the ** Start Building ** button.
Then, the CI/CD process starts, but the specified user setting file does not exist: /home/circleci/project/pom.xml in Install Dependencies, so the pom.xml
file used by Maven cannot be found. You can see that he is angry.
Looking at the Visual Studio Code Explorer, the pom.xml
file exists, but not in the top-level directory, but in the my-app
directory.
Therefore, it seems good to specify this location.
If you look at the 4th line of the config.yml
file you added earlier, you can see that you are using maven Orb, so search for maven Orb from the list of CircleCI Orbs here (https://circleci.com/developer/ja/orbs). ..
Clicking circleci/[email protected] (version number may vary with new version release) will bring up the maven Orb manual, so the parameters in the maven/test job * Specifying * app_src_directory ** seems to work.
At the same time, set the version of circleci/maven on the 4th line to 1.0.3.
config.yml
version: 2.1
orbs:
maven: circleci/[email protected]
workflows:
maven_test:
jobs:
- maven/test: # checkout, build, test, and upload test results
app_src_directory: my-app
I updated it, committed and pushed it and it worked! From the CCI-SMP-Java-Maven project, select the maven_test workflow and then the maven/test job and you should see something like this. At first glance, everything seems to work on the green, but there are (at least) two things that don't seem to work.
/home/circleci/project/my-app/target/my-app-1.0-SNAPSHOT.jar
file.Part 2 isn't just about going through the build (in green), it's getting the generated * .jar
file so you can see the test results report.
First of all, the test result, but it seems to work if you can specify the storage destination of the test result correctly. Maven Orb Manual In the middle of ** Usage Examples **, ** custom_test_results_path ** is If your tests results are not in the default (target/surefire-reports) directory then you could add Since it says a custom directory., I will add the description of test_results_path
in addition to app_src_directory
to the parameter of maven/test
in config.yml
.
config.yml
version: 2.1
orbs:
maven: circleci/[email protected]
workflows:
maven_test:
jobs:
- maven/test: # checkout, build, test, and upload test results
app_src_directory: my-app
test_results_path: my-app/target/surefire-reports
When I update, commit, and push this config.yml
to see the screen on the CircleCI side, ** 1 ** in white is added to the TETS tab in green, and in ** Uploading test results ** The execution result is also output as Archiving the following test results.
Click the TESTS tab and the test results will be displayed like this.
Using Maven Orb makes it easier to store test results (test_results_path
), but it cannot be used in combination with saving artifacts (store_artifacts
), so config.yml
is stored without Maven Orb. Describe. That said, the Maven Orb source code is published here here, so it's very helpful to be able to refer to it.
config.yml
version: 2
jobs:
build:
docker:
- image: cimg/openjdk:15.0.1
steps:
- checkout
- run:
command: find . -name 'pom.xml' | sort | xargs cat > /tmp/maven_cache_seed
working_directory: ~/project/my-app
- restore_cache:
key: circleci-java-maven-{{ checksum "/tmp/maven_cache_seed" }}
- run:
command: mvn package
working_directory: ~/project/my-app
- save_cache:
paths:
- ~/.m2/repository
key: circleci-java-maven-{{ checksum "/tmp/maven_cache_seed" }}
- store_test_results:
path: ~/project/my-app/target/surefire-reports
- store_artifacts:
path: ~/project/my-app/target/my-app-1.0-SNAPSHOT.jar
Now, I will explain it in several parts.
In the previous article, I used a Microsoft-made image (mcr.microsoft.com/vscode/devcontainers/java:0-15), but here I used a CircleCI-made convenience image optimized for use with CircleCI (CircleCI-made convenience image). I am using cimg/openjdk: 15.0.1). A list of convenience images can be found at here.
config.yml
build:
docker:
- image: cimg/openjdk:15.0.1
I saw a lot of libraries downloaded during the build process of the above thing. The library to be downloaded is specified in pom.xml
along with the library name and version, and is saved in the .m2/repository
directory.
In other words, if the contents of the pom.xml
file have not been changed, using the contents of .m2/repository
will reduce the download burden. Here, the contents of the pom.xml
file (output by cat
) are acquired in / tmp/maven_cache_seed
.
config.yml
- run:
command: find . -name 'pom.xml' | sort | xargs cat > /tmp/maven_cache_seed
working_directory: ~/project/my-app
- restore_cache:
key: circleci-java-maven-{{ checksum "/tmp/maven_cache_seed" }}
(Omission)
- save_cache:
paths:
- ~/.m2/repository
key: circleci-java-maven-{{ checksum "/tmp/maven_cache_seed" }}
Even if you don't do this roundabout way, you can put circleci-java-maven-{{checksum" ~/project/my-app/pom.xml "}}
in the key of ** restore_cache ** or ** save_cache **. It seems like you can pass it, but the method implemented here will result in an error in this ** restore_cache ** step if pom.xml
does not exist (following steps). Is not executed), so I think you are doing it this way (the source of the method is the Maven Orb source code).
This is simple. As a reminder, you can't use the $ CIRCLE_WORKING_DIRECTORY
environment variable in working_directory
(more precisely in config.yml
). For more information, see the description of working_directory
in Documentation (I'm addicted to this, but when I saw that literally" $ CIRCLE_WORKING_DIRECTORY "was created at the SSH login destination, I was able to notice it right away!).
config.yml
- run:
command: mvn package
working_directory: ~/project/my-app
This is also simple.
config.yml
- store_test_results:
path: ~/project/my-app/target/surefire-reports
This is also simple.
config.yml
- store_artifacts:
path: ~/project/my-app/target/my-app-1.0-SNAPSHOT.jar
The JAR file saved in the artifact in this way can be obtained from the ARTIFACTS tab on the CircleCI job screen.
I think that there are some points that were difficult to understand due to redundant or insufficient explanation, but if you go through it, if it is a Java (Maven) project, it may be possible to automate it by putting it on CircleCI, whether it is new or existing. Isn't it? I think you got the feeling of skin.
I would like to introduce other languages (and build tools) like this. See you again!
Recommended Posts