简体   繁体   中英

Parameterisation of a groovy Pipeline for use with jenkins

I have a groovy pipeline which I've inherited from a project that I forked.

I wish to pass in Jenkins Choice Parameters as a Parameterised build. At present, I only wish to expose the environment in which to run ( but will want to parameterise further at a later stage), such that a user can choose it from the Jenkins dropdown and use on demand.

I used the snippet generator to help.

Can someone please help with the syntax? I am using Node with a package.json to run a script and with to pass in either dev or uat :

properties([[$class: 'BuildConfigProjectProperty', name: '', namespace: '', resourceVersion: '', uid: ''], parameters([choice(choices: 'e2e\nuat', description: 'environment ', name: 'env')])])

node('de-no1') {
        try {
            stage('DEV: Initialise') {
            git url: 'https://myrepo.org/mycompany/create-new-user.git', branch: 'master', credentialsId: CREDENTIALS_ID
        }
        stage('DEV: Install Dependencies') {
            sh 'npm install'
        }

        stage('${params.env}: Generate new users') {
            sh 'npm run generate:${params.env}'
            archiveArtifacts artifacts: '{params.env}-userids.txt', fingerprint: true
        }

This currently fails with:

npm ERR! missing script: generate:{params.env}

Assume you want to replace ${params.env} with a value when you call npm?

If this is the case, you need to use double quotes " to let Groovy know you will be doing String templating...ie:

            sh "npm run generate:${params.env}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM