blob: 796cc3c94e4764c93e63295cc63c71cbe21465a6 [file] [log] [blame]
Sergey Kolekonovba203982016-12-21 18:32:17 +04001package com.mirantis.mk
2
3/**
4 *
5 * Docker functions
6 *
7 */
8
9/**
Jakub Josef1ef81e62017-04-03 19:14:54 +020010 * Build step to build docker image.
Sergey Kolekonovba203982016-12-21 18:32:17 +040011 *
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
chnyda16535ab2017-05-25 16:28:28 +020016 * @param params Other parameters for docker
Jakub Josef1ef81e62017-04-03 19:14:54 +020017 * @return "docker app" - result of docker.build
Sergey Kolekonovba203982016-12-21 18:32:17 +040018 */
chnyda16535ab2017-05-25 16:28:28 +020019def buildDockerImage(img, baseImg, dockerFile, timestamp, params=[]) {
Jakub Josef1ef81e62017-04-03 19:14:54 +020020 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 Kolekonovba203982016-12-21 18:32:17 +040023 }
chnyda16535ab2017-05-25 16:28:28 +020024
azvyagintsevfd40a252018-02-21 21:49:40 +020025 params << "--no-cache"
chnyda16535ab2017-05-25 16:28:28 +020026 params << "-f ${dockerFile}"
27 params << imageDir
28
Jakub Josef1ef81e62017-04-03 19:14:54 +020029 return docker.build(
30 "${img}:${timestamp}",
chnyda16535ab2017-05-25 16:28:28 +020031 params.join(' ')
Jakub Josef1ef81e62017-04-03 19:14:54 +020032 )
Sergey Kolekonovba203982016-12-21 18:32:17 +040033}
chnyda1cf6f0d2017-06-02 11:01:04 +020034
35/**
36 * Build step to build docker image.
37 *
Jakub Josefd4887ab2018-05-10 16:13:33 +020038 * @param dockerHubImg Name of image on dockerhub (ie: mirantis/salt-models-testing)
chnyda1cf6f0d2017-06-02 11:01:04 +020039 * @param defaultImg Image to use if dockerHubImg is not found
40 * @return img Docker image
41 */
42
43def 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
azvyagintsevfd40a252018-02-21 21:49:40 +020055}