Jenkins中使用环境变量

直接使用环境变量


pipeline {
    agent {
        label "${28}"
    }

    stages {
        
        stage("git clone"){
            steps{
                script{
                    sh """
                        pwd
                        ls
                    """
                  
                    // 环境变量的使用
                    // 输出所有环境变量  
                    echo "All environment variables: ${env}"  
                    
                    // 输出单个环境变量的值  
                    echo "HOME directory: ${env.HOME}"  
                    echo "Jenkins user: ${env.USER}"  
                    echo "Jenkins WORKSPACE: ${env.WORKSPACE}"
                    
                    // 遍历输出所有的环境变量  
                    env.each { key, value ->  
                        echo "Environment variable $key: $value"  
                    }
                    
                    // 如下为/home/jenkins/program/jenkins/workspace/middle_nMedia_test
                    echo "${WORKSPACE}"
                    // 构建号
                    echo "${BUILD_NUMBER}"
                    echo "${WORKSPACE}/${BUILD_NUMBER}"
                }
            }
        }
    }
}

Console Output:

Started by user 昌显瑞
[Pipeline] Start of Pipeline
[Pipeline] node
Running on 192.168.3.28 in /home/jenkins/program/jenkins/workspace/middle_nMedia_test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (git clone)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ pwd
/home/jenkins/program/jenkins/workspace/middle_nMedia_test
+ ls
[Pipeline] echo
All environment variables: org.jenkinsci.plugins.workflow.cps.EnvActionImpl@2f5a9fc9
[Pipeline] echo
HOME directory: /home/jenkins
[Pipeline] echo
Jenkins user: jenkins
[Pipeline] echo
Jenkins WORKSPACE: /home/jenkins/program/jenkins/workspace/middle_nMedia_test
[Pipeline] echo
Environment variable org.jenkinsci.plugins.workflow.cps.EnvActionImpl@2f5a9fc9: null
[Pipeline] echo
/home/jenkins/program/jenkins/workspace/middle_nMedia_test
[Pipeline] echo
28
[Pipeline] echo
/home/jenkins/program/jenkins/workspace/middle_nMedia_test/28
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

通过environment间接使用

在environment中定义新的全局的环境变量,在后文中使用。

亦可以将参数化构建的参数在environment中赋值给新的环境变量,然后后文中同样可以使用。

pipeline {
    agent {
        label "28"
    }
    
    environment {
        // 通过环境变量定义新的变量
        work_dir = "${BUILD_NUMBER}"
        server_main_workspace = "${WORKSPACE}/${BUILD_NUMBER}"

    }

    stages {
        stage("git clone"){
            steps{
                script{
                    sh """
                        pwd
                        ls
                    """
                    
                    echo "${work_dir}"
                    echo "${server_main_workspace}"
                }
            }
        }
    }
}

Console Output:

Started by user 昌显瑞
[Pipeline] Start of Pipeline
[Pipeline] node
Running on xxx.xxx.xx.xx in /home/jenkins/program/jenkins/workspace/xxxxxxxxx_test
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (git clone)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ pwd
/home/jenkins/program/jenkins/workspace/xxxxxxxxx_test
+ ls
[Pipeline] echo
29
[Pipeline] echo
/home/jenkins/program/jenkins/workspace/xxxxxxxxx_test/29
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

一个好的教程链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值