github: TCP connection reset by peer

2020-12-17 Thread unlimhost
Hello, responsive community!

Periodically, when checking out from github, there are such errors. if you have 
encountered, please share all possible solutions or solutions to accurately 
identify the culprit of the problem (network, github...):


The recommended git tool is: /usr/bin/git
using credential github-PAT
Fetching changes from the remote Git repository
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from 
https://github.com/myrepo/mygit.git
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:1001)
at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1242)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1302)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:125)
at 
org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:93)
at 
org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:80)
at 
org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
at 
java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at 
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at 
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: hudson.plugins.git.GitException: Command "/usr/bin/git fetch --tags 
--force --progress -- https://github.com/myrepo/mygit.git 
+refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout:
stderr: fatal: unable to access 'https://github.com/myrepo/mygit.git/': TCP 
connection reset by peer


p.s.
My code for checkout:

checkout ([
$class: 'GitSCM',
branches: [[name: params.branch_name]],
extensions: [[$class: 'CleanCheckout'],[$class: 'CheckoutOption', timeout: 
60],[$class: 'CloneOption', depth: 1, noTags: true, reference: '', shallow: 
false, timeout: 60]],
userRemoteConfigs: [[url: 'https://github.com/myrepo/mygit.git', credentialsId: 
'github-PAT']]])


Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/1683745647.20201218105301%40gmail.com.


Re: BUG with libraryResource : No such library resource null could be found.

2020-12-17 Thread Stuart Rowe
Any chance this is being called from a @NonCPS method? I've seen similar 
behaviour in that case.
On Monday, 14 December 2020 at 14:37:55 UTC-8 yannick...@gmail.com wrote:

> Hi,
>
> In a shared library, in a groovy script in /vars, the following code is 
> working perfectly:
>
> String call(String stageName) {
>   String cowMsg = libraryResource 'cow.txt'
>   if (cowMsg != null) {
> def vars = [:]
> vars['MESSAGE'] = "BEGIN STAGE: ${stageName}"
> def engine = new StreamingTemplateEngine()
> def asciiArtMsg = engine.createTemplate(cowMsg).make(vars).toString()
> echo "${asciiArtMsg}"
> } else {
> echo "I never see that because cowMsg is not null..."
> }
>
> This prints my file cow.txt as expected, because cowMsg is NOT null.
>
> BUT : if I remove the "if", I have this error :
>   No such library resource null could be found.
>
> It is like my resource suddenly became null !
>
> I load some other files without the need of this mysterious "if".
>
> Is that a Groovy bug ?
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/3d05598e-01d9-4a86-9b1d-bc798546b94cn%40googlegroups.com.


Re: Simultaneous pipelines deadlocked "waiting for next available executor"

2020-12-17 Thread John Girvin

I've since found that removing the node{} wrapping the custom buildDeployMaven 
step seems to solve the deadlock.

I'm guessing node{} was allocating an executor for the step, which then 
immediately requested another executor for it's internal pipeline{} ?

On Thursday, 17 December 2020 at 15:50:23 UTC John Girvin wrote:

> I'm trying to understand a problem with our Jenkins, where it seems 
> piplines get deadlocked waiting for an executor if we run many of them 
> simultaneously.
>
> Jenkins 2.271 on Linux x64. We have one master node configured with 4 
> executors. Running a job singly shows it taking 2 slots in the Build 
> Executor Status panel and works fine. However, if we run a number of jobs 
> simultaneously they all get stuck "waiting for next available executor" and 
> not progressing, seemingly indefinitely.
>
> The Console Output in each shows the pipeline library being checked out, 
> then:
>
> *[Pipeline] Start of Pipeline*
>
>
> *[Pipeline] nodeStill waiting to schedule taskWaiting for next available 
> executor *
>
> It's as if the script flyweights are consuming all the executors, leaving 
> none available to run the steps, but that doesn't really make sense to me 
> given what I've read about flyweights (they are unlimited?).
>
> It's not a machine capacity issue either. I wondered if it was how the 
> pipelines are structured?
>
> The pipelines are all scripted not declarative, and look like this:
> @Library('jenkins-pipelines@master') _
> node {
> buildDeployMaven([
> ...parameters...
> ])
> }
>
> buildDeployMaven is a groovy method structured like so, with each stage 
> being a script step:
> def call(params) {
> agent any
> tools {
> ...
> }
> pipeline {
> stages {
> stage('Checkout') {
> steps {
> script {
> ...
> }
> }
> }
>
> [further stages omitted but similar]
> }
> }
> }
>
> Any ideas appreciated!
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/34e325c1-e959-4572-8302-a4325d427793n%40googlegroups.com.


Re: java.lang.NoSuchMethodError: No such DSL method 'withKubeConfig' found among steps [approveReceivedEvent,

2020-12-17 Thread Mark Waite
Are you sure that you have the Kubernetes CLI plugin installed?
https://plugins.jenkins.io/kubernetes-cli/

On Thu, Dec 17, 2020 at 8:50 AM anilkumar panditi <
anilkumar.pand...@gmail.com> wrote:

> Hi,
> My Pipeline is failing with below error.
>
> java.lang.NoSuchMethodError: No such DSL method 'withKubeConfig' found
> among steps [approveReceivedEvent, approveRequestedEvent, archive, bat,
> build, catchError, checkout, container, containerLog, createEvent,
> deleteDir, dir, dockerFingerprintFrom, dockerFingerprintRun, echo,
> emailext, emailextrecipients, envVarsForTool, error, fileExists,
> findBuildScans, getContext, git, input, isUnix, jiraComment,
> jiraIssueSelector, jiraSearch, junit, kubernetesApply, kubernetesDeploy,
> library, libraryResource, load, lock, mail, milestone, node, parallel,
> podTemplate, powershell, properties, publishChecks, publishHTML, pwd, pwsh,
> readFile, readTrusted, resolveScm, retry, script,
> setGitHubPullRequestStatus, sh, slackSend, slackUploadFile,
> slackUserIdFromEmail, slackUserIdsFromCommitters, sleep, stage, stash,
> step, svn, timeout, timestamps, tm, tool, unarchive, unstable, unstash,
> validateDeclarativePipeline, waitUntil, warnError, withContext,
> withCredentials, withDockerContainer, withDockerRegistry, withDockerServer,
> withEnv, withGradle, wrap, writeFile, ws] or symbols [GenericTrigger,
> Number, Open, all, allBranchesSame, allOf, allowRunOnStatus, always, ant,
> antFromApache, antOutcome, antPath, antTarget, any, anyOf, apiToken,
> architecture, archiveArtifacts, artifactManager, asIsGITScm,
> authorizationMatrix, batchFile, bitbucket, bitbucketBranchDiscovery,
> bitbucketForkDiscovery, bitbucketPublicRepoPullRequestFilter,
> bitbucketPullRequestDiscovery, bitbucketSshCheckout, bitbucketTagDiscovery
>
> I have installed Kubernetes Plugin and added credentials.Any Idea?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Jenkins Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jenkinsci-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jenkinsci-users/e5dcb413-96bf-4339-aac3-9cec315e8efdn%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/CAO49JtH825tCDtpJYmhub3KTPRBgE78sO_1CcS9yzLd6R3e25A%40mail.gmail.com.


java.lang.NoSuchMethodError: No such DSL method 'withKubeConfig' found among steps [approveReceivedEvent,

2020-12-17 Thread anilkumar panditi
Hi,
My Pipeline is failing with below error.

java.lang.NoSuchMethodError: No such DSL method 'withKubeConfig' found 
among steps [approveReceivedEvent, approveRequestedEvent, archive, bat, 
build, catchError, checkout, container, containerLog, createEvent, 
deleteDir, dir, dockerFingerprintFrom, dockerFingerprintRun, echo, 
emailext, emailextrecipients, envVarsForTool, error, fileExists, 
findBuildScans, getContext, git, input, isUnix, jiraComment, 
jiraIssueSelector, jiraSearch, junit, kubernetesApply, kubernetesDeploy, 
library, libraryResource, load, lock, mail, milestone, node, parallel, 
podTemplate, powershell, properties, publishChecks, publishHTML, pwd, pwsh, 
readFile, readTrusted, resolveScm, retry, script, 
setGitHubPullRequestStatus, sh, slackSend, slackUploadFile, 
slackUserIdFromEmail, slackUserIdsFromCommitters, sleep, stage, stash, 
step, svn, timeout, timestamps, tm, tool, unarchive, unstable, unstash, 
validateDeclarativePipeline, waitUntil, warnError, withContext, 
withCredentials, withDockerContainer, withDockerRegistry, withDockerServer, 
withEnv, withGradle, wrap, writeFile, ws] or symbols [GenericTrigger, 
Number, Open, all, allBranchesSame, allOf, allowRunOnStatus, always, ant, 
antFromApache, antOutcome, antPath, antTarget, any, anyOf, apiToken, 
architecture, archiveArtifacts, artifactManager, asIsGITScm, 
authorizationMatrix, batchFile, bitbucket, bitbucketBranchDiscovery, 
bitbucketForkDiscovery, bitbucketPublicRepoPullRequestFilter, 
bitbucketPullRequestDiscovery, bitbucketSshCheckout, bitbucketTagDiscovery  

I have installed Kubernetes Plugin and added credentials.Any Idea?

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/e5dcb413-96bf-4339-aac3-9cec315e8efdn%40googlegroups.com.


Simultaneous pipelines deadlocked "waiting for next available executor"

2020-12-17 Thread John Girvin
I'm trying to understand a problem with our Jenkins, where it seems 
piplines get deadlocked waiting for an executor if we run many of them 
simultaneously.

Jenkins 2.271 on Linux x64. We have one master node configured with 4 
executors. Running a job singly shows it taking 2 slots in the Build 
Executor Status panel and works fine. However, if we run a number of jobs 
simultaneously they all get stuck "waiting for next available executor" and 
not progressing, seemingly indefinitely.

The Console Output in each shows the pipeline library being checked out, 
then:

*[Pipeline] Start of Pipeline*


*[Pipeline] nodeStill waiting to schedule taskWaiting for next available 
executor *

It's as if the script flyweights are consuming all the executors, leaving 
none available to run the steps, but that doesn't really make sense to me 
given what I've read about flyweights (they are unlimited?).

It's not a machine capacity issue either. I wondered if it was how the 
pipelines are structured?

The pipelines are all scripted not declarative, and look like this:
@Library('jenkins-pipelines@master') _
node {
buildDeployMaven([
...parameters...
])
}

buildDeployMaven is a groovy method structured like so, with each stage 
being a script step:
def call(params) {
agent any
tools {
...
}
pipeline {
stages {
stage('Checkout') {
steps {
script {
...
}
}
}

[further stages omitted but similar]
}
}
}

Any ideas appreciated!

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/ab156be5-94bd-4f94-9de1-3650ff9a898fn%40googlegroups.com.


Using Thread pool in Jenkins pipeline

2020-12-17 Thread ramesh pasham
Anyone tried the ThreadPool in jenkins pipeline? it is not executing as
expected ? Please advise.

import java.util.concurrent.*
class MyRunnable implements Runnable{
   Integer l
   MyRunnable(Integer l){
  this.l  = l

   }
   void run(){
   try{
print l
 }
catch(Exception e){
  print e
}
  }

}
node{
stage('stage1') {
ExecutorService executor = Executors.newFixedThreadPool(6)
mylist = 1..100
try{
for(l in mylist){
MyRunnable worker = new MyRunnable(l)
executor.execute(worker)
}
 }
finally{
executor.shutdown()
}
}
}
-Ramesh

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/CALEm6wTV_ScKAatBqapZz_-hhN%3DqFX_%2BAqw_qhWyOsUv7RgtGQ%40mail.gmail.com.


Re: Junit test : seed job to maven Job DSL

2020-12-17 Thread Charles Moulliard
FYI: I can, of course, create the job and build it using the `config.xml` 
generated on a jenkins instance running locally -> `java -jar 
jenkins-cli.jar -s http://localhost:8080 create-job maven-seed-job2 < 
seed-maven-job2.xml`

So I suspect a problem with the Job DSL Plugin when the job build is 
scheduled with the "ExecuteDslScripts". But If no stacktrace is reported, 
then it will very hard to figure out the issue
On Thursday, December 17, 2020 at 11:54:39 AM UTC+1 Charles Moulliard wrote:

> Hi
>
> I created a Junit test case which is available here:  
> https://github.com/ch007m/jenkins-job-dsl/blob/31efa049a95c333a8dba9b01ff2aa281b3520e34/src/test/java/dev/snowdrop/MavenJobDSLTest.java#L25-L53
>  
> where a seed job is created to populate a mavenJob using the DSL groovy 
> file provided 
>
> But when it is executed; the job build fails and reports this error
>
> Legacy code started this job.  No cause information is available
> Running as SYSTEM
> Building in workspace /var/folders/t2/jwchtqkn5y76hrfrws7dqtqmgn/T/j 
> h5344303144116520886/workspace/test0
> Processing provided DSL script
> ERROR: java.io.IOException: Unable to read 
> /var/folders/t2/jwchtqkn5y76hrfrws7dqtqmgn/T/j 
> h5344303144116520886/jobs/mvn-spring-boot-rest-http/config.xml
> Finished: FAILURE
>
> The error comes from the fact that the job "mvn-spring-boot-rest-http" 
> has not been created under the "jobs" folder. the jobs folder only contains 
> a "test0" folder.
>
> How can I investigate the problem and fix that ? 
>
> If I check the config.xml file generated, it looks good (vs same 
> config.xml file created using the UI)
>
> 
> 
>   false
>   
>   
>   false
>   false
>   
> false
>   false
>   
>   false
>   
> 
>   mavenJob(mvn-spring-boot-rest-http) {
> description A Maven Job compiling the project Spring Boot Rest 
> HTTP Example
>
> parameters {
> gitParameter {
> name SELECTED_TAG
> description The Git tag to checkout
> type PT_TAG
> defaultValue 2.3.4-2
> branch 
> branchFilter origin/(.*)
> quickFilterEnabled false
> selectedValue DEFAULT
> sortMode DESCENDING_SMART
> tagFilter *
> useRepository .*rest-http-example.git
> listSize 10
> }
> }
>
> scm {
> git {
> remote {
> url 
> https://github.com/snowdrop/rest-http-example.git;
> // branch($SELECTED_TAG)
> branch(2.3.4-2)
> }
> }
> }
> rootPOM pom.xml
> goals clean install
> }
>   true
>   false
>   false
>   false
>   false
>   false
>   false
>   IGNORE
>   IGNORE
>   IGNORE
>   JENKINS_ROOT
> 
>   
>   
>   
> 
>
> excepted that the config.xml created from the UI includes this additional 
> parameter
>
> 
>
> Cheers
>
> Charles
>

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/000da4cd-8384-4417-b3f0-def8986ef8d7n%40googlegroups.com.


Re: Better way to load all the plugins/dependencies for local java testing using jenkinsrule

2020-12-17 Thread Yannick Lacaute
Hi Charles,

I am working on a pipeline (build java > build docker > deploy pod in
Openshift) and currently I have no unit test.
I think you achieved something huge !! Well done by the way :)

Could you please give me someone web links or tips in order to do the same
as you ?
Do you use https://github.com/jenkinsci/JenkinsPipelineUnit ?
Is there a sample project from which we can start ?
Should we just have to follow the same organization in JenkinsPipelineUnit
> test ?
Have you automatised your scripts or do you have to play them sometimes to
update your project ?

Thank for you advices

PS : Currently my pipeline is declared in a shared library.



Regards



On Thu, Dec 17, 2020 at 9:51 AM Charles Moulliard 
wrote:

> I created 2 groovy scripts that I execute in a jenkins instance to figure
> out the list to be used and to be configured within the `build.gradle` file
>
> Jenkins.instance.pluginManager.plugins
> .findAll { !(it.shortName in ['job-dsl', 'structs']) }
> .collect { "testPlugins
> '${it.manifest.mainAttributes.getValue("Group-Id")}:${it.shortName}:${it.version}'"
> }
> .sort()
> .each { println it }
>
> and testCompile dependencies
> Jenkins.instance.pluginManager.plugins
> .findAll { !(it.shortName in ['job-dsl', 'structs']) }
> .collect { "testCompile
> '${it.manifest.mainAttributes.getValue("Group-Id")}:${it.shortName}:${it.version}@jar'"
> }
> .sort()
> .each { println it }
>
> On Wednesday, December 16, 2020 at 6:48:41 PM UTC+1 Charles Moulliard
> wrote:
>
>> Hi
>>
>> As a java unit test will need jar files, it is then needed to load them
>> nd to declare them within the pom.xml file or build.gradle file
>>
>> Example:
>>
>> testCompile("org.jenkins-ci.main:jenkins-test-harness:${jenkinsTestHarness}")
>> testCompile("org.jenkins-ci.main:jenkins-war:${jenkinsCore}")
>>
>> testCompile("org.jenkins-ci.plugins:script-security:${jenkinsTestSecurityScript}@jar
>> ")
>>
>> // Add the jar file as the testPlugins will install the hpi/jpi files
>> and not the jar needed by the junit test
>> testCompile 'org.jenkins-ci.tools:git-parameter:0.9.13@jar'
>>
>> testCompile 'org.jenkins-ci.plugins:structs:1.20@jar'
>> testCompile 'org.jenkins-ci.plugins:git:3.10.0@jar'
>> testCompile 'org.jenkins-ci.plugins:git-client:3.5.1@jar'
>> testCompile 'org.jenkins-ci.plugins:credentials:2.3.9@jar'
>> testCompile 'org.jenkins-ci.plugins:scm-api:2.6.4@jar'
>> testCompile 'org.jenkins-ci.plugins:matrix-project:1.18@jar'
>> testCompile
>> 'org.jenkins-ci.plugins:global-variable-string-parameter:1.2@jar'
>> testCompile 'org.jenkins-ci.plugins:junit:1.47@jar'
>>
>> testCompile 'org.jenkins-ci.plugins:workflow-remote-loader:1.5@jar'
>> testCompile
>> 'org.jenkins-ci.plugins.workflow:workflow-aggregator:2.6@jar'
>> testCompile 'org.jenkins-ci.plugins.workflow:workflow-api:2.40@jar'
>> testCompile
>> 'org.jenkins-ci.plugins.workflow:workflow-basic-steps:2.18@jar'
>> testCompile
>> 'org.jenkins-ci.plugins.workflow:workflow-scm-step:2.11@jar'
>> testCompile 'org.jenkins-ci.plugins.workflow:workflow-cps:2.80@jar'
>> testCompile
>> 'org.jenkins-ci.plugins.workflow:workflow-durable-task-step:2.35@jar'
>> testCompile 'org.jenkins-ci.plugins.workflow:workflow-job:2.38@jar'
>> testCompile
>> 'org.jenkins-ci.plugins.workflow:workflow-multibranch:2.20@jar'
>> testCompile
>> 'org.jenkins-ci.plugins.workflow:workflow-step-api:2.23@jar'
>> testCompile 'org.jenkins-ci.plugins.workflow:workflow-support:3.3@jar
>> '
>>
>> testCompile 'org.jenkins-ci.plugins:matrix-auth:2.6.2@jar'
>>
>> testCompile 'org.eclipse.jgit:org.eclipse.jgit:5.10.0.202012080955-r'
>>
>> // Plugins to install in test jenkins instance running locally
>> testPlugins 'org.jenkins-ci.main:maven-plugin:3.8'
>>
>> testPlugins 'org.jenkins-ci.plugins:cloudbees-folder:5.14'
>> testPlugins 'org.jenkins-ci.plugins:credentials:2.3.9'
>> testPlugins 'org.jenkins-ci.plugins:structs:1.20'
>> testPlugins
>> 'org.jenkins-ci.plugins:global-variable-string-parameter:1.2'
>>
>> testPlugins 'org.jenkins-ci.tools:git-parameter:0.9.13'
>> testPlugins 'org.jenkins-ci.plugins:git:3.10.0'
>> testPlugins 'org.jenkins-ci.plugins:git-client:3.5.1'
>> testPlugins 'org.jenkins-ci.plugins:scm-api:2.6.4'
>> testPlugins 'org.jenkins-ci.plugins:matrix-project:1.18'
>>
>> testPlugins 'org.jenkins-ci.plugins:workflow-remote-loader:1.5'
>> testPlugins 'org.jenkins-ci.plugins.workflow:workflow-aggregator:2.6'
>> testPlugins 'org.jenkins-ci.plugins.workflow:workflow-api:2.40'
>> testPlugins
>> 'org.jenkins-ci.plugins.workflow:workflow-basic-steps:2.18'
>> testPlugins 'org.jenkins-ci.plugins.workflow:workflow-scm-step:2.11'
>> testPlugins 'org.jenkins-ci.plugins.workflow:workflow-cps:2.80'
>> testPlugins
>> 'org.jenkins-ci.plugins.workflow:workflow-durable-task-step:2.35'
>> testPlugins 

Re: Jenkinsfile Accessing variables from shared libraries

2020-12-17 Thread Yannick Lacaute
Hi,

I think you should not use global variables at all : only global functions.
Trust me : you don't need global variables :)
To use global functions :

# /vars/myGlobFunc.groovy
def call() {
  // 
}
// This way you can call you function directly in "steps" if you are in
declarative pipeline, from anywhere in scripted
// This function has access to other global methods

# Or this way, in /vars/myClass.groovy :
def myGlobFunc.groovy {
   // ...
}
return this
// myClass.myGlobFunc()
// This function has also access to other global methods because of this
special class declaration
// But you cannot call this from steps in declarative, you must be inside
script

But if you really want global variables, I would recommand to use a single
object containing what you need and give this object to people who need it,
for example a class or a map :

def myVars = [:]
myVars['myGlobVar'] = 'hello'
pipeline {
  // ...
   aFunction(myVars)
}

# But if you really really want global, just remove the "def" :
myVars = [:]
myVars['myGlobVar'] = 'hello'
pipeline {
  // ...
   aFunction()  // myVars can be read in this function
}
// But I thihnk this is a bad code

Another way is to use static field of class, but it is also a bad pratice
to use static field if you want them mutable.

You could also just try to use the existing global var "env". This is a
clean solution, but you must only use text values.
env['myGlobVar'] = "hello"
echo env.myGlobVar

Good luck


On Thu, Dec 17, 2020 at 9:45 AM Ven H  wrote:

> In my case, I do the following. I have a GlobalVars.groovy file with below
> content.
>
> #!/usr/bin/env groovypackage com..;public class 
> GlobalVars {   static String myVar = ""}
>
> Then in my Shared Library class, I use it like below.
>
> GlobalVars.myVar = ""
>
> Try it like this and check. Here, I have declared the class as Public and the 
> variable as static. That's the only difference.
>
>
> Regards,
> Venkatesh
>
>
> On Thu, Dec 17, 2020 at 12:56 PM Adrian Wyssmann 
> wrote:
>
>> I see several problem
>>
>>1. using ' will not expand the parameters is your call should be sh
>>"echo ${CustomMessage}"
>>2. CustomMessage is declared within the class Vars(), so you cannot
>>access it
>>
>> You could implement a getter an then do something like this "echo
>> ${globalVars.getCustomMessage()}"
>>
>> Maybe others have better ideas...?
>> On Thursday, December 17, 2020 at 12:10:19 AM UTC+1 netwar...@gmail.com
>> wrote:
>>
>>> Hello there
>>> Maybe this is a very basic question, but am not being able to access a
>>> global variable
>>> from a shared library from a Jenkinsfile
>>>
>>> under vars I defined something like this
>>>
>>> globalVars.groovy
>>> class Vars () {
>>>
>>> def CustomMessage = "This is a new deployment"
>>>
>>> }
>>>
>>> from my Jenkinsfile
>>>
>>> library identifier: 'globalVars@master', \
>>>  retriever: modernSCM([$class: 'GitSCMSource', \
>>> credentialsId: 'tfsservice', \
>>> remote: 'http://myrepo.com', \
>>> traits: [gitBranchDiscovery()]])
>>>
>>>
>>> pipeline {
>>>
>>> stages {
>>> stage('Do Some Stuff') {
>>>   steps {
>>> script {
>>>
>>>sh 'echo ${CustomMessage}'
>>>
>>>   }
>>> }
>>>   }
>>> }
>>>
>>> }
>>>
>>> But the above returns nothing, what am I doing wrong?
>>> Thanks
>>> Regards
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Jenkins Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to jenkinsci-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/jenkinsci-users/099e3aee-dffc-459d-8eb5-d64204effb58n%40googlegroups.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Jenkins Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jenkinsci-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jenkinsci-users/CAPp28eojNNe9xEFtD8rNhxXnY5Z118zrhGLJRVsqSqaL4qiHFA%40mail.gmail.com
> 
> .
>


-- 
Yannick LACAUTE
Consultant indépendant
39 rue de Wattignies, 75012 Paris
Tel : 06 16 85 95 76

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 

Re: Better way to load all the plugins/dependencies for local java testing using jenkinsrule

2020-12-17 Thread Charles Moulliard
I created 2 groovy scripts that I execute in a jenkins instance to figure 
out the list to be used and to be configured within the `build.gradle` file

Jenkins.instance.pluginManager.plugins
.findAll { !(it.shortName in ['job-dsl', 'structs']) }
.collect { "testPlugins 
'${it.manifest.mainAttributes.getValue("Group-Id")}:${it.shortName}:${it.version}'"
 
}
.sort()
.each { println it }

and testCompile dependencies
Jenkins.instance.pluginManager.plugins
.findAll { !(it.shortName in ['job-dsl', 'structs']) }
.collect { "testCompile 
'${it.manifest.mainAttributes.getValue("Group-Id")}:${it.shortName}:${it.version}@jar'"
 
}
.sort()
.each { println it }

On Wednesday, December 16, 2020 at 6:48:41 PM UTC+1 Charles Moulliard wrote:

> Hi
>
> As a java unit test will need jar files, it is then needed to load them nd 
> to declare them within the pom.xml file or build.gradle file
>
> Example: 
> 
> testCompile("org.jenkins-ci.main:jenkins-test-harness:${jenkinsTestHarness}")
> testCompile("org.jenkins-ci.main:jenkins-war:${jenkinsCore}")
> 
> testCompile("org.jenkins-ci.plugins:script-security:${jenkinsTestSecurityScript}@jar")
>
> // Add the jar file as the testPlugins will install the hpi/jpi files 
> and not the jar needed by the junit test
> testCompile 'org.jenkins-ci.tools:git-parameter:0.9.13@jar'
>
> testCompile 'org.jenkins-ci.plugins:structs:1.20@jar'
> testCompile 'org.jenkins-ci.plugins:git:3.10.0@jar'
> testCompile 'org.jenkins-ci.plugins:git-client:3.5.1@jar'
> testCompile 'org.jenkins-ci.plugins:credentials:2.3.9@jar'
> testCompile 'org.jenkins-ci.plugins:scm-api:2.6.4@jar'
> testCompile 'org.jenkins-ci.plugins:matrix-project:1.18@jar'
> testCompile 
> 'org.jenkins-ci.plugins:global-variable-string-parameter:1.2@jar'
> testCompile 'org.jenkins-ci.plugins:junit:1.47@jar'
>
> testCompile 'org.jenkins-ci.plugins:workflow-remote-loader:1.5@jar'
> testCompile 
> 'org.jenkins-ci.plugins.workflow:workflow-aggregator:2.6@jar'
> testCompile 'org.jenkins-ci.plugins.workflow:workflow-api:2.40@jar'
> testCompile 
> 'org.jenkins-ci.plugins.workflow:workflow-basic-steps:2.18@jar'
> testCompile 
> 'org.jenkins-ci.plugins.workflow:workflow-scm-step:2.11@jar'
> testCompile 'org.jenkins-ci.plugins.workflow:workflow-cps:2.80@jar'
> testCompile 
> 'org.jenkins-ci.plugins.workflow:workflow-durable-task-step:2.35@jar'
> testCompile 'org.jenkins-ci.plugins.workflow:workflow-job:2.38@jar'
> testCompile 
> 'org.jenkins-ci.plugins.workflow:workflow-multibranch:2.20@jar'
> testCompile 
> 'org.jenkins-ci.plugins.workflow:workflow-step-api:2.23@jar'
> testCompile 'org.jenkins-ci.plugins.workflow:workflow-support:3.3@jar'
>
> testCompile 'org.jenkins-ci.plugins:matrix-auth:2.6.2@jar'
>
> testCompile 'org.eclipse.jgit:org.eclipse.jgit:5.10.0.202012080955-r'
> 
> // Plugins to install in test jenkins instance running locally
> testPlugins 'org.jenkins-ci.main:maven-plugin:3.8'
>
> testPlugins 'org.jenkins-ci.plugins:cloudbees-folder:5.14'
> testPlugins 'org.jenkins-ci.plugins:credentials:2.3.9'
> testPlugins 'org.jenkins-ci.plugins:structs:1.20'
> testPlugins 
> 'org.jenkins-ci.plugins:global-variable-string-parameter:1.2'
>
> testPlugins 'org.jenkins-ci.tools:git-parameter:0.9.13'
> testPlugins 'org.jenkins-ci.plugins:git:3.10.0'
> testPlugins 'org.jenkins-ci.plugins:git-client:3.5.1'
> testPlugins 'org.jenkins-ci.plugins:scm-api:2.6.4'
> testPlugins 'org.jenkins-ci.plugins:matrix-project:1.18'
>
> testPlugins 'org.jenkins-ci.plugins:workflow-remote-loader:1.5'
> testPlugins 'org.jenkins-ci.plugins.workflow:workflow-aggregator:2.6'
> testPlugins 'org.jenkins-ci.plugins.workflow:workflow-api:2.40'
> testPlugins 'org.jenkins-ci.plugins.workflow:workflow-basic-steps:2.18'
> testPlugins 'org.jenkins-ci.plugins.workflow:workflow-scm-step:2.11'
> testPlugins 'org.jenkins-ci.plugins.workflow:workflow-cps:2.80'
> testPlugins 
> 'org.jenkins-ci.plugins.workflow:workflow-durable-task-step:2.35'
> testPlugins 'org.jenkins-ci.plugins.workflow:workflow-job:2.38'
> testPlugins 'org.jenkins-ci.plugins.workflow:workflow-multibranch:2.20'
> testPlugins 'org.jenkins-ci.plugins.workflow:workflow-step-api:2.23'
> testPlugins 'org.jenkins-ci.plugins.workflow:workflow-support:3.3'
>
> testPlugins 'org.jenkins-ci.plugins:matrix-auth:2.6.2'
>
> testPlugins 'org.jenkins-ci.plugins:junit:1.47'
>
> testPlugins 'org.jenkins-ci.plugins:matrix-project:1.18'
>
> As you can imagine, this process is long,tedious, will change when we 
> upgrade to another jenkins server (as the list of the default plugins will 
> be updated).
>
> Question:
> Is there a script, tool able to grab from jenkins the plugins list and 
> next to create the corresponding plugins to be imported for Jenkinsrule 
> unit test and GAVs dependencies (pom.xml or build.gradle) ?
>
> Cheers

Access the jenkins.log or folder when using jenkinsrule

2020-12-17 Thread Charles Moulliard
Hi,

Is it possible to access to the jenkins.log or the folder containing the 
files as I got this error when I run a junit test case ?

Legacy code started this job.  No cause information is available
Running as SYSTEM
Building in workspace /var/folders/t2/jwchtqkn5y76hrfrws7dqtqmgn/T/j 
h2639828161785432376/workspace/test0
Processing DSL script mavenProject/mavenJob.groovy
ERROR: java.io.IOException: Unable to read 
/var/folders/t2/jwchtqkn5y76hrfrws7dqtqmgn/T/j 
h2639828161785432376/jobs/mvn-spring-boot-rest-http/config.xml
Finished: FAILURE

Example of code:

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void useMavenDSLGroovyFileAsJob() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
p.scheduleBuild2(0).get(); // run a build to create a workspace

p.getSomeWorkspace().child("mavenProject/mavenJob.groovy").copyFrom(getClass().getResourceAsStream("/mavenJob.groovy"));

ExecuteDslScripts e = new ExecuteDslScripts();
e.setTargets("mavenProject/mavenJob.groovy");
p.getBuildersList().add(e);

// Assert if build succeeded, if the Script executed includes the 
echo message
FreeStyleBuild b = 
j.assertBuildStatusSuccess(p.scheduleBuild2(0).get());

Cheers

Charles



-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/e4dfe5ba-f5be-4fad-9366-0d9566b5d281n%40googlegroups.com.


Re: Jenkinsfile Accessing variables from shared libraries

2020-12-17 Thread Ven H
In my case, I do the following. I have a GlobalVars.groovy file with below
content.

#!/usr/bin/env groovypackage com..;public
class GlobalVars {   static String myVar = ""}

Then in my Shared Library class, I use it like below.

GlobalVars.myVar = ""

Try it like this and check. Here, I have declared the class as Public
and the variable as static. That's the only difference.


Regards,
Venkatesh


On Thu, Dec 17, 2020 at 12:56 PM Adrian Wyssmann  wrote:

> I see several problem
>
>1. using ' will not expand the parameters is your call should be sh
>"echo ${CustomMessage}"
>2. CustomMessage is declared within the class Vars(), so you cannot
>access it
>
> You could implement a getter an then do something like this "echo
> ${globalVars.getCustomMessage()}"
>
> Maybe others have better ideas...?
> On Thursday, December 17, 2020 at 12:10:19 AM UTC+1 netwar...@gmail.com
> wrote:
>
>> Hello there
>> Maybe this is a very basic question, but am not being able to access a
>> global variable
>> from a shared library from a Jenkinsfile
>>
>> under vars I defined something like this
>>
>> globalVars.groovy
>> class Vars () {
>>
>> def CustomMessage = "This is a new deployment"
>>
>> }
>>
>> from my Jenkinsfile
>>
>> library identifier: 'globalVars@master', \
>>  retriever: modernSCM([$class: 'GitSCMSource', \
>> credentialsId: 'tfsservice', \
>> remote: 'http://myrepo.com', \
>> traits: [gitBranchDiscovery()]])
>>
>>
>> pipeline {
>>
>> stages {
>> stage('Do Some Stuff') {
>>   steps {
>> script {
>>
>>sh 'echo ${CustomMessage}'
>>
>>   }
>> }
>>   }
>> }
>>
>> }
>>
>> But the above returns nothing, what am I doing wrong?
>> Thanks
>> Regards
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Jenkins Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jenkinsci-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jenkinsci-users/099e3aee-dffc-459d-8eb5-d64204effb58n%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/CAPp28eojNNe9xEFtD8rNhxXnY5Z118zrhGLJRVsqSqaL4qiHFA%40mail.gmail.com.