我正在尝试从应并行运行的管道作业之一创建多个动态作业。
我希望我的詹金斯管道脚本根据用户输入下载并安装我的软件二进制文件。以下是我的示例阶段:
- 第 1 阶段:将下载构建版本
- 第 2 阶段:获取参数并安装软件的“云”部分
- 第 3 阶段:将接受用户输入“需要部署多少条边”(其中边缘是我构建的第二个组成部分)并根据边数创建动态作业。如果边数为 2,则应在运行时创建 2 个作业。如果边数为 3,则运行时应创建 3 个作业
以下是我设法创建的示例脚本
def SETUP_DIR = ""
Map<String, ?> myMap = [:]
Map<String, List<String>> minionMap = [:]
pipeline {
agent any
parameters {
string(name: 'RELEASE_VERSION', defaultValue: '1.0.0', description: 'Define build to use')
string(name: 'Cloud_IP', defaultValue: 'xxx.xx.x.xx', description: 'Central IP')
choice(name: 'No_Of_Edges', choices: ['1', '2', '3', '4', '5'], description: 'Total Edges to Deploy')
}
stages {
stage('Identify Installation Parameters') {
steps {
script {
// Retrieve the parameter values
def RELEASE_VERSION = params.RELEASE_VERSION
def Cloud_IP = params.Cloud_IP
def No_Of_Edges = params.No_Of_Edges
}
}
}
stage('Download Build') {
steps {
script {
def dirname = sh(returnStdout: true, script: 'date +"%d-%m-%Y_%H:%M:%S"').trim()
sh """
sudo mkdir -p /root/Automation/${dirname}
echo $dirname
SETUP_DIR=$dirname
cd /root/Automation/${SETUP_DIR}/
VER=${RELEASE_VERSION}
echo \${VER}
sudo curl -u cicd:cicd -X GET url\${VER}.tar.gz --output installer.tar.gz
sleep 60
sudo tar -vxf installer.tar.gz
sleep 30
sudo rm -rf installer.tar.gz
sleep 5
sudo chmod 777 installer
cd installer
<some-tasks>
"""
}
}
}
stage('Install Cloud') {
steps {
script {
echo "Installing IEAP Central"
printf "\n \n Updating all files \n \n"
sh """
<some-tasks>
printf "\n \n Deployed cloud successfully \n \n"
"""
}
}
}
stage('Deploy Edge Cluster') {
steps {
script {
echo "Installing Edge Control"
def loopCount = No_Of_Edges.toInteger()
for (int i = 1; i <= loopCount; i++) {
def paramInput = input(
id: "paramInput-${i}",
message: "Enter Edge${i} Value",
parameters: [
string(name: "Edge_Control_IP_${i}", defaultValue: '10.139.9.178', description: "Edge Control IP"),
]
)
def jobName = "Dynamic-Job-${i}"
def jenkinsfileContent = generateJenkinsfileContent(jobName, paramInput)
// Create dynamic pipeline job
createPipelineJob(jobName, jenkinsfileContent)
}
}
}
}
}
}
def generateJenkinsfileContent(String jobName, Map<String, String> params) {
def edgeControlIp = params["Edge_Control_IP"]
return """
pipeline {
agent any
stages {
stage('Dynamic Job') {
steps {
script {
echo "This is the dynamic job ${jobName}"
echo "Edge Control IP: ${edgeControlIp}"
}
}
}
}
}
"""
}
@NonCPS
def createPipelineJob(String jobName, String jenkinsfileContent) {
def jobFolder = "Installation" // Folder to store dynamic jobs
def jobFullName = "${jobFolder}/${jobName}" // Full name of the dynamic job
// Create or update the Jenkinsfile for the dynamic job
writeFile file: "${jobFullName}/Jenkinsfile", text: jenkinsfileContent
// Trigger the dynamic job by loading the Jenkinsfile
build job: "${jobFullName}"
}
收到的输出:
预计会调用 WorkflowScript.createPipelineJob 但最终捕获 writeFile;看:https://jenkins.io/redirect/pipeline-cps-method-mismatches/ https://jenkins.io/redirect/pipeline-cps-method-mismatches/