首页 > 解决方案 > sed command won't replace my string when executed form Jenkins

问题描述

I have a config.yml file that contains the following:

access_key: ACC_KEY
secret_key: SEC_KEY

Now I am trying to replace the ACC_KEY and SEC_KEY with the actual access_key ans secret_key.

I have a groovy method that executes a shell script as given below:

def update(){
    return this.execCmd("'sed -i s/ACC_KEY/${access_token}/g; s/SEC_KEY/${secret_token}/g' /root/.config/config.yml")
} 

Is there something wrong in the way I have specified the sed command inside the method? Because, whenever I run my Jenkins job, I am able to fetch the values of ${access_token} and ${secret_token} however it is not replacing ACC_KEY and SEC_KEY with those values.

标签: groovysedjenkins-pipelinejenkins-groovy

解决方案


没有看到你拥有的整个 config.yml 就很难了。使用像这样的 config.yml 以及下面我所拥有的 groovy 方法应该可以满足您的需求!

配置.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}"

参考:Jenkins Pipeline 访问环境变量


推荐阅读