AyKarsi
AyKarsi

Reputation: 9685

Run Jenkinsfile with hardcoded parameters

I have a jenkinsfile which builds and deploys to different environments depending on a parameter e.g.

    parameters {
        string(defaultValue: "integration", description: '', name: 'TARGET_ENV')
    }    

When triggering the job, the user is currently asked, which environment to deploy to. I would like to setup one job-item per environment, which does not ask the user to enter the env.

What is the best way to achieve this?

Upvotes: 1

Views: 1775

Answers (2)

snukone
snukone

Reputation: 332

You could create one workflow (scripted) pipeline job for each environment und call your actual job with the set parameter:

Pipeline Script for Workflow Job 'deployEnvDev':

node {
   stage("Deploy Environment Dev") {
      build job: 'here/the/path/to/your/deploy/job',
      wait: true,
        parameters: [
            string(name: 'TARGET_ENV', value: 'dev' )
        ]
    }
}

Pipeline Script for Workflow Job 'deployEnvProd':

node {
   stage("Deploy Environment Prod") {
      build job: 'here/the/path/to/your/deploy/job',
      wait: true,
        parameters: [
            string(name: 'TARGET_ENV', value: 'prod' )
        ]
    }
}

The path 'here/the/path/to/your/deploy/job' to your deploy-job is shown on the detail page of the deploy-job:

enter image description here

This setup enables you to have different jobs to start the deployment for specific environments without asking the user to set the environment manually. The deployment itself is done in the same job as in the past. So you have a central job for modifications or maintenance.

Upvotes: 2

ozlevka
ozlevka

Reputation: 2156

Configure parameter in your pipeline UI.

enter image description here

Add to a script:

parameters {
        string(defaultValue: "integration", description: '', name: 'TARGET_ENV')
    } 

And use in code:

echo "Parameter: ${TARGET_ENV}"

Upvotes: 0

Related Questions