Jenkins Pipeline(二)

1.Pipeline 变量

在 Jenkins 管道(Pipeline)中,变量是一种非常有用的功能,它们可以帮助你在构建过程中存储和传递数据。Jenkins 管道支持多种方式来定义和使用变量,包括环境变量脚本变量 以及全局变量。

1.2 脚本变量

在 pipeline 脚本中,你可以使用 Groovy 脚本来定义和操作变量。

复制代码
pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                script {
                    def myVar = 'anotherValue'
                    echo "My Variable: ${myVar}"
                }
            }
        }
    }
}

1.3 全局变量

Jenkins Pipeline 支持使用一些预定义的全局变量,例如 env 对象,它包含了环境变量和一些 Jenkins 特有的变量。

复制代码
pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo "Build Number: ${env.BUILD_NUMBER}"
                echo "Job Name: ${env.JOB_NAME}"
            }
        }
    }
}

1.3环境变量

环境变量可以在 Jenkins 的"系统管理"->"系统配置"中设置,也可以在 pipeline 脚本中使用 env 指令定义。

复制代码
pipeline {
    agent any
    environment {
        MY_VAR = 'someValue'
    }
    stages {
        stage('Example') {
            steps {
                echo "My Variable: ${env.MY_VAR}"
            }
        }
        stage('Build') {
            steps {
                script {
                    def result = sh(script: 'echo "Hello, Jenkins!"', returnStdout: true).trim()
                    env.MY_VARIABLE = result
                }
            }
        }
        stage('Test') {
            steps {
                sh 'echo $MY_VARIABLE'
            }
        }
    }
}

2. Pipeline 读取pom

2.1 使用readMavenPom

使用 readMavenPom需要安装Pipeline Maven Integration Plugin插件。

复制代码
pipeline {
    agent any
 
    stages {
        stage('Read POM') {
            steps {
                script {
                    // 读取 pom.xml 文件
                    def pom = readMavenPom file: 'pom.xml'
                    // 打印一些信息,例如项目版本和依赖项
                    echo "Project Version: ${pom.version}"
                    echo "Project Group: ${pom.groupId}"
                    echo "Project Artifact: ${pom.artifactId}"
                    echo "Dependencies:"
                    pom.dependencies.each { dep ->
                        echo "${dep.groupId}:${dep.artifactId}:${dep.version}"
                    }
                }
            }
        }
    }
}
相关推荐
艾莉丝努力练剑6 小时前
【Linux:文件】Ext系列文件系统(初阶)
大数据·linux·运维·服务器·c++·人工智能·算法
小天源7 小时前
Cacti在Debian/Ubuntu中安装及其使用
运维·ubuntu·debian·cacti
Trouvaille ~7 小时前
【Linux】TCP Socket编程实战(一):API详解与单连接Echo Server
linux·运维·服务器·网络·c++·tcp/ip·socket
芷栀夏7 小时前
深度解析 CANN 异构计算架构:基于 ACL API 的算子调用实战
运维·人工智能·开源·cann
全栈工程师修炼指南7 小时前
Nginx | stream 四层反向代理:SSL、PREREAD 阶段模块指令浅析与实践
运维·网络·网络协议·nginx·ssl
人间打气筒(Ada)8 小时前
jenkins基于Pipeline发布项目
java·pipeline·jenkins·流水线·ci·cd·cicd
威迪斯特8 小时前
CentOS图形化操作界面:理论解析与实践指南
linux·运维·centos·组件·图形化·桌面·xserver
一方热衷.8 小时前
在线安装对应版本NVIDIA驱动
linux·运维·服务器
独自归家的兔8 小时前
ubuntu系统安装dbswitch教程 - 备份本地数据到远程服务器
linux·运维·ubuntu
ONE_SIX_MIX8 小时前
ubuntu 24.04 用rdp连接,桌面黑屏问题,解决
linux·运维·ubuntu