How to store Jenkins job’s console output in another system? There are much opensource software is available to store the logs and retrieve logs in time. Jfrog is one of the most famous artifact solutions. Here, we will be using native Linux/Unix commands to pull the Jenkins logs and store it in local/NFS filesystem for auditing purpose. Since these article series talks mainly about calling ansible playbooks via Jenkins, the console output is mostly the playbook results.
Required Plugins:
1. Login to Jenkins and install the below-mentioned plugins.
Download & install the Post Build Task Plugin
Configure the Time-stamp Plugin:
2. Navigate to the “Configure system”.
3. Adjust the timestamp parameter according to the need. Ensure there is no empty space left since we will be saving the logs using this value.
Configure the Existing Job:
4. Pick any of the existing jobs and click on configure. Click on “Build Environment” and check “secret text“.
Note: It’s a Jenkins read-only credentials which I have selected to read the Jenkins job. If you have configured project-based security, ensure this user has enough permission to read the console logs.
5. Click on the Post Build Action tab and Add “Post Build Task”. Copy & paste the following contents into the script tab. Save the job. All the artifacts will be saved in directory “/home/ansible_artifacts/”. This directory can be included in log rotation to move the older logs to the archive directory using cronjob.
/bin/wget --auth-no-challenge --user $JENKINS_USR --password $JENKINS_PASS -O /home/ansible_artifacts/${JOB_BASE_NAME}_${REQ_INC}.${BUILD_TIMESTAMP}_console_output.log ${BUILD_URL}consoleText
Example:
Test our work:
6. Let’s trigger the build and see how the artifacts work. All the artifacts will be saved on the Jenkins server in the configured destination.
7. Here the console logs which shows that artifacts are stored in the configured location.
8. Login to the Jenkins server and check the logs generated by the job.
[root@ansible-server ansible_artifacts]# ls -lrt total 4 -rw-r--r-- 1 jenkins jenkins 1793 Feb 26 08:03 Invoke_Ansible_Playbook_df_check_REQU1234.2019-02-26-08:03:11_console_output.log [root@ansible-server ansible_artifacts]# pwd /home/ansible_artifacts [root@ansible-server ansible_artifacts]#
9. Let’s view the log.
[root@ansible-server ansible_artifacts]# more Invoke_Ansible_Playbook_df_check_REQU1234.2019-02-26-08\:03\:11_console_output.log Started by user admin Building on master in workspace /var/lib/jenkins/workspace/UnixArena_Project/Invoke_Ansible_Playbook_df_check Set build name. New build name is 'REQU1234' [Invoke_Ansible_Playbook_df_check] $ sshpass ******** /usr/bin/ansible-playbook /var/lib/awx/projects/UnixArena_Project/Unix_Arena_Demo_df.yaml -i /v ar/lib/awx/projects/UnixArena_Project/temp.hosts -f 5 -u **** -k -e FS_NAME=/var PLAY [all] ********************************************************************* TASK [Root FS usage] *********************************************************** changed: [192.168.3.151] TASK [debug] ******************************************************************* ok: [192.168.3.151] => { "msg": "System 192.168.3.151's /var FS utiliation is 64%" } PLAY RECAP ********************************************************************* 192.168.3.151 : ok=2 changed=1 unreachable=0 failed=0 Set build name. New build name is 'REQU1234' Performing Post build task... Match found for :build : True Logical operation result is TRUE Running script : /bin/wget --auth-no-challenge --user $JENKINS_USR --**** $JENKINS_PASS -O /home/ansible_artifacts/${JOB_BASE_NAME}_${REQ_INC}.${BUI LD_TIMESTAMP}_console_output.log ${BUILD_URL}consoleText [Invoke_Ansible_Playbook_df_check] $ /bin/sh -xe /tmp/jenkins8751864532556574210.sh + /bin/wget --auth-no-challenge --user **** --**** **** -O /home/ansible_artifacts/Invoke_Ansible_Playbook_df_check_REQU1234.2019-02-26-08:03:11_cons ole_output.log http://192.168.3.151:8080/job/UnixArena_Project/job/Invoke_Ansible_Playbook_df_check/24/consoleText --2019-02-26 08:03:14-- http://192.168.3.151:8080/job/UnixArena_Project/job/Invoke_Ansible_Playbook_df_check/24/consoleText Connecting to 192.168.3.151:8080... connected. [root@ansible-server ansible_artifacts]#
We have successfully configured artifacts within the Jenkins server on the given path. Hope this article is informative to you.
Share it! Comment it!! Be Sociable !!!
venkata sai krishna says
Hi, The article is very informative. But i am using github authentication with 2FA so how can i store my consoleText like yours in the s3 using a pipeline. I want to store the success log and failure log after every build to s3 using Jenkins pipeline can i expect any article on this. Thank you.