0

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.

Daniel Hawes
  • 103
  • 9

1 Answers1

0

Your first sh step fails since one command in the && chain fails. You don't need an && here. The commands in the first sh steps can be separated using normal line breaks. Also, use the shebang while you start the script. Example will be:

#!/bin/bash
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

What is the purpose of “&&” in a shell command?

Munavir Chavody
  • 489
  • 4
  • 16
  • The first lambda exist function does not cause the issue, in the output you can see it prints out the statement and moves on to the next check. My pipeline will exit after the check whenever the lambda function does not exist, due to the exit code. Also this is a jenkins pipeline, there is no need for a /bin/bash statement. – Daniel Hawes Aug 19 '19 at 14:25
  • `sh` and `bash` are different shells, although most of the unix like system has bash as default. Please read the link in my answer. You don't need an `&&` here, which is causing the pipeline to exit. – Munavir Chavody Aug 19 '19 at 14:27
  • the && is not the problem here, as it wouldn't hit the sh line below. Turns out the problem was the #!/bin/bash at the top which is interesting, never have had to include that in a sh command before. Is that due to declaring a function? – Daniel Hawes Aug 19 '19 at 14:31
  • This might be because of some functionality not being supported in `sh`. I'm not sure which one exactly. – Munavir Chavody Aug 19 '19 at 14:36