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}"
}
}
}
}
}
}