When executing jobs in Jenkins pipeline and a job is failed at night how do you resume it in the same night?

When executing selenium jobs in Jenkins pipeline and a job is failed at night how do you resume it in the same night?

Solution:

You can use Naginator plugin to achieve your intended behavior. Configure it as follows:

Install the plugin -> check the Post-Build action “Retry build
after failure” on your project’s configuration page.

If the build fails, it will be rescheduled to run again after the time you specified. You can choose how many times to retry running the job. For each consecutive unsuccessful build, you can choose to extend the waiting period.

Advertisements

passing parameters from groovy to python ( Jenkins job)

I am using groovy to call a python in my Jenkins job. i would like to pass few parameters also to the python and i need to use those in the python

I am calling the python like below

result = sh(returnStdout:true , script: '#!/bin/sh -e\n' + 'python test.py').trim()

The parameters i need to pass are jenkins build job parameters.

Can you suggest a solution to this problem.?

Solution:

Try:

cmd = """#!/bin/sh -e\n' + 'python test.py ${JENKINS_VAR_1} ${JENKINS_VAR_1}"""
result = sh(returnStdout:true , script: cmd).trim()

Assuming you already have the code in python to access the argument.

Terraform version error when deploying to AWS through jenkins?

I was deploying using terraform through Jenkins Terraform v0.10.7.
After a success deployment from my local machine using Terraform v0.11.1, I can not do it again from Jenkins, I have this error :

Terraform doesn't allow running any operations against a state
that was written by a future Terraform version. The state is
reporting it is written by Terraform '0.11.1'.

Solution:

  • Using v0.11.1 run: $ terraform destroy
  • Remove the .tfstate file
  • Using v0.10.7 (or any version you want to use from now on), run: $ terraform apply

How can Jenkinsfile extract value from console output?

How can I get Jenkins to extract a value from the console output that is generated by a command in a Jenkinsfile?

Specifically, how can I get Jenkins to extract the commit hash from the results of the checkout scm command in the following very simple Jenkinsfile?


Jenkinsfile


node {
    // Clean workspace before doing anything
    deleteDir()
    try {
        stage ('Clone') {
            checkout scm    
        }
    } catch (err) {
        currentBuild.result = 'FAILED'
        throw err
    }
}  


Output Of checkout scm command:


The Jenkins log prints the following as a result of running the checkout scm command in the above simplified Jenkinsfile:

Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
 > git init /var/jenkins_home/workspace/le-repo_sample-issue-branch-2WOFDGRDQWR367VAM7O26H2DKPTRVPDKRTGNRIS4AQNNFP7QIX2Q # timeout=10
Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git  
 > git --version # timeout=10  
 using GIT_ASKPASS to set credentials 
 > git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/sample-issue-branch:refs/remotes/origin/sample-issue-branch
 > git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/sample-issue-branch:refs/remotes/origin/sample-issue-branch # timeout=10
 > git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10
Fetching without tags
Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git  
using GIT_ASKPASS to set credentials 
 > git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/sample-issue-branch:refs/remotes/origin/sample-issue-branch
Checking out Revision 77cf12f42136efc77fecbcd1f761a54254278cb3 (sample-issue-branch)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 77cf12f42136efc77fecbcd1f761a54254278cb3
Commit message: "add whitespace"
 > git rev-list --no-walk e975fb4391677bc09f2056b3e8a6be62eda0b222 # timeout=10
[Bitbucket] Notifying commit build result


Restated Question:


Specifically, what do we add to the Jenkinsfile in order for the log to additionally print out the following at the end:

Commit hash is:  77cf12f42136efc77fecbcd1f761a54254278cb3  

This is obviously over simplified. The printed output in real life will go into a variable and be passed as an argument into a script. But for this OP, what specific syntax will enable Jenkins to extract the commit hash?

Solution:

If you need commit hash in particular, the checkout step’s documentation mentions an easy way to get it (rather than parse console output etc):

This step returns a Map of any variables the SCM plugin would set in a
Freestyle job, so if your SCM is git, you can do:

    def scmVars = checkout scm
    def commitHash = scmVars.GIT_COMMIT

    // or

    def commitHash = checkout(scm).GIT_COMMIT

Once you have it in a variable it’s just a matter of echoing it to see it in the log.

echo "Commit hash is: ${commitHash}"

Running unit tests after starting elasticsearch

I’ve downloaded and set up elasticsearch on an EC2 instance that I use to run Jenkins. I’d like to use Jenkins to run some unit tests that use the local elasticsearch.

My problem is that I haven’t found a way on how to start the elasticsearch locally and run the tests after, since the script doesn’t proceed after starting ES, because the job is not killed or anything.

I can do this by starting ES manually through SSH and then building a project with only the unit tests. However, I’d like to automate the ES launching.

Any suggestions on how I could achieve this? I’ve tried now using single “Execute shell” block and two “Execute shell” blocks.

Solution:

It is happening because you starting elasticsearch command in blocking way. It means command will wait until elasticsearch server is shutdown. Jenkins just keep waiting.

You can use following command

./elasticsearch 2>&1 >/dev/null &

or

nohup ./elasticsearch 2>&1 >/dev/null &

it will run command in non-blocking way.

You can also add small delay to allow elasticsearch server start

nohup ./elasticsearch 2>&1 >/dev/null &; sleep 5

Jenkins behind nginx (docker)

I have a nginx-container with the following location and upstream configuration:

upstream jenkins-docker {
  server jenkins:8080 fail_timeout=0;
}


# configuration file /etc/nginx/conf-files/jenkins-location.conf:
location /jenkins/ {
sendfile off;
  proxy_pass         http://jenkins-docker;
  proxy_redirect     off;
  proxy_http_version 1.1;

  proxy_set_header   Host              $host;
  proxy_set_header   X-Real-IP         $remote_addr;
  proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
  proxy_set_header   X-Forwarded-Proto $scheme;
  proxy_max_temp_file_size 0;

  #this is the maximum upload size
  client_max_body_size       10m;
  client_body_buffer_size    128k;

  proxy_connect_timeout      90;
  proxy_send_timeout         90;
  proxy_read_timeout         90;
  proxy_request_buffering    off; # Required for HTTP CLI commands in Jenkins > 2.54
}

Jenkins is in a docker container aswell. They are both connected to a docker bridge network. Inside nginx-container I can do:

curl jenkins:8080:

<html><head><meta http-equiv='refresh' content='1;url=/login?from=%2F'/>window.location.replace('/login?from=%2F');</head><body style='background-color:white; color:white;'>


Authentication required
<!--
You are authenticated as: anonymous
Groups that you are in:

Permission you need to have (but didn't): hudson.model.Hudson.Read
 ... which is implied by: hudson.security.Permission.GenericRead
 ... which is implied by: hudson.model.Hudson.Administer
-->

</body></html> 

nginx can communicate with jenkins.

In jenkins->manage Jenkins -> Configure System under “Jenkins Location” I changed the “Jenkins URL” to http://myIP/jenkins

When I type into my Browser myIp/jenkins it get redirect to http://myIp/login?from=%2Fjenkins%2F which results in a 404

When I change the location in nginx “location /jenkins/ {” just to “/” it works like a charm. Thats why I tried it with a rewrite:

rewrite ^/jenkins(.*) /$1 break;

When I do this I can access the jenkins dashboar with myIp/jenkis. But when I click on a menu item I get a 404

Solution:

You need to also set the –prefix command on your jenkins installation. Youi can do this in the jenkins.xml config file or by altering your command line arguments to include --prefix=/jenkins. The arguments can be seen at https://wiki.jenkins.io/display/JENKINS/Starting+and+Accessing+Jenkins

Jenkins DSL using the Build Blocker Plugin plugin

I am writing a Jenkins DSL script (groovy) that will create a Jenkins Job. One of the options I would like the job to have enabled is the box that reads “Block build if certain jobs are running”

I tried to use the “blockOn” code that I found here:
https://jenkinsci.github.io/job-dsl-plugin/#path/freeStyleJob-blockOn

But when I run my DSL script the job gets created and does NOT have the “Block build if certain jobs are running” box checked

Below is the entire DSL script that gets executed:

job('Testing-DSL') {
  blockOn(['2.Dummy_job', '1 .Dummy_job']) {
    blockLevel('GLOBAL')
    scanQueueFor('ALL')
} //closing blockOn section

      description('''\
This is just a template job that I use to reference how groovy code should look<br>
 ''')

logRotator(-1, 30, -1, -1)

parameters {
    choiceParam('CHOICE1', ['choice_option1', 'option2'], 'Some description for this param')
    stringParam('STRING1', 'Default_Value_string1', 'Some description for this option')

} //closing parameters section

    steps {
    shell('''\
echo $CHOICE1
echo $STRING1
''')

} //closing steps section
} //closing job section

Solution:

Your script is working for me, the “Block build if certain jobs are running” box is checked.

You may need to restart Jenkins before using Job DSL if you installed some plugins. See also https://github.com/jenkinsci/job-dsl-plugin/wiki/Frequently-Asked-Questions#why-isnt-my-generated-job-like-i-was-expecting-there-was-no-error-when-the-seed-job-ran.