LoginSignup
8
6

More than 5 years have passed since last update.

pipelineでDockerを使おう

Posted at

JenkinsfileでDockerを使う

Docker Pipeline Pluginを使う
https://go.cloudbees.com/docs/cloudbees-documentation/cje-user-guide/index.html#docker-workflow

:arrow_double_down: Dockerイメージの取得方法

ローカルにレジストリ等を立ち上げている場合は下記のような形で良いです

image = docker.image('127.0.0.1:5000/hoge/fuga:latest')
image.pull()

DockerHub等にあるものは下記のように、URL指定などを省略できます

image = docker.image('maven:3.3.3-jdk-8')
image.pull()

Dockerfileをgit等に突っ込んでる場合は下記のように build を使うと良いです
buildはカレントディレクトリのDockerfileを対象としビルドしてくれます

checkout scm
image = docker.build('fuga:moge')

:warning: Dockerビルドやイメージ取得に失敗した場合

Try, Catchで囲んでしまえばよしなにできます

def image = null
try{
  image = docker.image('maven:3.3.3-jdk-8')
}catch(err){
  // error ...
}

:thumbsup: Dockerイメージ内での処理記述

insideを使うことで、Docker内での処理を記述できます
ほぼ公式のサンプルままですが下記のように書きます

image.inside {
  git '…your-sources…'
  sh 'mvn -B clean install'
}

つなげることもできます

docker.image('maven:3.3.3-jdk-8').inside {
}

当然ですが、inside内で記述する内容は、jenkinsの環境ではなくdocker環境で実行されます

image.inside {
  // Docker上で `sudo apt-get -y install wget` が必要
  sh "wget -q うにゃほにゃ"
}

gradleを使うんだったら以下のように権限追加が必要だったりします

image.inside {
  sh "chmod +x ./gradlew"
  sh "./gradlew assembleRelease --stacktrace"
}
8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6