本文为霍格沃兹测试开发学社学员学习笔记分享
原文链接:https://ceshiren.com/t/topic/26711
远程工作目录
主机
全局安全配置
使用k8s和jenkins集成来解决
pipeline{}
pipeline {
agent any // 定义构建代理
environment {
param = 'xx'
}
stages { // 定义阶段
stage('pull source code') {
steps {
echo 'pull source code'
sleep 5
}
}
stage('build'){
steps {
echo 'build source code'
sleep 5
}
}
}
post{ // 定义全局后置处理程序
always {
echo 'say goodbye'
}
}
}
必须出现的指令
参数
any:可以在任意agent上执行pipeline
none:pipeline将不分配全局agent, 每个stage分配自己的agent
label:指定运行节点的Label
node:自定义运行节点配置
docker:控制目标节点上的docker运行相关内容
pipeline {
agent {
label 'master'
customWorkspace 'myWorkspace'
}
}
不是必须出现的指令
参数:
post {
always {
echo 'say always'
}
success {
echo 'say success'
}
failure {
echo 'say failure'
}
}
不是必须出现的指令
参数:
options {
timeout(time: 30, unit: 'SECONDS')
buildDiscarder(logRotator(numToKeepStr: '10'))
retry(5)
}
environment {
PERSON = "Kevin"
}
stages {
stage('pull source code') {
steps {
echo "hello ${PERSON}"
echo PERSON
}
}
不是必须出现的指令
使用 ${变量名} 进行参数化调用
参数:
parameters {
string(name: 'PERSON' ,defaultValue:'Jenkins',description:'输入的文本参数')
}
environment {
PERSON = "$params.PERSON"
}
stages {
stage('pull source code') {
steps {
echo "hello ${PERSON}"
echo 'pull source code'
}
}
不是必须出现的指令
参数:
triggers {
cron('H/2 * * * *')
}
node {
stage('Example') {
if (env.BRANCH_NAME == 'master') {
echo 'I only execute on the master branch'
} else {
echo 'I execute elsewhere'
}
}
}
stage('Test') {
node{
echo "This is test stage which run on the slave agent."
try {
echo "This is in the try block."
sh 'exit 1'
}catch (exc) {
echo "Something failed, I'm in the catch block."
}finally {
echo "Finally, I'm in the finally block."
}
}
}
环境工具变量的定义
Script 代码中引用环境变量,调用java、maven工具
stage('env tools') {
node('master'){
//定义maven java环境
def mvnHome = tool 'Maven 3.9.3'
def jdkHome = tool 'jdk1.8_master'
//引用环境变量,配置PATH变量
env.PATH = "${mvnHome}/bin:${env.PATH}"
env.PATH = "${jdkHome}/bin:${env.PATH}"
//调用java mvn 工具
sh "java -version"
sh "mvn --version"
}
}