JenkinsのビルドをDockerコンテナで実行する
やること
Spring Bootで作ったアプリケーションのビルド&テストを、Jenkinsを使ってDockerコンテナ内で実行する。
方法
- コンテナのイメージはopenjdk:8-jdk-alpineを使った。
- Jenkinsfileの書き方は、agentにanyを指定するか、dockerを指定するかでコンテナ内で実行するかしないかがかわる。
- mavenをコンテナに入れるの面倒なのでSpring Initlizr で作った時から入っている、mvnwを使う。
- Junitの結果のグラフがなぜか表示されなかったけど、Wikiのコメントに回避策が書いてあった。一応動いた。 https://wiki.jenkins.io/display/JENKINS/JUnit+graph
- maven-surefire-pluginのバージョンは2.20にしないとテストの実行が失敗する。
以下のようなJenkinsfileを作る。
pipeline { agent { docker { image 'openjdk:8-jdk-alpine' } } stages { stage('Checkout') { steps { checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://192.168.56.6:8081/gitbucket/git/root/dockertest.git']]]) } } stage('Build') { steps { sh './mvnw clean compile' } } stage('Test') { steps { sh './mvnw test' } } stage('Recoding Result') { steps { //junit '**/surefire-reports/*.xml' step([$class: 'JUnitResultArchiver', testResults: '**/surefire-reports/*.xml' ]) } } } }
実行結果
実行するとこんな感じ。
Started by user admin Obtained Jenkinsfile from git http://192.168.56.6:8081/gitbucket/git/root/dockertest.git Running in Durability level: MAX_SURVIVABILITY [Pipeline] node Running on Jenkins in /var/lib/jenkins/workspace/dockertest-docker [Pipeline] { [Pipeline] stage [Pipeline] { (Declarative: Checkout SCM) [Pipeline] checkout > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url http://192.168.56.6:8081/gitbucket/git/root/dockertest.git # timeout=10 Fetching upstream changes from http://192.168.56.6:8081/gitbucket/git/root/dockertest.git > git --version # timeout=10 > git fetch --tags --progress http://192.168.56.6:8081/gitbucket/git/root/dockertest.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision f97c7cff726cad83d24b041e4f44be49b479e536 (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f f97c7cff726cad83d24b041e4f44be49b479e536 Commit message: "Change stage name." > git rev-list --no-walk cbf8bf399f9563d46fe2cae73eba0ea99cd4457d # timeout=10 [Pipeline] } [Pipeline] // stage [Pipeline] withEnv [Pipeline] { [Pipeline] sh [dockertest-docker] Running shell script + docker inspect -f . openjdk:8-jdk-alpine . [Pipeline] withDockerContainer Jenkins does not seem to be running inside a container $ docker run -t -d -u 985:979 -w /var/lib/jenkins/workspace/dockertest-docker -v /var/lib/jenkins/workspace/dockertest-docker:/var/lib/jenkins/workspace/dockertest-docker:rw,z -v /var/lib/jenkins/workspace/dockertest-docker@tmp:/var/lib/jenkins/workspace/dockertest-docker@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** openjdk:8-jdk-alpine cat $ docker top 364938fc3805b25ed0bc7097881d2a4fb592d7799e6afe694fec2d4b4481f74e -eo pid,comm [Pipeline] { [Pipeline] stage [Pipeline] { (Checkout) [Pipeline] checkout > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url http://192.168.56.6:8081/gitbucket/git/root/dockertest.git # timeout=10 Fetching upstream changes from http://192.168.56.6:8081/gitbucket/git/root/dockertest.git > git --version # timeout=10 > git fetch --tags --progress http://192.168.56.6:8081/gitbucket/git/root/dockertest.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision f97c7cff726cad83d24b041e4f44be49b479e536 (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f f97c7cff726cad83d24b041e4f44be49b479e536 Commit message: "Change stage name." [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Build) [Pipeline] sh [dockertest-docker] Running shell script + ./mvnw clean compile /var/lib/jenkins/workspace/dockertest-docker [INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.example:dockertest >----------------------- [INFO] Building dockertest 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ dockertest --- [INFO] Deleting /var/lib/jenkins/workspace/dockertest-docker/target [INFO] [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ dockertest --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ dockertest --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 2 source files to /var/lib/jenkins/workspace/dockertest-docker/target/classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.787 s [INFO] Finished at: 2018-08-26T11:35:58Z [INFO] ------------------------------------------------------------------------ [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Test) [Pipeline] sh [dockertest-docker] Running shell script + ./mvnw test /var/lib/jenkins/workspace/dockertest-docker [INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.example:dockertest >----------------------- [INFO] Building dockertest 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ dockertest --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ dockertest --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ dockertest --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/dockertest-docker/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ dockertest --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 2 source files to /var/lib/jenkins/workspace/dockertest-docker/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.20:test (default-test) @ dockertest --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.example.dockertest.web.HelloControllerTest 〜長いので省略〜 [INFO] [INFO] Results: [INFO] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 9.830 s [INFO] Finished at: 2018-08-26T11:36:10Z [INFO] ------------------------------------------------------------------------ [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Recoding Result) [Pipeline] step Recording test results [Pipeline] } [Pipeline] // stage [Pipeline] } $ docker stop --time=1 364938fc3805b25ed0bc7097881d2a4fb592d7799e6afe694fec2d4b4481f74e $ docker rm -f 364938fc3805b25ed0bc7097881d2a4fb592d7799e6afe694fec2d4b4481f74e [Pipeline] // withDockerContainer [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS