I have a Jenkins pipeline where I need to check if an AWS Lambda function exists. During this check, you receive a 0 response if it is a function in AWS, and 255 if it isn't a function in AWS. But the script exits upon reaching this 255.
I had seen code on here about ways to check if a function exists or not, and ended up pursuing the technique of checking the exit codes. However I need a way to catch this exit code without actually exiting the Jenkins pipeline script.
For test purposes, testJenkins already exists in AWS Lambda, while testJenkins2 does not exist.
agent any
environment {
PATH = "/var/lib/jenkins/aws:$PATH"
}
stages {
stage('Checkout code') {
steps {
checkout scm
}
}
stage('Zip up Lambda') {
steps {
dir('lambda/testJenkins') {
sh 'zip testJenkins.zip testJenkins.py'
}
}
}
stage('Upload to AWS') {
steps {
dir('lambda/testJenkins') {
sh '''function does_lambda_exist() {
aws lambda get-function --function-name $1 > /dev/null 2>&1
if [ 0 -eq $? ]; then
echo "Lambda '$1' exists"
else
echo "Lambda '$1' does not exist"
fi
} && does_lambda_exist testJenkins && does_lambda_exist testJenkins2'''
sh 'aws lambda create-function --zip-file fileb://testJenkins.zip --function-name testJenkins --runtime python3.7 --role <role> --handler testJenkins.lambda_handler'
}
}
}
}
}
It should continue after the first test, but unfortunately exits right after receiving the exit code:
+ does_lambda_exist testJenkins
+ aws lambda get-function --function-name testJenkins
+ '[' 0 -eq 0 ']'
+ echo 'Lambda '\''testJenkins'\'' exists'
Lambda 'testJenkins' exists
+ does_lambda_exist testJenkins2
+ aws lambda get-function --function-name testJenkins2
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 255
Finished: FAILURE
Any help/better methods is greatly appreciated. New to Jenkins pipelines.