远程主机的 Jenkins 任务
Jenkins task for remote hosts
在部署场景中,我需要在主机列表上创建和 运行 jenkins 任务,即创建类似参数化任务(其中 ip 地址是参数)或在 Multijob Plugin 上使用 HOST 创建任务轴,但 运行 在多个主机上只有 2 个并行。
一种选择是 运行 ansible 主机列表,但我想分别查看每个主机的状态,并在需要时重新启动 jenkins 作业。
主要选项是使用 Job DSL Plugin or Pipeline Plugin,但在这里我需要帮助来理解应该使用什么 classes/methods of dsl groovy 代码来实现这一点。
有人可以帮忙吗?
假设主机已经配置为Jenkins slaves。
假设在管道作业参数中提供了主机
HOSTS
作为空格分隔的列表。以下示例应该可以帮助您入门:
def hosts_pairs = HOSTS.split().collate(2)
for (pair in host_pairs) {
def branches = [:]
for (h in pair) {
def host = h // fresh variable per iteration; it will be mutated
branches[host] = {
stage(host) {
node(host) {
// do the actual job here, e.g.
// execute a shell script
sh "echo hello world"
}
}
}
}
parallel branches
}
Matrix project and Throttle Concurrent Builds Plugin 的组合是可能的。
您只需设置一个用户定义的轴(例如 "targetHost"),将所有 IP 地址作为值,并在 "Throttle Concurrent Builds" 下设置所需的节流(请注意,您必须启用"Execute concurrent builds if necessary" 选项告诉 jenkins 允许并发执行)。
轴值在相应环境变量中的每个子构建期间可用(例如 targetHost
)。
下面是一个示例 config.xml,其中包含简单的 ping&wait 构建步骤:
<?xml version='1.0' encoding='UTF-8'?>
<matrix-project plugin="matrix-project@1.7.1">
<actions/>
<description></description>
<keepDependencies>false</keepDependencies>
<properties>
<hudson.plugins.throttleconcurrents.ThrottleJobProperty plugin="throttle-concurrents@1.9.0">
<maxConcurrentPerNode>2</maxConcurrentPerNode>
<maxConcurrentTotal>2</maxConcurrentTotal>
<categories class="java.util.concurrent.CopyOnWriteArrayList"/>
<throttleEnabled>true</throttleEnabled>
<throttleOption>project</throttleOption>
<limitOneJobWithMatchingParams>false</limitOneJobWithMatchingParams>
<matrixOptions>
<throttleMatrixBuilds>true</throttleMatrixBuilds>
<throttleMatrixConfigurations>true</throttleMatrixConfigurations>
</matrixOptions>
<paramsToUseForLimit></paramsToUseForLimit>
</hudson.plugins.throttleconcurrents.ThrottleJobProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>true</concurrentBuild>
<axes>
<hudson.matrix.TextAxis>
<name>targetHost</name>
<values>
<string>127.0.0.1</string>
<string>127.0.0.2</string>
<string>127.0.0.3</string>
<string>127.0.0.4</string>
<string>127.0.0.5</string>
</values>
</hudson.matrix.TextAxis>
</axes>
<builders>
<hudson.tasks.Shell>
<command>sleep 7
ping -c 7 $targetHost
sleep 7</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
<executionStrategy class="hudson.matrix.DefaultMatrixExecutionStrategyImpl">
<runSequentially>false</runSequentially>
</executionStrategy>
</matrix-project>
祝你好运!
在部署场景中,我需要在主机列表上创建和 运行 jenkins 任务,即创建类似参数化任务(其中 ip 地址是参数)或在 Multijob Plugin 上使用 HOST 创建任务轴,但 运行 在多个主机上只有 2 个并行。
一种选择是 运行 ansible 主机列表,但我想分别查看每个主机的状态,并在需要时重新启动 jenkins 作业。
主要选项是使用 Job DSL Plugin or Pipeline Plugin,但在这里我需要帮助来理解应该使用什么 classes/methods of dsl groovy 代码来实现这一点。
有人可以帮忙吗?
假设主机已经配置为Jenkins slaves。
假设在管道作业参数中提供了主机
HOSTS
作为空格分隔的列表。以下示例应该可以帮助您入门:
def hosts_pairs = HOSTS.split().collate(2)
for (pair in host_pairs) {
def branches = [:]
for (h in pair) {
def host = h // fresh variable per iteration; it will be mutated
branches[host] = {
stage(host) {
node(host) {
// do the actual job here, e.g.
// execute a shell script
sh "echo hello world"
}
}
}
}
parallel branches
}
Matrix project and Throttle Concurrent Builds Plugin 的组合是可能的。
您只需设置一个用户定义的轴(例如 "targetHost"),将所有 IP 地址作为值,并在 "Throttle Concurrent Builds" 下设置所需的节流(请注意,您必须启用"Execute concurrent builds if necessary" 选项告诉 jenkins 允许并发执行)。
轴值在相应环境变量中的每个子构建期间可用(例如 targetHost
)。
下面是一个示例 config.xml,其中包含简单的 ping&wait 构建步骤:
<?xml version='1.0' encoding='UTF-8'?>
<matrix-project plugin="matrix-project@1.7.1">
<actions/>
<description></description>
<keepDependencies>false</keepDependencies>
<properties>
<hudson.plugins.throttleconcurrents.ThrottleJobProperty plugin="throttle-concurrents@1.9.0">
<maxConcurrentPerNode>2</maxConcurrentPerNode>
<maxConcurrentTotal>2</maxConcurrentTotal>
<categories class="java.util.concurrent.CopyOnWriteArrayList"/>
<throttleEnabled>true</throttleEnabled>
<throttleOption>project</throttleOption>
<limitOneJobWithMatchingParams>false</limitOneJobWithMatchingParams>
<matrixOptions>
<throttleMatrixBuilds>true</throttleMatrixBuilds>
<throttleMatrixConfigurations>true</throttleMatrixConfigurations>
</matrixOptions>
<paramsToUseForLimit></paramsToUseForLimit>
</hudson.plugins.throttleconcurrents.ThrottleJobProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>true</concurrentBuild>
<axes>
<hudson.matrix.TextAxis>
<name>targetHost</name>
<values>
<string>127.0.0.1</string>
<string>127.0.0.2</string>
<string>127.0.0.3</string>
<string>127.0.0.4</string>
<string>127.0.0.5</string>
</values>
</hudson.matrix.TextAxis>
</axes>
<builders>
<hudson.tasks.Shell>
<command>sleep 7
ping -c 7 $targetHost
sleep 7</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
<executionStrategy class="hudson.matrix.DefaultMatrixExecutionStrategyImpl">
<runSequentially>false</runSequentially>
</executionStrategy>
</matrix-project>
祝你好运!