ANIL
ANIL

Reputation: 2682

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.

Upvotes: 0

Views: 1500

Answers (2)

tripleee
tripleee

Reputation: 189387

The quoting certainly looks wrong. The single quotes should probably go around the sed script;

# XXX Probably still wrong; see below
sed -i 's/ACC_KEY/${access_token}/g; s/SEC_KEY/${secret_token}/g' /root/.config/config.yml

though that's not correct either if these variables are coming from the environment; the shell won't substitute variables inside single quotes, but you can use double quotes:

sed -i "s/ACC_KEY/${access_token}/g; s/SEC_KEY/${secret_token}/g" /root/.config/config.yml

If there is a way to interpolate those values into the string already in Groovy, that's probably going to be somewhat more robust.

Upvotes: 1

visch
visch

Reputation: 775

Without seeing the entire config.yml that you have it's tough. With a config.yml like this, and a groovy method with what I have below should work for your needs!

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

Reference: https://jenkins.io/blog/2018/04/25/configuring-jenkins-pipeline-with-yaml-file/

Groovy method:

You could set environment variables in Jenkins and access them like this

println "access_key : ${env.access_key} , secret_key: ${secret_key}"

Reference: Jenkins Pipeline accessing environment variables

Upvotes: 1

Related Questions