Skip to content

Environment variables

Andrew Bayer edited this page Jan 10, 2017 · 6 revisions

Environment variables are set using name/value pairs:

pipeline {
    environment {
        FOO = "BAR"
    }

    agent { label "master" }

    stages {
        stage("foo") {
            steps {
                sh 'echo "FOO is $FOO"'
            }
        }
    }
}

You can add many more variables, one per line, like so:

    environment {
        FOO = "BAR"
        BAZ = "bang"
    }

Environment variables can be set globally (like the above) or per stage. If they are set per stage, they only apply to the running stage. Other stages will not get those environment variables.

You can also access credentials (ie. secrets) and set them to environment variables. For example:

	 environment {
	 		AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID')
			AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY')
	 }

This looks up the secret text for each credential id.

If you use username and password style of credentials, this is slightly different:

environment {
   SAUCE_ACCESS = credentials('sauce-lab-dev')
}

This will actually set 3 variables:

  • SAUCE_ACCESS containing <username>:<password>
  • SAUCE_ACCESS_USR containing the username
  • SAUCE_ACCESS_PSW containing the password