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}"
                    }
                }
            }
        }
    }
}
相关推荐
初听于你1 分钟前
运维高级故障排除与恢复-SysRq
运维·服务器·安全
RPA机器人就用八爪鱼32 分钟前
RPA自动化程序:企业数字化转型的智能引擎
运维·自动化·rpa
demodashi6661 小时前
Linux下ag搜索命令详解
linux·运维·windows
無识1 小时前
Linux-第四章web服务
linux·运维·服务器
fie88891 小时前
在CentOS 7上集成cJSON库的方法
linux·运维·centos
爱吃橘的橘猫2 小时前
如何解决VMware虚拟机中Linux系统终端不显示ens33 inet IP地址的问题
linux·运维·服务器·虚拟机
佐杰2 小时前
Jenkins使用指南1
java·运维·jenkins
IT小哥哥呀2 小时前
Jenkins + Docker 打造自动化持续部署流水线
docker·微服务·自动化·jenkins·springboot·高并发·限流
kyle~2 小时前
计算机系统---USB的四种传输方式
运维·c++·计算机系统
梁正雄2 小时前
linux服务-Bonding网卡绑定工具
linux·运维·linux bonding·网卡绑定