我试图从一个应该并行运行的管道作业中创建多个动态作业。我想我的jenkins管道脚本下载和安装我的软件二进制基于用户输入。下面是我的示例阶段:
1.阶段1:将下载构建
1.阶段2:获取参数并安装我软件的“云”部分
1.阶段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/
1条答案
按热度按时间ggazkfy81#
为了更清楚起见,我在这里使用了示例而不是注解。下面是如何在管道中使用jenkins cli:下载cli,下载现有作业并将规范发送回Jenkins以创建具有不同名称的相同作业。这是一个很好的起点,可以修改规范xml,以创建所需的作业或做其他需要的事情。
字符串