在shell脚本中,我们{}何时在扩展变量时使用?
例如,我见过以下内容:
var=10 # Declare variable
echo "${var}" # One use of the variable
echo "$var" # Another use of the variable
Run Code Online (Sandbox Code Playgroud)
是否存在显着差异,还是只是风格?一个比另一个更受欢迎吗?
我是jenkins管道的新手,我正在定义一个声明性语法管道,我不知道我是否可以解决我的问题,因为我找不到解决方案.
在这个例子中,我需要将一个变量传递给ansible插件(在旧版本中我使用ENV_VAR或者使用inject插件从文件中注入它,该变量来自脚本.
这是我完美的风景(但它不起作用,因为环境{}):
pipeline {
agent { node { label 'jenkins-node'}}
stages {
stage('Deploy') {
environment {
ANSIBLE_CONFIG = '${WORKSPACE}/chimera-ci/ansible/ansible.cfg'
VERSION = sh("python3.5 docker/get_version.py")
}
steps {
ansiblePlaybook credentialsId: 'example-credential', extras: '-e version=${VERSION}', inventory: 'development', playbook: 'deploy.yml'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了其他方法来测试env vars在其他帖子中的工作方式,例如:
pipeline {
agent { node { label 'jenkins-node'}}
stages {
stage('PREPARE VARS') {
steps {
script {
env['VERSION'] = sh(script: "python3.5 get_version.py")
}
echo env.VERSION
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但"echo env.VERSION"返回null.
还尝试了相同的例子: - VERSION = python3.5 get_version.py …
在我的Jenkins管道中,我通常使用post声明性函数向我发送电子邮件,导致管道失败.
该post函数的简单语法如下:
post {
failure {
mail to: 'team@example.com',
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
body: "Something is wrong with ${env.BUILD_URL}"
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的电子邮件中,我还想提一下管道失败的哪个阶段(假设管道有5到6个阶段).我怎样才能做到这一点?任何帮助深表感谢.
上述要求的扩展将是向用户提供(失败的阶段)的实际错误日志,也作为失败通知电子邮件的一部分.
想法是,当用户收到来自jenkins的失败通知时,他应该知道管道的哪个阶段与错误日志一起失败.
提前致谢.
如果您的gradle.properties文件中定义了变量,那么何时需要使用花括号进行变量替换(例如"some string ${yourVariable}"),何时可以不使用(例如"some string $yourVariable").始终使用花括号是最佳做法吗?