【发布时间】:2023-01-07 03:05:40
【问题描述】:
我是 Jenkins 的新手,已经连续 3 天处理一个问题,一直想不通,所以我希望有人能帮忙。
我正在尝试将一个秘密从 hashicorp 保险库传递到一个詹金斯管道中,看起来我可以提取这个秘密,但我不能在 withVault 语句的大括号之外使用它,有人可以指出我正确的方向吗将这个秘密变成一个全局变量,然后我可以在管道内部使用它?
这是我的代码:
#!/usr/bin/env groovy
def projectProperties = [
[$class: 'BuildDiscarderProperty',strategy: [$class: 'LogRotator', numToKeepStr: '5']]
]
node{
withVault(configuration: [timeout: 60, vaultCredentialId: 'approle', vaultUrl: 'https://redacted.com'], vaultSecrets: [[path: '/secrets/kaniko', secretValues: [[vaultKey: 'key']]]])
{
sh 'echo $key' #Shows that the key has been pulled while running the pipeline
}
}
pipeline {
agent {
kubernetes {
cloud 'openshift'
idleMinutes 15
activeDeadlineSeconds 1800
yaml """
apiVersion: v1
kind: Pod
metadata:
name: kaniko
spec:
volumes:
- name: build-context
emptyDir: {}
- name: kaniko-secret
secret:
secretName: regcred-${NAMESPACE}
items:
- key: .dockerconfigjson
path: config.json
securityContext:
runAsUser: 0
serviceAccount: kaniko
initContainers:
- name: kaniko-init
image: ubuntu
containers:
- name: kaniko
image: gcr.io/kaniko-project/executor:latest
args: ["--context=git://${key}@github.com/redacted/dockerfiles.git#refs/heads/${BRANCH}",
"--destination=image-registry.openshift-image-registry.svc:5000/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}",
"--dockerfile=/jenkins-slave-ansible/Dockerfile",
"--skip-tls-verify"]
resources:
limits:
cpu: 1
memory: 5Gi
requests:
cpu: 100m
memory: 256Mi
volumeMounts:
- name: build-context
mountPath: /kaniko/build-context
- name: kaniko-secret
mountPath: /kaniko/.docker
restartPolicy: Never
"""
}
}
parameters {
choice(name: 'NAMESPACE', choices: ['engineering', 'ce-jenkins-testing'])
string(defaultValue: 'master', description: 'Please enter your branch name', name: 'BRANCH')
string(defaultValue: 'test', description: 'Please enter your image name (e.g.: jenkins-slave-ansible)', name: 'IMAGE_NAME')
string(defaultValue: 'latest', description: 'Please add your tag (e.g.: 1.72.29)', name: 'IMAGE_TAG')
}
etc..... more code below
我需要能够使用行内上方的密钥:
args: ["--context=git://${key}@github.com/redacted/dockerfiles.git#refs/heads/${BRANCH}"
提前致谢!
【问题讨论】:
-
是的,
withVault方法只将变量暴露给它自己的范围。您是否试图在agent指令中的 Kubernetes 清单中使用 Vault 秘密?您需要使用 Vault 凭据绑定插件将机密公开为agent指令中的凭据。 -
在保险库内有一个秘密,我想在上面提到的最后一行的管道内使用它。但我不确定如何将秘密传递到 withVault 范围之外。
标签: jenkins jenkins-pipeline jenkins-groovy hashicorp-vault