Friday, September 27, 2019

CICD -- Github/build package/upload artifact to Nexus/run the ansible-playbook to deploy


[8:52 PM, ] krishna17m: i have done the following
[8:55 PM, ] krishna17m: node{
    def mvnHome = tool name: 'maven3', type: 'maven'
    def mvnCmd = "${mvnHome}/bin/mvn"
    stage('Git Checkout'){
        git 'https://github.com/krishna17m/my-app.git'
    }
    stage('Create Package'){
        sh "${mvnCmd} clean package"
    }
    stage('Upload War To Nexus'){
        // sh "${mvnCmd} deploy"
     }
    stage('Run Ansible Deploy Playbook'){
       // Connect to Ansible Control Machine
        sshagent(['ansible-cm']) {
          def playbookCmd = 'ansible-playbook -i /home/ubuntu/ansible-3tier/dev-inventory  /home/ubuntu/ansible-3tier/deploy-to-tomcat.yml'
          sh "ssh -o StrictHostKeyChecking=no ubuntu@172.31.91.42 ${playbookCmd}"
}
}}
[8:56 PM, ] krishna17m: 1) taking code from github   2) build package 3) upload the war to Nexus 4) run the playbook to deploy  tomcat via ansible

[8:58 PM, ] krishna17m: the ansible playbook
[8:58 PM, ] krishna17m: ubuntu@ip-172-31-91-42:~/ansible-3tier$ more deploy-to-tomcat.yml
---
- hosts: tomcat
  become: True
  vars:
    - nexus_url: http://172.31.94.118:8081/repository/maven-releases/
    - nexus_user: admin
    - nexus_pwd: admin123
    - war_version: 0.0.3
  tasks:
   - name: Install lxml for nexus
     pip:
       name: lxml
   - name: remove old war file
     debug:
        msg: "deploying {{war_version}}"
        #    - name: Remove old files
     #      file:
    #     path:  "{{item}}"
    #    state: absent
    # with_items:
    #         - /opt/apache-tomcat-7.0.61/webapps/myweb.war
    #      - /opt/apache-tomcat-7.0.61/webapps/myweb

   - name: Download war file from Nexus
     maven_artifact:
        group_id: in.krishna
        artifact_id: myweb
        extension: war
        version: "{{war_version}}"
        repository_url: "{{nexus_url}}"
        username: "{{nexus_user}}"
        password: "{{nexus_pwd}}"
        dest: /opt/apache-tomcat-7.0.61/webapps/myweb.war
     notify:
       - Restart Tomcat

  handlers:
   - name: Restart Tomcat
     service:
       name: tomcat
       state: enabled
ubuntu@ip-172-31-91-42:~/ansible-3tier$
[8:59 PM, ] krishna17m: =========================OUTPUT ==========================
[8:59 PM, ] krishna17m: Started by user Krishna
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/cicdwithansible
[Pipeline] {
[Pipeline] tool
[Pipeline] stage
[Pipeline] { (Git Checkout)
[Pipeline] git
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/krishna17m/my-app.git # timeout=10
Fetching upstream changes from https://github.com/krishna17m/my-app.git
 > git --version # timeout=10
 > git fetch --tags --progress https://github.com/krishna17m/my-app.git +refs/heads/:refs/remotes/origin/
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision e8adfa974e082c52d0d0be9ba94ab8735015a0e4 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f e8adfa974e082c52d0d0be9ba94ab8735015a0e4
 > git branch -a -v --no-abbrev # timeout=10
 > git branch -D master # timeout=10
 > git checkout -b master e8adfa974e082c52d0d0be9ba94ab8735015a0e4
Commit message: "Update pom.xml"
 > git rev-list --no-walk e8adfa974e082c52d0d0be9ba94ab8735015a0e4 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Create Package)
[Pipeline] sh
[cicdwithansible] Running shell script
+ /opt/apache-maven-3.5.4/bin/mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< in.krishna:myweb >--------------------------
[INFO] Building my-app 0.0.3
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ myweb ---
[INFO] Deleting /var/lib/jenkins/workspace/cicdwithansible/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myweb ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/cicdwithansible/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ myweb ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /var/lib/jenkins/workspace/cicdwithansible/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myweb ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/cicdwithansible/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:testCompile (default-testCompile) @ myweb ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /var/lib/jenkins/workspace/cicdwithansible/tar…

No comments:

Post a Comment