Skip to content

Instantly share code, notes, and snippets.

@pedroamador
Last active March 1, 2019 14:56
Show Gist options
  • Save pedroamador/8b8188d87b97dc9ecb0508cef6df40d2 to your computer and use it in GitHub Desktop.
Save pedroamador/8b8188d87b97dc9ecb0508cef6df40d2 to your computer and use it in GitHub Desktop.
Jenkins Declarative Pipeline Deploy Combo
#!groovy
@Library('github.com/red-panda-ci/jenkins-pipeline-library@v2.6.2') _
// Initialize global config
cfg = jplConfig('testproject', 'backend' ,'', [hipchat: '', slack: '', email: 'pedroamador.rodriguez+testproject@gmail.com'])
pipeline {
agent any
parameters {
string(defaultValue: "develop", description: 'What source branch?', name: 'branchName')
choice(choices: ['dev', 'staging', 'quality', 'live'], description: 'What target environmebnt?', name: 'targetName')
}
stages {
stage ('Initialize') {
steps {
checkout scm: [$class: 'GitSCM', branches: [[name: '*/' + branchName]], userRemoteConfigs: [[url: 'git@github.com:red-panda-ci/jenkins-pipeline-library.git']]]
}
}
stage ('Deploy to host') {
steps {
script {
sh 'pullremote.sh ' + targetName
}
}
}
}
post {
always {
jplPostBuild(cfg)
}
}
options {
timestamps()
ansiColor('xterm')
buildDiscarder(logRotator(artifactNumToKeepStr: '20',artifactDaysToKeepStr: '30'))
disableConcurrentBuilds()
skipDefaultCheckout()
timeout(time: 1, unit: 'DAYS')
}
}
#!/bin/bash
ssh -t -t root@$1 << EOF
set -eu
set -o pipefail
cd /opt/testproject
git --no-pager diff
git stash save "Deploy $(date '+%Y-%M-%d %H:%M:%S')"
git checkout .
git fetch -p
if [ "$1" == "dev" ] \
[ "$1" == "staging" ] || \
[ "$1" == "quality" ]; then
git checkout ${branchName}
fi
git pull
exit
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment