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}"
                    }
                }
            }
        }
    }
}
相关推荐
特长腿特长16 小时前
centos、ubantu系列机的用户和用户组的结构是什么?具体怎么配置?用户组权限怎么使用?这篇文章持续更新,帮助你复习linux的基础知识
linux·运维·centos
zzzyyy53816 小时前
Linux环境变量
linux·运维·服务器
赛博云推-Twitter热门霸屏工具17 小时前
Twitter运营完整流程:从0到引流获客全流程拆解(2026)
运维·安全·自动化·媒体·twitter
CHHC188017 小时前
NetCore树莓派桌面应用程序
linux·运维·服务器
帮我吧智能服务平台18 小时前
装备制造智能制造升级:远程运维与智能服务如何保障产线OEE
运维·服务器·制造
w61001046618 小时前
cka-2026-cri-dockerd
运维·k8s·cka
卤炖阑尾炎19 小时前
PostgreSQL 日常运维全指南:从基础操作到备份恢复
运维·数据库·postgresql
handsomestWei19 小时前
Docker引擎API接入配置
运维·http·docker·容器·api
Tingjct19 小时前
Linux常用指令
linux·运维·服务器
daad77720 小时前
wifi_note
运维·服务器·数据库