Environment环境变量


environment{…},使用键值对来定义一些环境变量并赋值。它的作用范围,取决environment{…}所写的位置。

  • 写在顶层环境变量,可以让所有stage下的step共享这些变量
  • 也可以单独定义在某一个stage下,只能供这个stage去调用变量,其他的stage不能共享这些变量。

一般来说,我们基本上上定义全局环境变量,如果是局部环境变量,我们直接用def关键字声明就可以,没必要放environment{…}里面。

同时,environment{…}支持credentials() 方法来访问预先在Jenkins保存的凭据,并赋值给环境变量

credentials() 支持的凭据类型:

  • Secret Text

  • Secret File

  • Username and password:使用变量名_USR and 变量名_PSW 来获取其中的用户名和Password

pipeline {
    agent any
    stages {
        stage('Example Username/Password') {
            environment {
                SERVICE_CREDS = credentials('010203c9-b0a5-4101-8a24-c2c8ed13de66')
            }
            steps {
                sh 'echo "Service user is $SERVICE_CREDS_USR"'
                sh 'echo "Service password is $SERVICE_CREDS_PSW"'
                sh 'curl -u $SERVICE_CREDS https://myservice.example.com'
            }
        }
    }
}

SSH with Private Key

pipeline {
    agent any
    stages {
        stage('Example Username/Password') {
            environment {
                SSH_CREDS = credentials('010203c9-b0a5-4101-8a24-c2c8ed13de66')
            }
            steps {
                sh 'echo "SSH private key is located at $SSH_CREDS"'
                sh 'echo "SSH user is $SSH_CREDS_USR"'
                sh 'echo "SSH passphrase is $SSH_CREDS_PSW"'
            }
        }
    }
}

SSH with Private Key

pipeline {
    agent any
    stages {
        stage('Example Username/Password') {
            environment {
                SSH_CREDS = credentials('my-prefined-ssh-creds')
            }
            steps {
                sh 'echo "SSH private key is located at $SSH_CREDS"'
                sh 'echo "SSH user is $SSH_CREDS_USR"'
                sh 'echo "SSH passphrase is $SSH_CREDS_PSW"'
            }
        }
    }
}

 

 

 

 

环境 environment


environment指令指定一系列键值对,这些键值对将被定义为所有step或特定stage的step的环境变量,具体取决于environment指令位于Pipeline中的位置。

该指令支持一种特殊的助手方法credentials(),可以通过Jenkins环境中的标识符来访问预定义的凭据。对于类型为“Secret Text”的凭据,该 credentials()方法将确保环境变量中包含该Secret Text内容。对于“标准用户名和密码”类型的凭证,指定的环境变量将被设置为username:password并且将自动定义两个附加的环境变量:MYVARNAME_USRMYVARNAME_PSW

是否必填
参数没有
允许出现在pipeline块内或stage
例如:
Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    environment { 
        CC = 'clang'
    }
    stages {
        stage('Example') {
            environment { 
                AN_ACCESS_KEY = credentials('my-prefined-secret-text') 
            }
            steps {
                sh 'printenv'
            }
        }
    }
}
  1. environment顶级pipeline块中使用的指令将适用于Pipeline中的所有步骤
  2. 在一个stage中定义的environment指令只将给定的环境变量应用于该stage中的步骤
  3. environment具有一个帮助方法credentials(),可用于在Jenkins环境中通过其标识符访问预定义的凭据

 

 

env 构建时变量


 之前讲的是全局变量,现在是构建时候自己定义所运行的一些变量

  • 定义: 通过键值对(k-v)格式定义流水线在运行时的环境变量, 分为流水线级别和阶段级别。(可以使用enviorment语句块里面去写我们的变量,不需要加逗号分割,每行就是一个变量)

流水线级别环境变量参考:(全局变量,在pipeline下面定义的语句块,在这里面的变量在下面所有的阶段都可以去使用)

pipeline {
    environment {
        NAME = "zeyang"
        VERSION = "1.1.10"
        ENVTYPE = "DEV"
    }
}

阶段级别环境变量参考:(局部变量,在某个阶段里面定义的变量,那么只能在某个阶段去使用

pipeline {
    ...
    ...
    stages {
        stage("build"){
            environment {              
              VERSION = "1.1.20"
            }
            steps {
                script {
                    echo "${VERSION}"
                }
            }
        }
    }
}

 全局变量


pipeline {
    agent any

    environment{
      VERSION = "1.1.1"
      ENV_TYPE = "DEV"
    }

    stages {
        stage('Hello') {
            steps {
                script{
                    echo "The variable version is ${VERSION}"
                    echo "The variable env_type is ${ENV_TYPE}"
                }
            }
   
        }
    }

}


Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/pipeline-test
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
The variable version is 1.1.1
[Pipeline] echo
The variable env_type is DEV
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

这里是在stage里面定义了变量,也就是局部变量 

pipeline {
    agent any

    environment{
      VERSION = "1.1.1"
      ENV_TYPE = "DEV"
    }

    stages {
        stage('Hello') {
        
            environment{
                VERSION="1.0.0"
            }

            steps {
                script{
                    echo "The variable version is ${VERSION}"
                    echo "The variable env_type is ${ENV_TYPE}"
                }
            }
   
        }
    }

}


Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/pipeline-test
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] withEnv
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] echo
The variable version is 1.0.0
[Pipeline] echo
The variable env_type is DEV
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

全局变量(env.JOB_NAME 这个是jenkins内置的全局变量,env.是自定义的全局环境变量,注意不要覆盖了Jenkins内置的全局变量)

pipeline {
    agent any

    environment{
      VERSION = "1.1.1"
      ENV_TYPE = "DEV"
    }

    stages {
        stage('Hello') {
        
            environment{
                VERSION="1.0.0"
            }

            steps {
                script{
                    echo "The variable version is ${VERSION}"
                    echo "The variable env_type is ${ENV_TYPE}"
                    echo "The job name is ${env.JOB_NAME}" //内置的环境变量
                    env.JOB_NAMEA="mytest-pipeline"   //自定义的全局变量,也就是整个流水线可以去使用
                    echo "the variable env.JOB_NAMEA is ${env.JOB_NAMEA}"
                }
            }
   
        }
    }

}



Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/pipeline-test
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage (hide)
[Pipeline] { (Hello)
[Pipeline] withEnv
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] echo
The variable version is 1.0.0
[Pipeline] echo
The variable env_type is DEV
[Pipeline] echo
The job name is pipeline-test
[Pipeline] echo
the variable env.JOB_NAMEA is mytest-pipeline
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐