Home > Blockchain >  how to schedule parameterized pipelines to run just once with one parameter and the rest with anothe
how to schedule parameterized pipelines to run just once with one parameter and the rest with anothe

Time:09-22

I have a pipeline which I just added 2 parameters to build release or debug (parameters are called release or debug). The pipeline checks for every commit and then build release (C program) but I would like to build debug once a day, let's say everyday at 12. without me having to run the pipeline and changing the parameter manually (it is set to release by default). Is there any way to do this? This is a very short version of what the pipeline looks like:

    pipeline {
    
        stages {
          stage('Setup parameters') {
            steps {
                script { 
                    properties([
                        parameters([
                            choice(
                                defaultValue: 'RELEASE', 
                                choices: ['RELEASE', 'DEBUG'], 
                                name: 'BUILD_CONFIG'
                            ),
                        ])
                    ])
                }
            }
        }
        stage('Build release'){
            when {
                expression {
                   return params.BUILD_CONFIG == 'RELEASE'
                }
            }
            steps{
                script {
                    def msbuild = tool name: 'MSBuild', type: 'hudson.plugins.msbuild.MsBuildInstallation'
                    bat "\"${msbuild}\" /Source/project-GRDK.sln /t:Rebuild  /p:configuration=\"Release Steam D3D11\""
                }
            }
        }
        stage('Build debug'){
            when {
                expression {
                   return params.BUILD_CONFIG == 'DEBUG'
                }
            }
            steps{
                script {
                    def msbuild = tool name: 'MSBuild', type: 'hudson.plugins.msbuild.MsBuildInstallation'
                    bat "\"${msbuild}\" /Source/project-GRDK.sln /t:Rebuild /p:configuration=\"Debug Steam D3D11\""
                }
            }
        }
        }
    }

CodePudding user response:

It is possible to determine the cause of the build with currentBuild.rawBuild.getCause(Class<T> type). The type you are looking for is UserIdCause. Following would build a stage in case the job was not triggered by an user (manually). In this stage steps are from Build debug stage.

stage('Build debug if time triggered') {
   when {
     expression {
       return currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause) == null
     }
   }
   steps {
     script {
       def msbuild = tool name: 'MSBuild', type: 'hudson.plugins.msbuild.MsBuildInstallation'
       bat "\"${msbuild}\" /Source/project-GRDK.sln /t:Rebuild /p:configuration=\"Debug Steam D3D11\""
            
     }
}

You will also need to add an expression to Build release and Build debug stages, in order to prevent building if the job is not triggered by an user (manually).

    stage('Build release'){
        when {
            allOf {
               expression {
                   return params.BUILD_CONFIG == 'RELEASE'
               }
               expression {
                   return currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause) != null
               }
            }
        }
        ...

Docu:
https://javadoc.jenkins-ci.org/hudson/model/Cause.html
https://javadoc.jenkins-ci.org/hudson/model/Run.html
How to differentiate build triggers in Jenkins Pipeline

CodePudding user response:

Another option would be to create a second job, which triggers the build job with the right parameters:

pipeline {
   agent any

   triggers {
       cron('H 12 * * *')
   }

   stages {
       stage('Build xxx debug') {
           steps {
               build job: "your-job-name-here", parameters: [
                   choice(name: 'BUILD_CONFIG', value: 'DEBUG')
               ]
           }
       }
   }
}
  • Related