Terms Of Use

Privacy Policy

Featured snippets are MIT license

Gears & Masters

DevOpsnipp.com © 2020

medium.png

Production deployment by Jenkins Pipeline

Production deployment by Jenkins Pipeline

22

Votes

node() { try { stage ("Get the Code") { checkout scm: [$class: 'GitSCM', branches: [[name: "refs/tags/${params.COMMIT_ID}"]], extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'ansible-access']], userRemoteConfigs: [[url: 'ssh://git@192.168.1.206:2222/ansible/access.git']]] checkout scm: [$class: 'GitSCM', branches: [[name: '*/master']], extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'environment']], doGenerateSubmoduleConfigurations: false, userRemoteConfigs: [[url: 'ssh://git@192.168.1.206:2222/ansible/environment.git']]] sh 'pwd > workspace' } stage ("Install Application Dependencies") { sh 'sudo pip install --upgrade ansible==${ANSIBLE_VERSION} testinfra docker' } stage ("Execute role on host(s)") { dir("environment") { sh "ansible-playbook -i hosts -l 'all:!localhost:!staging' playbooks/ansible-access.yml" } } stage ("Test Role execution") { workspace = readFile('workspace').trim() withEnv(["WORKSPACE_DIR=${workspace}", "MOLECULE_INVENTORY_FILE=${workspace}/environment/hosts"]){ dir("environment") { sh "testinfra --connection=ansible --ansible-inventory=hosts --hosts='all:!localhost:!staging' ${WORKSPACE_DIR}/ansible-access/molecule/default/tests/test_default.py --verbose" } } } stage('Tag git'){ withEnv(["COMMIT_ID=${params.COMMIT_ID}_production"]){ dir("ansible-access") { sh '''#!/bin/bash if [[ $(git tag | grep "${COMMIT_ID}" | wc -l) -eq 1 ]] then echo "Tag already exists" else echo "Tag will be created" git config user.name "jenkins" git config user.email "jenkins@localhost" git tag -a $COMMIT_ID -m "Added tagging" git push --tags fi ''' } } } } catch(all) { currentBuild.result = "FAILURE" throw err }}

Vote Here

You must earn at least 1 vote on your snippets to be allowed to vote