TheJeff
TheJeff

Reputation: 4121

Jenkins with Jenkinsfile Pipeline - Trigger Builds Remotely

I have a Jenkinsfile setup for our CI/CD pipeline, and it runs through the pipeline on git actions like Pull Requests, Branch Creation, Tag Pushes, etc..

Prior to this setup, I was used to setting up Jenkins build jobs in the Jenkins UI. The advantage of this, was that I could setup dedicated build jobs that I could trigger remotely, and independently of git webhook actions. I could do a POST to the job endpoint with parameters to trigger various actions.

Documentation for this process would be referenced here - see "Trigger Builds Remotely"

I could also hit the big button that says "Build", or "Build with Parameters" in the UI, which was super nice.

How would one do this with a Jenkinsfile? Is this even possible to define build jobs in a pipeline definition within a Jenkinsfile? I.E. define functions / build jobs that have dedicated URLs that could be called on the Jenkins URL independent of webhook callbacks?

What's the best practice here?

Thanks for any tips, references, suggestions!

Upvotes: 1

Views: 149

Answers (1)

S.Spieker
S.Spieker

Reputation: 7385

I would recommend starting with Multibranch pipelines. In general you get all the things you mentioned, but a little better. Because thhe paramteres can be defined within your Jenkinsfile. In short just do it like this:

  1. Create a Jenkinsfile an check this into a Git Repository.
  2. To create a Multibranch Pipeline: Click New Item on Jenkins home page.
  3. Enter a name for your Pipeline, select Multibranch Pipeline and click OK.
  4. Add a Branch Source (for example, Git) and enter the location of the repository.
  5. Save the Multibranch Pipeline project.

A declarative Jenkinsfile can look like this:

    pipeline {
      agent any
      parameters {
        string(name: 'Greeting', defaultValue: 'Hello', description:  'How should I greet the world?')
      } 
      stages {
        stage('Example') {
          steps {
            echo "${params.Greeting} World!"
          }
        }
      }
    }

A good tutorial with screenshhots can be found here: https://www.jenkins.io/doc/book/pipeline/multibranch/

Upvotes: 0

Related Questions