我有一个python文件peekaboo.py
,它在独立执行时输出如下json。
{"fernet_key": "some fernet_key", "db_url": "some_db_url", "pki_password": "some_pki_password"}
然而,当jekins试图运行相同的python文件时,它会失败,如下所示:
顺便说一下,这个python文件应该设置pki_client_cacert_password, db_url, fernet_key的值,这些值将传递给sible playbookpipeline {
parameters {
string(defaultValue: '', description: '', name: 'pki_client_cacert_password', trim: true)
string(defaultValue: '', description: '', name: 'db_url', trim: true)
}
stages {
stage('DeployToDev') {
steps {
env.artifacts = sh "/var/lib/jenkins/python_jobs/peekaboo_venv/bin/python3 /var/lib/jenkins/python_jobs/peekaboo.py --db_url=${env.db_url} --pki_password=${env.pki_client_cacert_password}"
echo "${env.artifacts}"
ansiblePlaybook credentialsId: "${env.git_credential_id}", disableHostKeyChecking: true, extras: "-e "pki_client_cacert_password=${env.pki_client_cacert_password} db_url=${env.db_url} fernet_key=${env.fernet_key} use_isrg_freetds=false"",
}
}
}
}
我们得到以下错误
错误:
WorkflowScript: 32: Expected a step @ line 32, column 21.
env.artifacts = sh "/var/lib/jenkins/python_jobs/peekaboo_venv/bin/python3 /var/lib/jenkins/python_jobs/peekaboo.py --db_url=${env.db_url} --pki_password=${env.pki_client_cacert_password}"
我的问题是,
- 如何修复此错误
- 如何访问生成的值以传递给可见的作业。
您可以尝试使用赋值语句sh:
script{
env.artifacts = sh(
returnStdout: true,
script: "/var/lib/jenkins/python_jobs/peekaboo_venv/bin/python3 /var/lib/jenkins/python_jobs/peekaboo.py --db_url=${env.db_url} --pki_password=${env.pki_client_cacert_password}"
)
}
这只适用于管道作业,而不是自由式作业。
我必须添加script标签,并且还使用双引号"
来括住字符串
script{
env.artifacts = sh(
returnStdout: true,
script: "/var/lib/jenkins/python_jobs/peekaboo_venv/bin/python3 /var/lib/jenkins/python_jobs/peekaboo.py --db_url=${env.db_url} --pki_password=${env.pki_client_cacert_password}"
)
}