Build and test Java + Gradle applications with Wercker
Although my work record aspect is strong, I created the procedure for Wercker based on the actual work record. Since I am new to CI itself, there may be some mistakes. In that case, please point out.
Overview
This is the procedure for automatic build and automatic test with Wercker in the GitHub repository of Java application using Gradle.
I've tested public repositories here, but I might try private repositories and add results.
Wercker
URL
https://app.wercker.com/
Overview
Wercker, reading is worker. Recently acquired by Oracle. You can use the private repository for free. You can build a build environment using Docker.
table of contents
- Wercker sign-in
- Register your GitHub repository with Wercker
- Wercker.yml created, pushed to GitHub repository, automated build, automated test works
- Set wercker.yml to pass the build
- Confirm that if the JUnit test on Gradle fails, the build will also fail on Wercker.
- Force Wercker check on GitHub repository and reject merge if test NG
Wercker sign-in
- Access Wercker
- You can log in with your GitHub account
Register your GitHub repository with Wercker
- Click the Wercker Create your first application button
- Select User & SCM screen
- Select a user from the doropdown begin.*
--Wercker account is displayed, select the account to use
- Select SCM
--Supported by GitHub, Bitbucket, GitLab
――This article uses GitHub, so select GitHub
- Select Repository screen
- Select the GitHub repository to add to Wercker
--Only repositories with admin privileges are displayed (should, read the Wercker documentation, but point out if it's wrong)
- Setup SSH Key screen
- wercker will check out the code without using an SSH key
--Wercker checks out the target repository without an SSH key
--Select this because it is recommended
- Add the deploy key to the selected repository for me
--Register SSH key to access the repository
--Deprecated, probably because you don't easily pass repository operation privileges
- Review screen
- Make sure that the settings are as you selected
- Make my app public
--If checked, you will be able to publish the deployment information when you click the badge that appears later.
--Do not check if not needed
- Application addition on Wercker is completed
Create wercker.yml and push it to your GitHub repository
- From the repository registration completion screen, the yml addition screen is open as it is
- Select Java Gradle from the select box labeled Select a language
- The contents of wercker.yml will be displayed, so copy it to the clipboard.
- Create a file with the copied contents as a wercker.yml file directly under the root of the repository.
--Java version may need to be changed accordingly
--Changed from
box: openjdk: 8-jdk
to
box: openjdk: 10.0.1-jdk --box is synonymous with pull in Dockerfile -Refer to [openjdk official DockerHub](https://hub.docker.com/_/openjdk/) and described the `` `10.0.1-jdk
part.
- Push wercker.yml to the repository
- Catch the push and automatically run on Wercker
- Success or error
--In my work, I failed with an error during the build phase of wercker.
--There was a log that Gradle 4.2 was specified at the time of execution, but since Gradle 4.2 does not support Java 10, an error occurs.
--Since it was developed with Gradle 4.8, the Gradle version used is low.
- Explain the resolution of the error in the matter
Wercker default wercker.yml contents
# This references an OpenJDK container from the
# Docker Hub https://hub.docker.com/_/openjdk/
# Read more about containers on our dev center
# http://devcenter.wercker.com/docs/containers/index.html
box: openjdk:8-jdk
# This is the build pipeline. Pipelines are the core of wercker
# Read more about pipelines on our dev center
# http://devcenter.wercker.com/docs/pipelines/index.html
build:
# Steps make up the actions in your pipeline
# Read more about steps on our dev center:
# http://devcenter.wercker.com/docs/steps/index.html
steps:
# https://github.com/wercker/step-gradle
- java/gradle:
task: build
cache_project_cache: true
Set wercker.yml to allow the build to pass
Countermeasures
- If you set version: in the build item of wercker.yml, java / gradle, you should be able to make it any version.
- failed
--Can be specified in java / gradle of wercker.yml, so 4.8.0 was set
--The version referenced here is managed in the GitHub repository, but at this point it was only up to 4.2.
--As a result, Gradle 4.8 cannot be obtained and cannot be installed.
- If you use gradle wrapper instead of relying on java / gradle of wercker.yml, you should be able to execute it with any version.
--Changed wercker.yml according to Wercker Official Guide
--Since it is reflected in the final content, see below for details.
--Deleted the originally written build step item
--Run gradlew bootRun on dev item
--Run gradlew build on build item
- failed
--When Wercker enters the build phase,
Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain
occurs
- Add gradle-wrapper.jar to the repository so gradlew can run
--All .jars were uncommitted with .gitignore
--The class could not be found because there is no gradle-wrapper.jar in the gradle / wrapper directory directly under the root directory of the repository.
--Added
! Gradle-wrapper.jar
to .gitignore and added gradle-wrapper.jar to commit target
--It is also a necessary jar for those who do not install gradle and run it with gradlew, so it is okay to make it a repository management target
- Successful build on Wercker
Final wercker.yml
# This references an OpenJDK container from the
# Docker Hub https://hub.docker.com/_/openjdk/
# Read more about containers on our dev center
# http://devcenter.wercker.com/docs/containers/index.html
box: openjdk:10.0.1-jdk
# defining the dev pipeline
dev:
steps:
# A step that executes `gradle bootRun` command
- script:
name: run gradle
code: |
./gradlew bootRun
# Build definition
build:
# The steps that will be executed on build
steps:
# A step that executes `gradle build` command
- script:
name: run gradle
code: |
./gradlew --full-stacktrace -q --project-cache-dir=$WERCKER_CACHE_DIR build
Confirm that if the JUnit test on Gradle fails, the build will also fail on Wercker
- Dare to change the test code so that NG appears in the test class
- Commit and push the code containing the NG test to the GitHub repository
- Confirm that the build fails on Wercker
――I actually tried it and confirmed that the build phase failed.
Force Wercker check on GitHub repository and reject merge if test NG
- Open Settings in the GitHub repository
- Open Branches
- Select and open the master branch
- Check Require status checks to pass before merging
- Check the wercker / build item
- Subsequent pull requests to master cannot be merged without a successful Wercker build
Finally
So far, you can catch the push to GitHub on Wercker and check whether the build (test performed at the same time as the build) succeeds or fails automatically.
There are still many things that can be done by setting the workflow, so I plan to leave them as a separate article if I try again.
bonus
Wercker Developer Documentation
Mechanism of Wercker, how to make original box and step