Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-71002

Ability to modify SlaveTemplates programmatically

XMLWordPrintable

      What is the recommended way to modify list of templates in AmazonEC2Cloud instance?

      Because the `templates` field is private, there is no easy way to load and modify the could configuration using Jenkins Groovy scripts.

      One way is to use reflection to access the field and modify the list (from https://stackoverflow.com/a/49042851/1538920):

       

      import hudson.model.*
      import hudson.plugins.ec2.EC2Cloud
      import hudson.plugins.ec2.SlaveTemplate
      import jenkins.model.Jenkins
      import java.lang.reflect.Field
      
      def jenkins = Jenkins.get()
      def newTemplate = new SlaveTemplate(...)
      def cloud = jenkins.clouds.getByName('ec2-cloud')
      Field field = EC2Cloud.class.getDeclaredField('templates')
      field.setAccessible(true)
      def templates = (List) field.get(cloud)
      templates.add(newTemplate)
      jenkins.save() 

       

      The other would be to create a full copy of the AmazonEC2Cloud and remove the old one.

      Is there a better way to do that and if not, could it be implemented in the plugin?

            thoulen FABRIZIO MANFREDI
            piomar123 Piotr Marcińczyk
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: