在 Jenkins 上使用 Groovy 配置 amazon-ecs slave 插件

Configure amazon-ecs slave plugin using Groovy on Jenkins

我正在尝试使用 init.groovy 脚本为 Jenkins 配置 amazon-ecs-plugin,但找不到相关文档。我是 groovy 基于配置自动化的新手

尝试使用

获取所有属性
import jenkins.model.*
import com.cloudbees.jenkins.plugins.amazonecs.*
ECSCloud.metaClass.properties.each {println it.name+":\t"+it.type }

输出:

regionName:           class java.lang.String
searchName:           class java.lang.String
slaveTimoutInSeconds: int
searchIndex:          interface hudson.search.SearchIndex
ACL:                  class hudson.security.ACL
descriptor:           class hudson.model.Descriptor
credentialsId:        class java.lang.String
search:               class hudson.search.Search
ecsService:           class com.cloudbees.jenkins.plugins.amazonecs.ECSService
class:                class java.lang.Class
searchUrl:            class java.lang.String
tunnel:               class java.lang.String
templates:            interface java.util.List
cluster:              class java.lang.String
jenkinsUrl:           class java.lang.String
amazonECSClient:      class com.amazonaws.services.ecs.AmazonECSClient
displayName:          class java.lang.String

但是,不确定如何继续子类 like ecsService: class com.cloudbees.jenkins.plugins.amazonecs.ECSService

不确定,如何定义 属性

def ecs-cloud = new ECSCloud(
  regionName="String"
  ecsService="<NOT SURE HOT TO CONFIGURE THIS>"
......
)

手动配置后的.xml文件看起来像

<clouds>
    <com.cloudbees.jenkins.plugins.amazonecs.ECSCloud plugin="scalable-amazon-ecs@1.0">
      <name>ECS-CLUSTER-NAME</name>
      <templates>
        <com.cloudbees.jenkins.plugins.amazonecs.ECSTaskTemplate>
          <label>jnlp-slave</label>
          <image>jenkinsci/jnlp-slave</image>
          <remoteFSRoot>/home/jenkins</remoteFSRoot>
          <memory>800</memory>
          <cpu>800</cpu>
          <privileged>false</privileged>
          <taskDefinitionArn>TASK-DEF-ARN</taskDefinitionArn>
        </com.cloudbees.jenkins.plugins.amazonecs.ECSTaskTemplate>
      </templates>
      <credentialsId></credentialsId>
      <cluster>arn:aws:ecs:REGION:ACCOUNTID:cluster/ECS-CLUSTER-NAME</cluster>
      <regionName>REGION</regionName>
      <tunnel></tunnel>
      <jenkinsUrl>JENKINS-URL</jenkinsUrl>
      <slaveTimoutInSeconds>900</slaveTimoutInSeconds>
      <ecsService>
        <credentialsId></credentialsId>
        <regionName>REGION</regionName>
      </ecsService>
    </com.cloudbees.jenkins.plugins.amazonecs.ECSCloud>
  </clouds>

提前致谢。

更新

使用 EC2 插件的类似问题。

所以我在这方面取得了一些进展。它不是幂等的,但它有效。该代码是为我的用例量身定制的,但您可以根据自己的情况进行调整。

import java.util.Arrays
import java.util.logging.Logger
Logger logger = Logger.getLogger("ecs-cluster")

logger.info("Loading Jenkins")
import jenkins.model.*
instance = Jenkins.getInstance()

import com.cloudbees.jenkins.plugins.amazonecs.ECSTaskTemplate.MountPointEntry
def mounts = Arrays.asList(
  new MountPointEntry(
    name="docker",
    sourcePath="/var/run/docker.sock",
    containerPath="/var/run/docker.sock",
    readOnly=false),
  new MountPointEntry(
    name="jenkins",
    sourcePath="/home/jenkins",
    containerPath="/home/jenkins",
    readOnly=false),
)

logger.info("Creating template")
import com.cloudbees.jenkins.plugins.amazonecs.ECSTaskTemplate
def ecsTemplate = new ECSTaskTemplate(
  templateName="jnlp-slave-with-docker",
  label="ecs-with-docker",
  image="jnlp-slave-with-docker:latest",
  remoteFSRoot=null,
  memory=2048,
  cpu=512,
  privileged=false,
  logDriverOptions=null,
  environments=null,
  extraHosts=null,
  mountPoints=mounts
)

logger.info("Retrieving ecs cloud config by descriptor")
import com.cloudbees.jenkins.plugins.amazonecs.ECSCloud
ecsCloud = new ECSCloud(
  name="name",
  templates=Arrays.asList(ecsTemplate),
  credentialsId=null,
  cluster="arn:aws:ecs:us-east-1:123456789:cluster/ecs-jenkins-slave",
  regionName="us-east-1",
  jenkinsUrl="https://my-jenkins.com",
  slaveTimoutInSeconds=60
)

logger.info("Gettings clouds")
def clouds = instance.clouds
clouds.add(ecsCloud)
logger.info("Saving jenkins")
instance.save()

上述答案中的 ECSTaskTemplate 不再有效。缺少内存保留。

这是 ECSTaskTemplate

的工作示例
def ecsTemplate = new ECSTaskTemplate(
  templateName="jnlp-slave",
  label="ecs",
  image="jenkinsci/jnlp-slave",
  remoteFSRoot=null,
  memory=0,
  memoryReservation=2048,
  cpu=512,
  privileged=false,
  logDriverOptions=null,
  environments=null,
  extraHosts=null,
  mountPoints=null
)