Jenkinsfile Trigger a job from quay.io trigger plugin - docker

I am attempting to move from a Freestyle Project to a multibranch pipeline build. I would like my Jenkinsfile to trigger when a new container has been pushed to my Quay.io repository. In the Freestyle Project I'm able to accomplish this with the Quay.io Trigger Plugin.
Moving to the Multibranch build pipeline I've found this post, that describes how to trigger on a dockerhub trigger. I've also used the Jenkins Pipeline Syntax "wizard" to generate the code to add to my Jenkinsfile:
properties([[$class: 'ScannerJobProperty', doNotScan: false], [$class: 'RebuildSettings', autoRebuild: false, rebuildDisabled: false], [$class: 'ThrottleJobProperty', categories: [], limitOneJobWithMatchingParams: false, maxConcurrentPerNode: 0, maxConcurrentTotal: 0, paramsToUseForLimit: '', throttleEnabled: false, throttleOption: 'project'], pipelineTriggers([[$class: 'QuayIoTrigger', repositories: ['hostedsparkbots/janitorbot-timer', 'hostedsparkbots/janitorbot', 'hostedsparkbots/sparky']]])])
In the above case when I do a scan of my github repository I get a wall of backtraces from the jenkins console:
java.lang.IllegalArgumentException: java.lang.ClassCastException#712ddbf9
at sun.reflect.GeneratedMethodAccessor4447.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jenkinsci.plugins.structs.describable.Setter$1.set(Setter.java:33)
at org.jenkinsci.plugins.structs.describable.DescribableModel.injectSetters(DescribableModel.java:338)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:261)
Caused: java.lang.IllegalArgumentException: Could not instantiate {repositories=[hostedsparkbots/janitorbot-timer, hostedsparkbots/janitorbot, hostedsparkbots/sparky]} for QuayIoTrigger(repositories?: String[])
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:264)
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:380)
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerceList(DescribableModel.java:461)
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:365)
at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:318)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:259)
Caused: java.lang.IllegalArgumentException: Could not instantiate {triggers=[{$class=QuayIoTrigger, repositories=
Does anybody actually have this working in a Jenkinsfile?

The answer:
If you cast the collection of repositories as a java.util.Set in the Jenkinsfile, this works as expected. Using your list above, you'd want to do this:
#!groovy
#import java.util.Set // this may not be required?
properties([
pipelineTriggers([[
$class: 'QuayIoTrigger',
repositories: (['hostedsparkbots/janitorbot-timer',
'hostedsparkbots/janitorbot',
'hostedsparkbots/sparky'] as Set)
]])
])
The background:
I had been struggling to figure this out myself, but ultimately dug through the source code in the Quay.io Trigger Plugin. The current plugin isn't designed with Jenkins Pipeline in mind, so it uses a Set in the constructor for the repository collection.
This is where the cast exception is occurring, as Groovy is treating the list of strings as an array, an not able automatically cast this into a Set.
By explicitly creating the list of repositories as a Set, the plugin is configurable in the Jenkinsfile.
Hope this helps!

Related

In Jenkins pipeline, how to set a value for an environment variable when loading a shared library?

I have a multi-branch pipeline that uses a Jenkinsfile to load a shared library defined in my system configuration.
#Library("my-shared-library") _
import com.company.exa.builders.BaseBuilder
import com.company.exa.builders.EdiBuilder
import hudson.model.*
buildNumbers = getBuildNumbers() // Function not shown, but it works
properties ([
disableConcurrentBuilds(),
[$class: 'jenkins.model.BuildDiscarderProperty',
strategy: [$class: 'LogRotator',
numToKeepStr: '50',
artifactNumToKeepStr: '20']],
parameters ([
choiceParam(name: "VERSION_CHOICE",
choices: buildNumbers,
description: "Version from Builds"),
stringParam(name: "VERSION_PASSEDIN",
defaultValue: env.BRANCH_NAME,
description: "Passed-in version. Note this will override VERSION_CHOICE."),
booleanParam(name: "UPLOAD_ARTIFACTS",
defaultValue: false,
description: "Upload artifacts to file servers?"),
choiceParam(name: "DEBUG_LEVEL",
choices: ["0", "1", "2", "3"],
description: "Debug level; 0=less verbose, 3=most verbose")
])
])
When I run it clicking Scan Multibranch Pipeline Now, I get
00:00:01.018 Loading library my-shared-library
00:00:01.019 Attempting to resolve maser from remote references...
00:00:01.019 > git --version # timeout=10
00:00:01.023 > git --version # 'git version 2.17.1'
00:00:01.023 using GIT_SSH to set credentials Jenkins Master SSH
00:00:01.028 > git ls-remote -h -- git#bitbucket.org:cfouts-kmha/kmha-infrastructure.git # timeout=10
00:00:01.546 Found match: refs/heads//master revision a1bc1e273b41c4e892d7c25814d0f2a1c261f7e5
00:00:01.546 ERROR: Checkout failed
00:00:01.546 java.lang.IllegalArgumentException: Null value not allowed as an environment variable: VERSION_PASSEDIN
00:00:01.546 at hudson.EnvVars.put(EnvVars.java:379)
00:00:01.546 at hudson.model.StringParameterValue.buildEnvironment(StringParameterValue.java:59)
...complaining that variable VERSION_PASSEDIN is null. I've tried setting the VERSION_PASSEDINvariable to just "" in the following locations to no avail...
The multi-branch pipeline's Folder properties
The multi-branch pipeline's parent folder properties
In the Jenkinsfile itself
In the System configuration global properties
Any clues on how to fix this? I have a feeling it's something obvious that I'm not seeing.
Note that if I run the job with a branch's "Build with parameters" link, the job runs fine.
You have a chicken-and-egg problem when trying to set default value for parameters, e.g. here:
stringParam(name: "VERSION_PASSEDIN",
defaultValue: env.BRANCH_NAME,
To run your pipeline, Jenkins needs to figure out the value of the parameters. However, to figure out the value of the parameters, Jenkins needs to run your pipeline. See the problem?
To overcome this problem, here's what happens: Jenkins assumes the value of nothing for both VERSION_CHOICE and VERSION_PASSEDIN. In the first case, it's an empty choice; in the second, it's null.

How to download source code by specific change set number in jenkins?

node{
stage('Source Control Management'){
checkout([
$class: 'TeamFoundationServerScm',
credentialsConfigurer: [
$class: 'AutomaticCredentialsConfigurer'
],
projectPath: '$/Onprem/Source/Service',
serverUrl: 'http://abcd/',
useOverwrite: true,
useUpdate: true,
workspaceName: 'Hudson-${JOB_NAME}-${NODE_NAME}'
])
}
}
This pipeline script checkout the latest code whose chain set no is: 921
I want the pipeline to checkout only the previous code with chain set no: 917
What to do?
The plugin README says this available and describes how to do it for a Freestyle job:Use a versionspec argument to specify affected versions of items. Confirmed this works in a Freestyle job, but not a pipeline. Also, only works if supplied a Build Parameter, not as a regular parameter; odd.

How to checkout from PTC Integrity in Jenkins Pipeline SCM Step Plugin

I want to add a process step in my pipeline, that checks out a project from PTC Integrity. There is a example in the Snippet Generator for a checkin but it is not built correctly, so I can not take this for orientation.
As PTC Integrity is not properly documented for the use with SCM Step Plugin (although supported according to their compability list) I have no idea how to do this.
My first step was to Find out what if there is a Integrity SCM class with this pipeline script:
node {
checkout scm: [$class: 'IntegritySCM']
}
This throws as expected an IllegalArgumentException:
java.lang.IllegalStateException: cannot call getRootUrlFromRequest from outside a request handling thread
at jenkins.model.Jenkins.getRootUrlFromRequest(Jenkins.java:2366)
at hudson.scm.IntegritySCM.<init>(IntegritySCM.java:113)
Caused: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:260)
Caused: java.lang.IllegalArgumentException: Could not instantiate {serverConfig=d917f329-9826-4ffa-8bbd-de68271c4abd}
for IntegritySCM(browser: IntegrityRepositoryBrowser{IntegrityWebUI(url: String)}, serverConfig: String, userName: String, password: String, configPath: String, includeList: String,
excludeList: String, cleanCopy: boolean, lineTerminator: String, validCheckpoint: boolean, freezeMembers: boolean, noCheckout: boolean,
restoreTimestamp: boolean, skipAuthorInfo: boolean, checkpointBeforeBuild: boolean, checkpointLabel: String,
alternateWorkspace: String, fetchChangedWorkspaceFiles: boolean, deleteNonMembers: boolean, checkoutThreadPoolSize: int, configurationName: String)
From this I can conclude which arguments can be given for the checkout command.
Now my question is, how can I build the checkout command in the pipeline script?
I saw an example on the github page of the scm step plugin but for mercurial which does it like this:
checkout scm: [$class: 'MercurialSCM', 'source': '....', clean: true...]
But I can't apply this to Integrity. E.g. I don't know how to tell the command the path of the project I want to checkout.
The pipeline snippet generator tool can be used to generate a pipeline syntax for any SCM classes supported by Pipeline. You can take a look here: https://jenkins.io/blog/2016/05/31/pipeline-snippetizer/
A sample pipeline snippet is something like this:
checkout([$class: 'IntegritySCM', checkpointBeforeBuild: false, configPath: '\integrity project configuration path\', configurationName: '7fc7dc75-f94e-4cc7-ab57-558f611d0fb8', deleteNonMembers: false, fetchChangedWorkspaceFiles: false, localClient: true, password: '{AQAAABAAAAAQ7YBJmtuzpndmmu/eUEL3v80g/oF3g+lzpM1S1JHkk5E=}', restoreTimestamp: false, serverConfig: '836d9a67-b82d-4cf3-ba06-c4557a18306b', skipAuthorInfo: false])

Parameterized pipeline build jenkins always use last build parameters. How to fix this?

I am using Jenkins pipeline with multibranch setup. I set a build parameter for "Build with parameters" using Jenkinsfile. Issue is it always picks up the previous build parameters. Here is the code:
properties([[$class: 'JobRestrictionProperty'],
parameters([
string(defaultValue: 'https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md#build-parameters', description: 'File-Location', name: 'File_Location')
]), [$class: 'ThrottleJobProperty', categories: [], limitOneJobWithMatchingParams: false, maxConcurrentPerNode: 0, maxConcurrentTotal: 0, paramsToUseForLimit: '', throttleEnabled: false, throttleOption: 'project'], pipelineTriggers([])])
I am doing a reset of this variable(making it empty) after every run. But for the next build, it always picks the last one(which is empty), not the new one defined in the Jenkins file.
I think this is by design, parametrized pipelines prompts for parameters only the first time and then use those parameters by default

jenkins pipeline warnings graph

I started to migrate some jobs in jenkins to pipeline execution.
Is there any chance to see the warnings graph in multi branch pipeline jobs? Within my older projects I can configure the graphs with "Configure the trend graph" option. These option will crash in the pipeline syntax tool.
Is there any option to make the graph visible?
I have in my Jenkinsfile:
stage ('Warnings gcc') {
step([$class: 'WarningsPublisher', canComputeNew: false, canResolveRelativePaths: false, defaultEncoding: '', excludePattern: '', healthy: '', includePattern: '', messagesPattern: '', parserConfigurations: [[parserName: 'GNU Make + GNU C Compiler (gcc)', pattern: 'error_and_warnings.txt']], unHealthy: ''])
}
stage ('Warnings clang') {
step([$class: 'WarningsPublisher', canComputeNew: false, canResolveRelativePaths: false, defaultEncoding: '', excludePattern: '', healthy: '', includePattern: '', messagesPattern: '', parserConfigurations: [[parserName: 'Clang (LLVM based)', pattern: 'error_and_warnings_clang.txt']], unHealthy: ''])
}
The reports will be generated but no graph is displayed.
UPDATE: Now it still did not work but it is also impossible to use the snipped generator for the warnings plugin.
Entering a file name in the snipped generator for the warning plugin results in a java null pointer exception:
javax.servlet.ServletException: java.lang.NullPointerException
at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:796)
at org.kohsuke.stapler.Stapler.invoke(Stapler.java:876)
at org.kohsuke.stapler.MetaClass$5.doDispatch(MetaClass.java:236)
at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:58)
at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:746)
at org.kohsuke.stapler.Stapler.invoke(Stapler.java:876)
at org.kohsuke.stapler.MetaClass$10.dispatch(MetaClass.java:362)
some more lines follows ...
Mentioned in the revision log for the warnings plugin V 4.56:
Fixed deactivation of trend graphs (using the analysis collector plug-in)
But I use 4.57 and it still did not display any graph!
I posted the problem also to jenkins user list (no feedback for weeks) and also added bug report and bug report
Can anyone reproduce the problem or is the multi-branch pipeline simply still broken at all? Seems to be that there are not so much users for this plugin...
These issues have been resolved with the latest versions of both Jenkins, the pipelines plugin[s], and the plugins you have mentioned above.
Additionally, the bugs you specifically reported have been resolved:
[FIXED JENKINS-39553] Make GitHub plugin BuildableItem aware (#153)
[FIXED JENKINS-39532] Do not access the workspace for pipelines
Update your Jenkins instance and all of your plugins (some of them have interdependencies on others) and after the suggested restart you should be able to display the graph successfully.

Resources