小编典典

在Jenkins上使用Groovy配置Amazon-ecs从属插件

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

但是,不确定如何继续处理子类 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>

提前致谢。


阅读 352

收藏
2020-07-25

共1个答案

小编典典

所以我在这方面取得了一些进展。它不是幂等的,但是可以。该代码是针对我的用例量身定制的,但对于您而言,自己进行调整也不应该太难。

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()
2020-07-25