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

Cannot set auth token Id programatically

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major Major
    • slack-plugin
    • None

      I have this code which works:

      #!groovy
      import jenkins.model.*
      import hudson.plugins.slack.*
      import hudson.util.Secret
      import com.cloudbees.plugins.credentials.impl.*
      import com.cloudbees.plugins.credentials.*
      import com.cloudbees.plugins.credentials.domains.*
      import com.cloudbees.plugins.credentials.common.*
      import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl
      
      def instance = Jenkins.getInstance()
      
      credentials = new StringCredentialsImpl(
        CredentialsScope.GLOBAL,
        "jenkins_slack_token",
        "slack token",
        new Secret(System.getenv('SLACK_TOKEN')))
      SystemCredentialsProvider.instance.store.addCredentials(Domain.global(), cred)
      
      def _slack = instance.getDescriptorByType(jenkins.plugins.slack.SlackNotifier.DescriptorImpl)
      _slack.teamDomain = System.getenv('SLACK_DOMAIN')
      _slack.room = System.getenv('SLACK_CHANNEL')
      _slack.token = System.getenv('SLACK_TOKEN')
      //_slack.setAuthTokenCredentialId(credentials.id)
      //_slack.authTokenCredentialId = credentials.id
      _slack.save()
      

      However

      _slack.setAuthTokenCredentialId(credentials.id)

      and

      _slack.authTokenCredentialId = credentials.id

      both return:

      Jan 05, 2018 6:41:55 PM jenkins.util.groovy.GroovyHookScript execute                                                                                                                                               WARNING: Failed to run script file:/var/jenkins_home/init.groovy.d/slack.groovy                                                                                                                            
      groovy.lang.MissingMethodException: No signature of method: jenkins.plugins.slack.SlackNotifier$DescriptorImpl.setAuthTokenCredentialId() is applicable for argument types: (java.lang.String) values: [jenkins_slack_token]                                                                                                                                                                                                                  at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)                                                                                       
              at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)                                                                                                       
              at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)                                                                                                                           at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)                                                                                                                           at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)                                                                                                                           at slack.run(slack.groovy:24)                                                                                                 
              at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)                                                                                                     
              at jenkins.util.groovy.GroovyHookScript.execute(GroovyHookScript.java:136)                                                      
              at jenkins.util.groovy.GroovyHookScript.execute(GroovyHookScript.java:127)                                                     
              at jenkins.util.groovy.GroovyHookScript.run(GroovyHookScript.java:110)                                                                                          
              at hudson.init.impl.GroovyInitScript.init(GroovyInitScript.java:41)                                                                                                                                                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)                                                                                                                                                     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)                                                                                                                          
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)                                                                                                                           at java.lang.reflect.Method.invoke(Method.java:498)                                                                                                                                                                at hudson.init.TaskMethodFinder.invoke(TaskMethodFinder.java:104)                                                                                                                               
              at hudson.init.TaskMethodFinder$TaskImpl.run(TaskMethodFinder.java:175)                                                                                                                                 
              at org.jvnet.hudson.reactor.Reactor.runTask(Reactor.java:282)                                                                                                                                                      at jenkins.model.Jenkins$5.runTask(Jenkins.java:1065)                                                                                                                                                  
              at org.jvnet.hudson.reactor.Reactor$2.run(Reactor.java:210)                                                                                                                                                        at org.jvnet.hudson.reactor.Reactor$Node.run(Reactor.java:117)                                                                                                                                          
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)                                                                                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)                                                                                                                                 at java.lang.Thread.run(Thread.java:748) 
      

      or

      Jan 05, 2018 6:37:16 PM jenkins.util.groovy.GroovyHookScript execute                                                                                                                                               WARNING: Failed to run script file:/var/jenkins_home/init.groovy.d/slack.groovy                                                                                                                                    groovy.lang.MissingPropertyException: No such property: authTokenCredentialId for class: jenkins.plugins.slack.SlackNotifier$DescriptorImpl                                                                                at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)                                                                                                                         at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:486)                                                                                                                   at slack.run(slack.groovy:24)                                                                                                                                                                                      at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)                                                                                                                                                          at jenkins.util.groovy.GroovyHookScript.execute(GroovyHookScript.java:136)                                                                                            
              at jenkins.util.groovy.GroovyHookScript.execute(GroovyHookScript.java:127)                                                                                    
              at jenkins.util.groovy.GroovyHookScript.run(GroovyHookScript.java:110)                                                                                          
              at hudson.init.impl.GroovyInitScript.init(GroovyInitScript.java:41)                                                            
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)                                                                                                  
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)                                                                                                                                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)                                                                                                                           at java.lang.reflect.Method.invoke(Method.java:498)                                                                                                                                                       
              at hudson.init.TaskMethodFinder.invoke(TaskMethodFinder.java:104)                                                                                                                                                  at hudson.init.TaskMethodFinder$TaskImpl.run(TaskMethodFinder.java:175)                                                                                                                                            at org.jvnet.hudson.reactor.Reactor.runTask(Reactor.java:282)                                                                                                                                           
              at jenkins.model.Jenkins$5.runTask(Jenkins.java:1065)                                                                                                                                                   
              at org.jvnet.hudson.reactor.Reactor$2.run(Reactor.java:210)                                                                                                                                                        at org.jvnet.hudson.reactor.Reactor$Node.run(Reactor.java:117)                                                                                                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)                                                                                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)                                                                                                                      
              at java.lang.Thread.run(Thread.java:748)
      

            kmadel Kurt Madel
            myoung34 marc young
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: