I'm trying to run the sed
command in Jenkins groovy scripted pipeline file.
Parsing variable as below
def SERVICE = args.service
def RESOURCE = "Services"
regionSuffix = (action == 'failover') ? 'us-east-2' : 'us-east-1'
environment taking as an argument
The below sed
command is working in the Ubuntu terminal.
sed "\|CFT_ENV_FILE|s|$|$RESOURCE/$SERVICE/$environment-$regionSuffix.yml|" docker-compose.yml > docker-compose-$SERVICE.yml
but when I apply this command via Groovy file it gives me an error:
sh '''sed "\\|CFT_ENV_FILE|s|$|"${RESOURCE}"/"${SERVICE}"/"${args.environment}"-"${regionSuffix}".yml|" docker-compose.yml > docker-compose-"${SERVICE}".yml'''
[2022-08-03T11:54:48.642Z] /tmp/jenkins-ac851b81/workspace/Infrastructure/failover/region-failover-test-job@tmp/durable-98781677/script.sh: line 1:
"\|CFT_ENV_FILE|s|$|"${RESOURCE}"/"${SERVICE}"/"${args.environment}"-"${regionSuffix}".yml|": bad substitution
script returned exit code 1
CodePudding user response:
Your variables are in Jenkins/Groovy scope, not in shell scope.
Therefore, you need to substitute their values before passing them to shell:
Use "
(double quotes) instead of '
(single quotes):
sh """sed '\\|CFT_ENV_FILE|s|$|${RESOURCE}/${SERVICE}/${args.environment}-${regionSuffix}.yml|' docker-compose.yml > docker-compose-${SERVICE}.yml"""
Please notice that you don't need quotes every time you use a variable (e.g "${RESOURCE}")
Also - consider that you probably need to escape dollar sign (when not used to groovy vars) else based on the command logic.
You can also use just one quotes sign instead of triple, the triple are used for multi-lined string