vij*_*yst 4 jenkins docker jenkins-docker jenkins-pipeline
我有一个Jenkinsfile或Jenkins管道,它创建一个新图像并从该图像中启动一个容器.它第一次运作良好.但是在后续运行中,我希望停止并删除前一个容器.我的Jenkins文件如下:
node {
def commit_id
stage('Preparation') {
checkout scm
sh "git rev-parse --short HEAD > .git/commit-id"
commit_id = readFile('.git/commit-id').trim()
}
stage('docker build/push') {
docker.withRegistry('https://index.docker.io/v1/', 'dockerhub') {
def app = docker.build("my-docker-id/my-api:${commit_id}", '.').push()
}
}
stage('docker stop container') {
def apiContainer = docker.container('api-server')
apiContainer.stop()
}
stage('docker run container') {
def apiContainer = docker.image("my-docker-id/my-api:${commit_id}").run("--name api-server --link mysql_server:mysql --publish 3100:3100")
}
}
Run Code Online (Sandbox Code Playgroud)
舞台'docker stop container'失败了.那是因为我不知道正确的API来获取容器并阻止它.谢谢.
与在Jenkins文件中一样,您可以使用sh命令.
这样,您可以使用以下行:
sh 'docker ps -f name=zookeeper -q | xargs --no-run-if-empty docker container stop'
sh 'docker container ls -a -fname=zookeeper -q | xargs -r docker container rm'
Run Code Online (Sandbox Code Playgroud)
这将确保容器x(此处命名zookeper)(如果它正在运行)首先被停止并被移除.
迈克尔A.指出,在评论这不是一个妥善的解决办法,并承担被在从安装泊坞窗.
他指的是jenkinsci/plugins/docker/workflow/Docker.groovy,但是Docker该类的容器方法尚未实现.
2018年8月更新:
Pieter Vogelaar在评论中指出" Jenkinsfile Docker管道多阶段 "他写道:
通过使用全局
pipelineContext对象,可以在更进一步的阶段中使用返回的容器对象.
它是:
pipelineContext全局变量,其类型为LinkedHashMap.
Jenkinsfile编程语言是Groovy.在Groovy中,这接近于JavaScript对象的等价物.此变量使得可以在阶段之间共享数据或对象.
所以这是一个声明性管道,从以下开始:
// Initialize a LinkedHashMap / object to share between stages
def pipelineContext = [:]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4742 次 |
| 最近记录: |