Skip to content

Instantly share code, notes, and snippets.

@apolloclark
Created January 31, 2019 17:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save apolloclark/042faf1475abb08c22a4dd6cee80ed5d to your computer and use it in GitHub Desktop.
Save apolloclark/042faf1475abb08c22a4dd6cee80ed5d to your computer and use it in GitHub Desktop.
Jenkins credentials-plugin example script
# run jenkins/jenkins:lts, with volumes, exposed ports
docker run -d -v jenkins_home:/var/jenkins_home \
  -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts
  
# Jenkins Pipeline script, using Credentials

pipeline {
    agent any

	parameters {
         credentials(
             name: 'param_secret',
             description: 'Build parameter secret.',
             defaultValue: 'default_value',
             credentialType: 'Build parameter secret.',
             required: true
        )
    }

    environment {
    	ENV_SECRET = credentials('global_secret') 
    }

    stages {
        stage('Param Secret') {
            steps {
                echo "param_secret = ${params.param_secret}"
            }
        }
        stage('Global Secret') {
            steps {
                echo "env_secret = $ENV_SECRET"
            }
        }
        stage('Stage Secret') {
            environment { 
                STAGE_SECRET = credentials('global_secret') 
            }
            steps {
                echo "stage_secret =  $STAGE_SECRET"
            }
        }
    }
}

References

https://jenkins.io/doc/book/pipeline/jenkinsfile/#handling-credentials

https://jenkins.io/doc/pipeline/steps/credentials-binding/

https://jenkins.io/doc/book/pipeline/syntax/#parameters

https://github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Deployment#deploying-a-serverless-application

https://github.com/jcsou/credentials-plugin/blob/user-scoped-creds/UserCredTest/TestSshJob1.jenkinsfile

https://github.com/jcsou/credentials-plugin/blob/user-scoped-creds/UserCredTest/TestSshJob2.jenkinsfile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment