Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Docker functions |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
Jakub Josef | 1ef81e6 | 2017-04-03 19:14:54 +0200 | [diff] [blame] | 10 | * Build step to build docker image. |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 11 | * |
| 12 | * @param img Image name |
| 13 | * @param baseImg Base image to use (can be empty) |
| 14 | * @param dockerFile Dockerfile to use |
| 15 | * @param timestamp Image tag |
chnyda | 16535ab | 2017-05-25 16:28:28 +0200 | [diff] [blame] | 16 | * @param params Other parameters for docker |
Jakub Josef | 1ef81e6 | 2017-04-03 19:14:54 +0200 | [diff] [blame] | 17 | * @return "docker app" - result of docker.build |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 18 | */ |
chnyda | 16535ab | 2017-05-25 16:28:28 +0200 | [diff] [blame] | 19 | def buildDockerImage(img, baseImg, dockerFile, timestamp, params=[]) { |
Jakub Josef | 1ef81e6 | 2017-04-03 19:14:54 +0200 | [diff] [blame] | 20 | def imageDir = dockerFile.substring(0, dockerFile.lastIndexOf("/")) |
| 21 | if (baseImg) { |
| 22 | sh "git checkout -f ${dockerFile}; sed -i -e 's,^FROM.*,FROM ${baseImg},g' ${dockerFile}" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 23 | } |
chnyda | 16535ab | 2017-05-25 16:28:28 +0200 | [diff] [blame] | 24 | |
azvyagintsev | fd40a25 | 2018-02-21 21:49:40 +0200 | [diff] [blame] | 25 | params << "--no-cache" |
chnyda | 16535ab | 2017-05-25 16:28:28 +0200 | [diff] [blame] | 26 | params << "-f ${dockerFile}" |
| 27 | params << imageDir |
| 28 | |
Jakub Josef | 1ef81e6 | 2017-04-03 19:14:54 +0200 | [diff] [blame] | 29 | return docker.build( |
| 30 | "${img}:${timestamp}", |
chnyda | 16535ab | 2017-05-25 16:28:28 +0200 | [diff] [blame] | 31 | params.join(' ') |
Jakub Josef | 1ef81e6 | 2017-04-03 19:14:54 +0200 | [diff] [blame] | 32 | ) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 33 | } |
chnyda | 1cf6f0d | 2017-06-02 11:01:04 +0200 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Build step to build docker image. |
| 37 | * |
| 38 | * @param dockerHubImg Name of image on dockerhub (ie: tcpcloud/salt-models-testing) |
| 39 | * @param defaultImg Image to use if dockerHubImg is not found |
| 40 | * @return img Docker image |
| 41 | */ |
| 42 | |
| 43 | def getImage(dockerHubImg, defaultImg="ubuntu:latest") { |
| 44 | |
| 45 | def img |
| 46 | |
| 47 | try { |
| 48 | img = docker.image(dockerHubImg) |
| 49 | img.pull() |
| 50 | } catch (Throwable e) { |
| 51 | img = docker.image(defaultImg) |
| 52 | } |
| 53 | |
| 54 | return img |
azvyagintsev | fd40a25 | 2018-02-21 21:49:40 +0200 | [diff] [blame] | 55 | } |