Iulian
Iulian

Reputation: 319

Jenkins DSL pipeline issue

So I've got this groovy script that supposed to seed my DSL jobs but I am not sure what I am doing wrong when fetching the credentials and assigning them to env vars:

for(job in product_base_jobs) {
pipelineJob("${job}") {
    definition{
        cpsScm {
            scm {
                git {
                    branches('staging')
                    remote {
                        credentials('jenkins-git')
                        url("[email protected]/${job}.git")
                    }
                }
            }
            scriptPath("./Jenkinsfile")
        }
        triggers {
        }
    }
    wrappers{
        colorizeOutput()
        timestamps()
        credentialsBinding{
            usernamePassword('ARTIFACTORY_USER','ARTIFACTORY_PASS','JenkinsArtifactoryCredentials')
            usernamePassword('ACCESS_KEY','SECRET_KEY','Jenkins_S3')
        }
    }
    environmentVariables{
        env('ARTIFACTORY_USER',"${ARTIFACTORY_USER}")
        env('ARTIFACTORY_PASS',"${ARTIFACTORY_PASS}")
        env('ACCESS_KEY',"${ACCESS_KEY}")
        env('SECRET_KEY',"${SECRET_KEY}")
    }
}

}

I also tried to use another approach unsuccessfully:

        credentialsBinding{
            usernamePassword{
                usernameVariable('ARTIFACTORY_USER')
                passwordVariable('ARTIFACTORY_PASS')
                credentialsId('JenkinsArtifactoryCredentials')
            }
            usernamePassword{
                usernameVariable('ACCESS_KEY')
                passwordVariable('SECRET_KEY')
                credentialsId('Jenkins_S3')
            }
        }
        environmentVariables{
            env('ARTIFACTORY_USER',"${ARTIFACTORY_USER}")
            env('ARTIFACTORY_PASS',"${ARTIFACTORY_PASS}")
            env('ACCESS_KEY',"${ACCESS_KEY}")
            env('SECRET_KEY',"${SECRET_KEY}")
        }
    }

Here is the error I am getting:

Processing DSL script jobs.groovy
Warning: (jobs.groovy, line 30) acceptMergeRequestOnSuccess is deprecated
Warning: (jobs.groovy, line 34) addNoteOnMergeRequest is deprecated
Warning: (jobs.groovy, line 35) addVoteOnMergeRequest is deprecated
Warning: (jobs.groovy, line 36) acceptMergeRequestOnSuccess is deprecated
ERROR: (jobs.groovy, line 50) No such property: ARTIFACTORY_USER for class: javaposse.jobdsl.dsl.helpers.toplevel.EnvironmentVariableContext
Finished: FAILURE

Is there a bug that I am dealing with or is it just a wrong approach. Normally I would use the WitchCredentials as part of a scripted fashion but I want to consolidate a number of jobs that have a lot of similarities in common.

Upvotes: 0

Views: 1181

Answers (1)

daspilker
daspilker

Reputation: 8194

The Pipeline job type does not support wrappers and environmentVariables. It's an issue in Job DSL that these methods are available, see JENKINS-31832.

Instead of using Job DSL to add the credentials, use Pipeline code in your Jenkinsfile to access any credentials. See Credentials Binding:

node {
  withCredentials([usernameColonPassword(credentialsId: 'mylogin', variable: 'USERPASS')]) {
    sh '''
      set +x
      curl -u $USERPASS https://private.server/ > output
    '''
  }
}

Upvotes: 1

Related Questions