Skip to content

Instantly share code, notes, and snippets.

@apolloclark
Created January 31, 2019 17:07
Show Gist options
  • 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