Home > OS >  Jenkins stage doesn't call custom method
Jenkins stage doesn't call custom method

Time:10-29

I have a Jenkins pipeline that does some code linting through different environments. I have a linting method that I call based on what parameters are passed. However, during my build, the stage that calls the method does nothing and returns nothing. Everything appears to be sane to me. Below is my code, and the stages showing the null results.

Jenkinsfile:

IAMMap = [
  "west": [
    account: "XXXXXXXX",
  ],
  "east": [
    account: "YYYYYYYYY",
  ],
]

pipeline {
  options {
    ansiColor('xterm')
  }

  parameters {
    booleanParam(
      name: 'WEST',
      description: 'Whether to lint code from west account or not. Defaults to "false"',
      defaultValue: false
    )
    booleanParam(
      name: 'EAST',
      description: 'Whether to lint code from east account or not. Defaults to "false"',
      defaultValue: true
    )
    booleanParam(
      name: 'LINT',
      description: 'Whether to perform linting. This should always default to "true"', 
      defaultValue: true
    )
  }

  environment {
    CODE_DIR             = "/code"
  }

  stages {
    stage('Start Lint') {
      steps {
        script {
          if (params.WEST && params.LINT) {
            codeLint("west")
          }

          if (params.EAST && params.LINT) {
            codeLint("east")
          }
        }
      }
    }
  }

  post {
    always {
      cleanWs disableDeferredWipeout: true, deleteDirs: true
    }
  }
}

def codeLint(account) {
  return {
    stage('Code Lint') {
      dir(env.CODE_DIR) {
        withAWS(IAMMap[account]) {
          sh script: "./lint.sh"
        }
      }
    }
  }
}

Results:

15:00:20  [Pipeline] { (Start Lint)
15:00:20  [Pipeline] script
15:00:20  [Pipeline] {
15:00:20  [Pipeline] }
15:00:20  [Pipeline] // script
15:00:20  [Pipeline] }
15:00:20  [Pipeline] // stage
15:00:20  [Pipeline] stage
15:00:20  [Pipeline] { (Declarative: Post Actions)
15:00:20  [Pipeline] cleanWs
15:00:20  [WS-CLEANUP] Deleting project workspace...
15:00:20  [WS-CLEANUP] Deferred wipeout is disabled by the job configuration...
15:00:20  [WS-CLEANUP] done

As you can see nothing gets executed. I assure you I am checking the required parameters when running Build with Parameters in the console. As far as I know, this is the correct syntax for a declarative pipeline.

CodePudding user response:

Don't return the Stage, just execute it within the codeLint function.

def codeLint(account) {
    stage('Code Lint') {
      dir(env.CODE_DIR) {
        withAWS(IAMMap[account]) {
          sh script: "./lint.sh"
        }
      }
    }
}

Or once the Stage is returned you can run it. This may need Script approval.

codeLint("west").run()
  • Related