从 Jenkins 执行时,sed 命令不会替换我的字符串
sed command won't replace my string when executed form Jenkins
我有一个 config.yml
文件,其中包含以下内容:
access_key: ACC_KEY
secret_key: SEC_KEY
现在我正在尝试用实际的 access_key
和 secret_key
.
替换 ACC_KEY
和 SEC_KEY
我有一个 groovy 方法执行下面给出的 shell 脚本:
def update(){
return this.execCmd("'sed -i s/ACC_KEY/${access_token}/g; s/SEC_KEY/${secret_token}/g' /root/.config/config.yml")
}
我在方法中指定 sed
命令的方式有问题吗?因为,每当我 运行 我的 Jenkins 工作时,我都能够获取 ${access_token}
和 ${secret_token}
的值,但是它不会用那些替换 ACC_KEY 和 SEC_KEY值。
没有看到你所拥有的整个 config.yml 是很困难的。使用像这样的 config.yml,以及我下面的 groovy 方法应该可以满足您的需求!
config.yml
config:
dockerfile: .woloxci/Dockerfile
project_name: some-project-name
services:
- postgresql
- redis
steps:
analysis:
- bundle exec rubocop -R app spec --format simple
- bundle exec rubycritic --path ./analysis --minimum-score 80 --no-browser
setup_db:
- bundle exec rails db:create
- bundle exec rails db:schema:load
test:
- bundle exec rspec
security:
- bundle exec brakeman --exit-on-error
audit:
- bundle audit check --update
environment:
RAILS_ENV: test
GIT_COMMITTER_NAME: a
GIT_COMMITTER_EMAIL: b
LANG: C.UTF-8
access_key: ACC_KEY
secret_key: SEC_KEY
参考:https://jenkins.io/blog/2018/04/25/configuring-jenkins-pipeline-with-yaml-file/
Groovy 方法:
您可以在 Jenkins 中设置环境变量并像这样访问它们
println "access_key : ${env.access_key} , secret_key: ${secret_key}"
参考:
引用确实看起来不对。单引号应该围绕 sed
脚本;
# XXX Probably still wrong; see below
sed -i 's/ACC_KEY/${access_token}/g; s/SEC_KEY/${secret_token}/g' /root/.config/config.yml
尽管如果这些变量来自环境,那也不正确; shell 不会替换单引号内的变量,但您可以使用双引号:
sed -i "s/ACC_KEY/${access_token}/g; s/SEC_KEY/${secret_token}/g" /root/.config/config.yml
如果有办法将这些值插入到 Groovy 中已有的字符串中,那可能会更加稳健。
我有一个 config.yml
文件,其中包含以下内容:
access_key: ACC_KEY
secret_key: SEC_KEY
现在我正在尝试用实际的 access_key
和 secret_key
.
ACC_KEY
和 SEC_KEY
我有一个 groovy 方法执行下面给出的 shell 脚本:
def update(){
return this.execCmd("'sed -i s/ACC_KEY/${access_token}/g; s/SEC_KEY/${secret_token}/g' /root/.config/config.yml")
}
我在方法中指定 sed
命令的方式有问题吗?因为,每当我 运行 我的 Jenkins 工作时,我都能够获取 ${access_token}
和 ${secret_token}
的值,但是它不会用那些替换 ACC_KEY 和 SEC_KEY值。
没有看到你所拥有的整个 config.yml 是很困难的。使用像这样的 config.yml,以及我下面的 groovy 方法应该可以满足您的需求!
config.yml
config:
dockerfile: .woloxci/Dockerfile
project_name: some-project-name
services:
- postgresql
- redis
steps:
analysis:
- bundle exec rubocop -R app spec --format simple
- bundle exec rubycritic --path ./analysis --minimum-score 80 --no-browser
setup_db:
- bundle exec rails db:create
- bundle exec rails db:schema:load
test:
- bundle exec rspec
security:
- bundle exec brakeman --exit-on-error
audit:
- bundle audit check --update
environment:
RAILS_ENV: test
GIT_COMMITTER_NAME: a
GIT_COMMITTER_EMAIL: b
LANG: C.UTF-8
access_key: ACC_KEY
secret_key: SEC_KEY
参考:https://jenkins.io/blog/2018/04/25/configuring-jenkins-pipeline-with-yaml-file/
Groovy 方法:
您可以在 Jenkins 中设置环境变量并像这样访问它们
println "access_key : ${env.access_key} , secret_key: ${secret_key}"
参考:
引用确实看起来不对。单引号应该围绕 sed
脚本;
# XXX Probably still wrong; see below
sed -i 's/ACC_KEY/${access_token}/g; s/SEC_KEY/${secret_token}/g' /root/.config/config.yml
尽管如果这些变量来自环境,那也不正确; shell 不会替换单引号内的变量,但您可以使用双引号:
sed -i "s/ACC_KEY/${access_token}/g; s/SEC_KEY/${secret_token}/g" /root/.config/config.yml
如果有办法将这些值插入到 Groovy 中已有的字符串中,那可能会更加稳健。