tetienne
tetienne

Reputation: 379

How to run a stage only when build is launched manually

My team owns a non regression testing project. In this project, there is code and non regression test. Like classic project we want to analyse our code with linter or other tools. But we don't want to run our tests on each branch for each commit, they last hours. We want to launch these tests manually.

To run test exclusively on master we have this in our Jenkinsfile:

stage("Test") {
            when {branch "master"}

            steps {

               sh 'pipenv run pytest -n5 --dist=loadscope --junitxml report.xml |
            }
            post {
              always {
                junit 'report.xml'
              }
            }
        }

But once we merge our branch into master, a build on master is triggered and the tests are launched.

To avoid this, I think I have to play with the triggeredBy parameter of the when block: https://jenkins.io/doc/book/pipeline/syntax/

But I cannot find which triggeredBy map a manual launch event (event that is sent when we click on the run button within Jenkins interface).

Upvotes: 2

Views: 3742

Answers (2)

tetienne
tetienne

Reputation: 379

Thx for your help. The following code behaves as expected.

stage("Test") {
            when {allOf {branch "master"; triggeredBy 'UserIdCause'}}
            steps {
              sh 'pipenv run pytest -n5 --dist=loadscope --junitxml report.xml '
            }
            post {
              always {
                junit 'report.xml'
              }
            }
        }

Upvotes: 7

Dibakar Aditya
Dibakar Aditya

Reputation: 4203

You can use this:

stage('Test') {
    when {
        expression {
            currentBuild.buildCauses.toString().contains('UserIdCause')
        }
    }
    steps {
         sh 'pipenv run pytest -n5 --dist=loadscope --junitxml report.xml
    }

}

Upvotes: 0

Related Questions