5. Triggers

5.1. Webhook

  • Webhook trigger for GitScm polling

5.2. Cron

  • cron()

  • Accepts a cron-style string to define a regular interval at which the Pipeline should be re-triggered

triggers {
    cron('H */4 * * 1-5')
}

5.3. Poll SCM

  • pollSCM()

  • Build periodically

  • Accepts a cron-style string to define a regular interval at which Jenkins should check for new source changes

  • If new changes exist, the Pipeline will be re-triggered

  • Available since Jenkins 2.22

triggers {
    pollSCM('H */4 * * 1-5')
}

5.4. Upstream

  • Build after other projects are built

  • upstream()

  • Accepts a comma separated string of jobs and a threshold

  • When any job in the string finishes with the minimum threshold, the Pipeline will be re-triggered.

triggers {
    upstream(upstreamProjects: 'job1,job2',
             threshold: hudson.model.Result.SUCCESS)
}

5.5. REST API

Listing 41. build trigger via Jenkins API
curl -X POST http://localhost:8080/job/JOB_NAME/build \
--user USER:TOKEN \
--data-urlencode json='{
    "parameter": [
        {"name":"id", "value":"123"},
        {"name":"verbosity", "value":"high"}
    ]}'

5.6. Example

Listing 42. Example Trigger
pipeline {
    agent any

    triggers {
        cron('@daily')
    }

    stages {
        stage("Test") {
            steps {
                sh '/bin/echo Testing...'
            }
        }
    }
}

5.7. Assignments

5.7.1. API Trigger

  1. Napisz skrypt sh wykorzystujący curl

  2. Skrypt po odpaleniu ma triggerować build

  3. Dodaj skrypt do crontab

  4. Skrypt ma się uruchamiać @daily

  5. Zwróć uwagę, że cron ma mniejszą ilość zmiennych środowiskowych (skrypt, który u Ciebie działa, może nie być odpalany przez cron)