Jenkins中使用环境变量

直接使用环境变量

groovy 复制代码
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:

bash 复制代码
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中赋值给新的环境变量,然后后文中同样可以使用。

groovy 复制代码
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:

bash 复制代码
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

一个好的教程链接

相关推荐
LinuxSuRen15 小时前
Jenkins 命令行多线程并发下载制品包
运维·jenkins
神的孩子都在歌唱1 天前
es创建的索引状态一直是red
大数据·elasticsearch·jenkins
万万君2 天前
Linux 更改Jenkins使用其他账户启动
linux·jenkins
赤叶丶秋枫2 天前
Jenkins入门使用
运维·jenkins
紫菜(Nori)2 天前
Jenkins Api Token 访问问题
运维·jenkins
嘤嘤怪呆呆狗2 天前
【开发问题记录】使用 Docker+Jenkins+Jenkins + gitee 实现自动化部署前端项目 CI/CD(centos7为例)
前端·vue.js·ci/cd·docker·gitee·自动化·jenkins
张某人想退休2 天前
Postman最新接口自动化持续集成
自动化·jenkins·postman
lifeng43212 天前
Jenkins集成部署(图文教程、超级详细)
运维·jenkins
无所不在的物质2 天前
Jenkins基础教程
运维·云原生·自动化·jenkins
喝醉酒的小白3 天前
ElasticSearch 的核心功能
大数据·elasticsearch·jenkins