How to configure Jenkins to launch a job on it's startup without using UI manual configuration

1.6k Views Asked by At

I want to implement a healthcheck(invoking an endpoint) in Jenkins, which should run in docker container. I don't want to manually configure my pipeline job in Jenkins every time container is up.

I've tried JCasC, but it doesn't allow to autoconfigure jobs.

How do I automatically configure jobs on first Jenkins launch and trigger them on each time Jenkins is up?

2

There are 2 best solutions below

0
Jamie Jackson On

Here's is an example jobs section of jenkins.yml (CasC config file); it requires the "Job DSL" and "Startup Trigger" plugins (in addition to the "Configuration as Code" plugin, of course):

jobs:
  - script: >
      pipelineJob("Deploy Local2 Stack") {
        description()
        keepDependencies(false)
        triggers {
          hudsonStartupTrigger {
            nodeParameterName("master")
            label("master")
            quietPeriod("0")
            runOnChoice("False")
          }
        }
        definition {
          cpsScm {
            scm {
              git {
                remote {
                  url("https://myrepo/mystuff.git")
                  credentials("scm")
                }
                branch("master")
              }
            }
            scriptPath("pipeline/main/Jenkinsfile")
          }
        }
        disabled(false)
      }

The triggers section is what's important; the rest is just to put it in context.

Apparently, the startup trigger can also work on slave nodes but I gather that's not what you want, so I specified master.

Note: This is the kernel from which I pieced together the solution: https://issues.jenkins-ci.org/browse/JENKINS-41671?focusedCommentId=287026&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-287026

1
Skiv On

Could you please tell me where you can see all the plugins that can be installed using jcasc? Or where to get the plugins you talked about