Issue with Jenkins pipeline stript

Viewed 17

I am trying to build a pipeline for modifying kafka topics. Part of the steps includes getting the current number of partitions of the topic to determine if it can be updated or not. For some reason, jenkins does not like my use of variables in the script. I am getting an error on --topic ${TOPIC}. the variable is correctly enclosed by {} so i don't understand what is going on.

error being given:

WorkflowScript: 32: illegal string body character after dollar sign;
   solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 32, column 36.
                       --topic ${TOPIC} | head -1)

snippet of pipeline:

script {
                    sh """
                    if [ -n "\$(printf '%s\n' ${NUM_PARTITIONS} | sed 's/[0-9]//g')" ]; then
                      echo 'Num partitions is not numeric'
                      exit 1 
                    fi
                    """
                    
                    sh """
                    desc=\$(kafka-topics.sh \
                    --bootstrap-server ${KAFKA_SERVERS} \
                    --command-config sasl.properties \
                    --describe \
                    --topic ${TOPIC} | head -1)
                    
                    if [ \$? -ne 0 ]; then
                      echo 'describe failed'
                      exit 1
                    fi
                    
                    np=\$(echo desc | gawk 'match($0, /PartitionCount:\s*([[:digit:]]*)\s*/, a) {print a[1]}')
                    
                    if [ ${NUM_PARTITIONS} -le np ]; then
                        echo 'num partitions <= configured partitions.  alter skipped'
                    else
                      kafka-topics.sh \
                      --bootstrap-server ${KAFKA_SERVERS} \
                      --command-config sasl.properties \
                      --alter \
                      --topic ${TOPIC} \
                      --partitions ${NUM_PARTITIONS}
                    fi
                    """
}
1 Answers

got it working. Weird escaping rules...

script {
                    sh """
                    if [ -n "\$(printf '%s\n' ${NUM_PARTITIONS} | sed 's/[0-9]//g')" ]; then
                      echo 'Num partitions is not numeric'
                      exit 1 
                    fi

                    if [ -n "\$(printf '%s\n' ${RETENTION} | sed 's/[0-9]//g')" ]; then
                      echo 'Retention is not numeric'
                      exit 1 
                    fi
                    """
                    
                    sh """
                    desc=\$(${KAFKA_SCRIPTS_LOC}/kafka-topics.sh \
                    --bootstrap-server ${KAFKA_SERVERS} \
                    --command-config ${SASL_LOC} \
                    --describe \
                    --topic $TOPIC)
                    
                    if [ \$? -ne 0 ]; then
                      echo 'describe failed'
                      exit 1
                    fi
                    
                    np=\$(echo \${desc} | head -1 | gawk 'match(\$0, /PartitionCount:\\s*([[:digit:]]*)\\s*/, a) {print a[1]}')
                    
                    if [ ${NUM_PARTITIONS} -le \${np} ]; then
                        echo 'num partitions <= configured partitions.  alter skipped'
                    else
                      ${KAFKA_SCRIPTS_LOC}/kafka-topics.sh \
                      --bootstrap-server ${KAFKA_SERVERS} \
                      --command-config ${SASL_LOC} \
                      --alter \
                      --topic ${TOPIC} \
                      --partitions ${NUM_PARTITIONS}
                    fi
                    """
}
Related