最小"run this command in this docker image" jenkinsfile



有一个docker映像,其名称为image_name。

我想要一份詹金斯的工作,先是docker pull IMAGE_NAME,然后是docker run IMAGE_NAME /bin/bash -c '/usr/bin/my_startup arg1 arg2 arg3

我根据一些谷歌搜索的例子尝试了一些Jenkinsfile的变体,但它们不起作用,而且我显然不了解阶段、代理、steps {sh '...'}命令在什么环境中运行,或者任何Jenkins术语。

什么是最小的jenkinsfile,它将提取IMAGE_NAME并运行其中包含参数的命令?

最小jenkinsfile

只要这不是";代码高尔夫";风格问题,下面的管道将为你做的技巧:

pipeline {
agent any // modify as needed for an agent that supports container runtime
stages {
stage('Pull Image and Run Container') {
script {
// pulls image and then executes as container; note bonus example of arguments to container instantiation if needed
docker.image('IMAGE_NAME').withRun('-e "env_var=env_value" -p 1234:5678') { container -> // container is lambda scope variable which you can reference as needed
// execute startup
sh(label: 'Startup', script: '/usr/bin/my_startup arg1 arg2 arg3')
}
}
}
}
}

如果你有一个私人和/或自定义注册表,那么你需要在Jenkins主服务器上安装withRegistry块和注册表凭据插件。

这很有效:

docker.image('MY_IMAGE').inside {
sh '/bin/my-command my-args'
}

显然,这是一个Pipeline脚本,在Jenkins 2.150.1中的Groovy沙箱中运行。

相关内容

最新更新