Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declarative Pipeline - Support CustomPipelineTemplate #219

Closed
sujikann opened this issue May 8, 2020 · 6 comments · Fixed by #237
Closed

Declarative Pipeline - Support CustomPipelineTemplate #219

sujikann opened this issue May 8, 2020 · 6 comments · Fixed by #237
Labels
bug Something isn't working documentation Improvements or additions to documentation
Milestone

Comments

@sujikann
Copy link

sujikann commented May 8, 2020

Overview

The terraform-pipeline supports setting up a Declarative pipeline. Declarative pipelines give us the advantage of restarting pipeline from a stage from a previous run. "Restart from Stage" requires the stash artifact to be available to use in a subsequent stage.
This issue can be resolved by using a CustomPipelineTemplate. However, Jenkins throws an error when we use a CustomPipelineTemplate.
Version
Pipeline 2.6 version on Jenkins
terraform-pipeline 5.6

Error

groovy.lang.MissingPropertyException: No such property: none for class: CustomPipelineTemplate

@Library(['terraform-pipeline@v5.6', 'terraform-pipeline-customizations']) _

Jenkinsfile.init(this)
Jenkinsfile.declarative = true
Jenkinsfile.pipelineTemplate = this.CustomPipelineTemplate

vars/CustomPipelineTemplate.groovy

pipeline {
     agent none

     options { preserveStashes() }
     stages {
         stage('Validate') {
             steps {
                 script {
                     validate.build()
                 }
             }
         }
 
         stage('Build Artifact') {
             steps {
                 script {
                     buildArtifact.build()
                 }
             }
         }
  }
@dkolb
Copy link
Contributor

dkolb commented May 9, 2020

Per the README.md, vars/CustomPipelineTemplate.groovy should define a method call(stages) and then you use ((Stage)stages.getAt(index)).build() inside the script block.

@sujikann
Copy link
Author

I tried this but it didnt work. The pipeline gives an error saying
groovy.lang.MissingPropertyException: No such property: any for class: CustomPipelineTemplate.

@kmanning
Copy link
Collaborator

kmanning commented May 29, 2020

Talked with sujikann, and confirmed the problem.

  1. Follow the README and create a custom template:
// var/CustomPipelineTemplate.groovy
def call(stages) {
    pipeline {
        agent none

        stages {
...
  1. Create a declarative pipeline, and use your template:
Jenkinsfile.init(this)
Jenkinsfile.declarative = true
Jenkinsfile.pipelineTemplate = this.CustomPipelineTemplate

Expected: you get a declarative pipeline using the code provided by your template
Actual: you get the error groovy.lang.MissingPropertyException: No such property: any for class: CustomPipelineTemplate.

@kmanning
Copy link
Collaborator

kmanning commented May 29, 2020

WORKAROUND: Looks like there's a defect in the README itself. For some reason, naming your variable stages causes some sort of problem. If you rename the variable to args, the problem goes away.

Example:

// var/CustomPipelineTemplate.groovy
def call(args) {
    pipeline {
        agent none

        stages {
...

The docs should be fixed, so that if you copy/paste the example code, it works.

@kmanning kmanning added bug Something isn't working documentation Improvements or additions to documentation labels May 29, 2020
@kmanning kmanning added this to the v5.8 milestone May 29, 2020
@sujikann
Copy link
Author

sujikann commented May 29, 2020

Yes, renaming stages resolved the issue. Thanks @kmanning

@kmanning kmanning reopened this May 29, 2020
@kmanning
Copy link
Collaborator

Gonna reopen the issue, only because the documentation is incorrect and should be fixed, to prevent this problem/confusion in the future.

@kmanning kmanning linked a pull request Jul 7, 2020 that will close this issue
@kmanning kmanning closed this as completed Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants