Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart Jenkins using Ansible and wait for it to come back?

I'm trying to restart the Jenkins service using Ansible:

- name: Restart Jenkins to make the plugin data available   service: name=jenkins state=restarted  - name: Wait for Jenkins to restart   wait_for:     host=localhost     port=8080     delay=20     timeout=300  - name: Install Jenkins plugins   command:     java -jar {{ jenkins_cli_jar }} -s {{ jenkins_dashboard_url }} install-plugin {{ item }}     creates=/var/lib/jenkins/plugins/{{ item }}.jpi   with_items: jenkins_plugins 

But on the first run, the third task throws lots of Java errors including this: Suppressed: java.io.IOException: Server returned HTTP response code: 503 for URL, which makes me think the web server (handled entirely by Jenkins) wasn't ready. Sometimes when I go to the Jenkins dashboard using my browser it says that Jenkins isn't ready and that it will reload when it is, and it does, it works fine. But I'm not sure if accessing the page is what starts the server, or what.

So I guess what I need is to curl many times until the http code is 200? Is there any other way?

Either way, how do I do that?

How do you normally restart Jenkins?

like image 719
ChocoDeveloper Avatar asked May 28 '14 18:05

ChocoDeveloper


People also ask

How long does it take for Jenkins to restart?

I also restarted the Jenkins service and it worked. It did take 3-4 minutes after I restarted the service for the page to load up, though. So make sure you're patient before moving on to something else.

Which menu command is used to restart Jenkins without waiting for build to complete?

Using url <jenkins_url>/safeRestart – Allows all running jobs to complete. New jobs will remain in the queue to run after the restart is complete. <jenkins_url>/restart – Forces a restart without waiting for builds to complete.


2 Answers

Using the URI module http://docs.ansible.com/ansible/uri_module.html

   - name: "wait for ABC to come up"      uri:        url: "http://127.0.0.1:8080/ABC"        status_code: 200      register: result      until: result.status == 200      retries: 60      delay: 1 
like image 178
chrism Avatar answered Sep 22 '22 17:09

chrism


I solved it using curl + Ansible's until, which I just learned about. It will request the page until the http status code is 200 OK.

- name: Wait untils Jenkins web API is available   shell: curl --head --silent http://localhost:8080/cli/   register: result   until: result.stdout.find("200 OK") != -1   retries: 12   delay: 5 
like image 45
ChocoDeveloper Avatar answered Sep 21 '22 17:09

ChocoDeveloper