Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-55630

Support declarative pipelines

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      Currently, it only seems to be possible to create a scripted pipeline for Go projects with this plugin. I was struggling to find equivalent support for modeling a declarative pipeline.

      The issues I ran into:

      1. Accessing a tool in a declarative pipeline should work just fine.
      2. Defining a custom workspace is not allowed in a declarative pipeline.
      3. Setting the environment variables for different build stages is impractical. If possible those environment variables should already be preset.

      This is how I solved the issue right now, however, Jenkins seems to move away from scripted pipelines toward declarative ones. Let me know if this is already possible.

      node {
          def goHome = tool('go-1.11')
          
          ws("${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${BUILD_ID}/src/github.com/bmuschko/link-verifier") {
              withEnv(["GOROOT=${goHome}", "GOPATH=${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${BUILD_ID}/", "PATH+GO=${goHome}/bin"]) {
              ...
          }
      }
      

        Attachments

          Activity

          Hide
          orrc Christopher Orr added a comment -

          Can you show the Declarative Pipeline that you tried already and are having trouble with?

          The Go-related environment variables should already be exported during a Pipeline build, and so this Pipeline works fine to install Go and be able to run it:

          pipeline {
            agent any
            tools {
              go '1.11'
            }
            stages {
              stage('Build') {
                steps {
                  sh 'go version'
                  sh 'git clone https://github.com/bmuschko/link-verifier'
                  dir('link-verifier') {
                    sh 'go build …'
                  }
                }
              }
            }
          } 

          Does that cover what you're trying to do?

          Show
          orrc Christopher Orr added a comment - Can you show the Declarative Pipeline that you tried already and are having trouble with? The Go-related environment variables should already be exported during a Pipeline build, and so this Pipeline works fine to install Go and be able to run it: pipeline { agent any tools { go '1.11' } stages { stage( 'Build' ) { steps { sh 'go version' sh 'git clone https: //github.com/bmuschko/link-verifier' dir( 'link-verifier' ) { sh 'go build …' } } } } } Does that cover what you're trying to do?
          Hide
          bmuchko Benjamin Muschko added a comment -

          Thanks for your response. I should have mentioned that I am using `dep` for package management.

          pipeline {
              agent any
              tools {
                  go 'go-1.11'
              }
              stages {
                  stage('Prepare') {
                      steps {
                          sh 'go version'
                          sh 'curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh'
                          sh 'dep ensure'
                      }
                  }
                  stage('Build') {
                      steps {
                          sh 'go build'
                      }
                  }
              }
          }
          

          When trying to run `dep ensure` I get the following error message:

          + dep ensure
          /Users/bmuschko/.jenkins/workspace/link-verifier is not within a known GOPATH/src
          

          That's why I came to the conclusion that I needed a custom workspace. Do you have another idea on how to work around the issue? Things are probably easier with `vgo` as your project source doesn't necessarily have to reside in `$GOPATH/src`.

          Show
          bmuchko Benjamin Muschko added a comment - Thanks for your response. I should have mentioned that I am using `dep` for package management. pipeline { agent any tools { go 'go-1.11' } stages { stage( 'Prepare' ) { steps { sh 'go version' sh 'curl https: //raw.githubusercontent.com/golang/dep/master/install.sh | sh' sh 'dep ensure' } } stage( 'Build' ) { steps { sh 'go build' } } } } When trying to run `dep ensure` I get the following error message: + dep ensure /Users/bmuschko/.jenkins/workspace/link-verifier is not within a known GOPATH/src That's why I came to the conclusion that I needed a custom workspace. Do you have another idea on how to work around the issue? Things are probably easier with `vgo` as your project source doesn't necessarily have to reside in `$GOPATH/src`.
          Hide
          bmuchko Benjamin Muschko added a comment -

          FYI: I switched my project to Go modules. Go modules makes the setup much easier. You can just use a declarative pipeline definition.

          Show
          bmuchko Benjamin Muschko added a comment - FYI: I switched my project to Go modules. Go modules makes the setup much easier. You can just use a declarative pipeline definition.

            People

            • Assignee:
              orrc Christopher Orr
              Reporter:
              bmuchko Benjamin Muschko
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: