I have kind of a weird setup that I need to run a command as a docker container in a sh
block in a Jenkinsfile.
Issue that I'm facing is specifically around the awk
command used to trim the output.
Here is the command that WORKS FINE when ran directly on a bash shell:
OPFILENAME=$(docker run -t \
-e AWS_SECRET_ACCESS_KEY='<omitted>' \
-e AWS_ACCESS_KEY_ID='<omitted>' \
-e AWS_DEFAULT_REGION='us-east-1' \
mydockerimage:0.1 \
bash -c "aws s3 ls my-bucket-name/dir/ | sort | tail -n 1 | awk '{print \$4}' ") && \
echo $OPFILENAME
So I need to run this exact same thing on a remote host through a Jenkins pipeline, here is the general syntax:
pipeline {
agent any
environment {
BUILDHOST = 'buildhost.example.com'
SSHCMD = "ssh -o StrictHostKeyChecking=no jenkins@${env.BUILDHOST}"
}
stages {
stage('Get filename from s3') {
steps {
sshagent ( ['ssh_config']) {
sh """${SSHCMD} '''
OPFILENAME=\$(sudo docker run -t \
-e AWS_SECRET_ACCESS_KEY='<omitted>' \
-e AWS_ACCESS_KEY_ID='<omitted>' \
-e AWS_DEFAULT_REGION='us-east-1' \
mydockerimage:0.1 \
bash -c "aws s3 ls my-bucket-name/dir/ | sort | tail -n 1 | awk '{print \$4}') && \
echo \$OPFILENAME
'''
"""
}
}
}
}
}
Here is the error that Jenkins throws:
...bash -c "aws s3 ls my-bucket-name/dir/ | sort | tail -n 1 | awk {print' '}") && echo $OPFILENAME "
'
bash: -c: line 1: unexpected EOF while looking for matching `"'
bash: -c: line 3: syntax error: unexpected end of file
Notice how it transformed the awk
command as such: awk {print' '}
Trying with various changes: ...| sort | tail -n 1 | awk \'{print \$4}\'")
results in the exact same error.
Tried like this: awk "'{print \$4}'"
and it doesn't throw an error but in the logs it shows like this: awk "{print' '}"
thus the awk trimming desired doesn't take place. Grr!
So I know there is something wrong with the quoting, and since I am using triple-quotes for multi-line commands it's dirtying it up even more!
I have referenced this gist, trying to make sense of how to do this: https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4 but still running into issue after issue.
Caveats:
- Command must run in an sshagent
block and on a remote host. Commands cannot be ran in the local Jenkins workspace.