【发布时间】:2021-03-22 17:22:21
【问题描述】:
我正在使用 Jenkins 和 kubernetes 插件来运行我的作业,我需要运行一个管道:
- 构建 docker 镜像
- 提交到注册表
- 在以下步骤中使用相同的图像来执行测试。
Container(image:A): build image B
Container(image:B) : test image B
所以我想使用变量并在 kubernetes podtemplate 中替换它们,如下所示:
pipeline {
agent none
stages {
stage("Build image"){
// some script that builds the image
steps{
script{
def image_name = "busybox"
}
}
}
stage('Run tests') {
environment {
image = "$image_name"
}
agent {
kubernetes {
yaml """\
apiVersion: v1
kind: Pod
metadata:
labels:
some-label: some-label-value
spec:
containers:
- name: busybox
image: "${env.image}"
command:
- cat
tty: true
""".stripIndent()
}
}
steps {
container('busybox') {
sh 'echo "I am alive!!"'
}
}
}
}
}
但我得到的变量是空的:
[Normal][ci/test-10-g91lr-xtc20-s1ng1][Pulling] Pulling image "null"
[Warning][ci/test-10-g91lr-xtc20-s1ng1][Failed] Error: ErrImagePull
[Warning][ci/test-10-g91lr-xtc20-s1ng1][Failed] Failed to pull image "null": rpc error: code = Unknown desc = Error response from daemon: pull access denied for null, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
你知道我怎样才能实现类似的行为吗?
【问题讨论】:
-
注意:我已经看到,如果我将
environment {}添加为pipeline{}的子级,它可以工作,但这不是我想要实现的,因为我希望图像是从上一步生成的,而不是在管道中硬编码 -
尝试在“build image”阶段直接设置
env.image = 'busybox',并从“run tests”中移除environment块。
标签: jenkins kubernetes