I'm attempting to update ECS task and service by using the following stage in my pipeline:
stage('Update ECS Task Definition & Service') {
steps {
dir('foo') {
sh """
TASK_DEFINITION=\$(aws ecs describe-task-definition --task-definition \"${env.TASK_NAME}\" --region \"${env.AWS_REGION}\")
NEW_TASK_DEFINITION=\$(echo \${TASK_DEFINITION} | jq '.taskDefinition | del(.taskDefinitionArn) | del(.revision) | del(.status) | del(.requiresAttributes) | del(.compatibilities) | del(.registeredAt) | del(.registeredBy)')
NEW_REVISION=\$(aws ecs register-task-definition --region \"${env.AWS_REGION}\" --cli-input-json \"${NEW_TASK_DEFINITION}\")
NEW_REVISION_DATA=\$(echo \${NEW_REVISION} | jq '.taskDefinition.revision')
echo "New Revision for \${env.TASK_NAME} is: \${NEW_REVISION_DATA}"
"""
}
}
}
This produces the following error:
groovy.lang.MissingPropertyException: No such property: NEW_TASK_DEFINITION for class: groovy.lang.Binding
Am I not escaping correctly? I'm not sure the cleanest way to write this.
Note: env.TASK_NAME
and env.AWS_REGION
are environment variables earlier declared in the pipeline.
CodePudding user response:
This should work. Take a look at the following working pipeline.
pipeline {
agent any
stages {
stage('Example') {
steps {
script {
sh """
VAR1=\$(echo "abcd123")
VAR2=\$(echo \$VAR1)
echo \$VAR1
echo \$VAR2
"""
}
}
}
}
}
If you are not passing any Pipeline variables into the shell block, you can simply use three single quotes(''') instead of """. In that case, you don't have to escape the $
.
pipeline {
agent any
stages {
stage('Example') {
steps {
script {
sh '''
VAR1=$(echo "abcd123")
VAR2=$VAR1
echo $VAR1
echo $VAR2
'''
}
}
}
}
}
Update
You should be escaping the $
before the NEW_TASK_DEFINITION
check the following.
NEW_REVISION=\$(aws ecs register-task-definition --region \"${env.AWS_REGION}\" --cli-input-json \"\${NEW_TASK_DEFINITION}\")