Created
July 2, 2018 21:05
-
-
Save BillyPurvis/927fe951f748007fc6c0e001f62eedcb to your computer and use it in GitHub Desktop.
Jenkins With Email Notifications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String branchName = env.BRANCH_NAME | |
String gitCredentials = "CREDENTIAL_ID" | |
String repoUrl = "https://github.com/username/repo-name.git" | |
// Server Credentials ID | |
String sshCredentials = "" // Defaults to empty to avoid accidental releases | |
String devServerCredentials = "CREDENTIAL_ID" | |
String stagingCredentials = "CREDENTIAL_ID" | |
String productionCredentials = "CREDENTIAL_ID" | |
String ip = "" // Server IP goes here | |
String usr_dir = "" // Server user and path | |
node { | |
// Determine the branch and relevant SSH Credentials to use | |
if (branchName == "server/dev-server") { | |
sshCredentials = devServerCredentials | |
usr_dir = "USER_DIR" | |
ip = "SERVER_IP" | |
} else if (branchName == "server/staging") { | |
sshCredentials = stagingCredentials | |
usr_dir = "USER_DIR" | |
ip = "SERVER_IP" | |
} else if (branchName == "master") { | |
sshCredentials = productionCredentials | |
usr_dir = "USER_DIR" | |
ip = "SERVER_IP" | |
} | |
try { | |
// Start Stages | |
stage('Clone') { | |
// Clones the repository from the current branch name | |
echo 'Make the output directory' | |
sh 'mkdir -p build' | |
echo 'Cloning files from (branch: "' + branchName + '" )' | |
dir('build') { | |
git branch: branchName, credentialsId: gitCredentials, url: repoUrl | |
} | |
} | |
stage('Build') { | |
echo 'Building Project...' | |
dir('build') { | |
sh 'tar -czf buildFiles.tar.gz app composer.* config cron hooks package-lock.json package.json public resources routes server.php' | |
} | |
} | |
stage('Distribute') { | |
echo 'Deploying code to the mainframe *cue Matrix theme*' | |
sshagent([sshCredentials]) { | |
sh 'scp -oStrictHostKeyChecking=no build/buildFiles.tar.gz ' + usr_dir + '@' + ip + ':/home/' + usr_dir + '/public_html' | |
} | |
} | |
stage('Clean') { | |
echo 'Removing old files...' | |
sshagent([sshCredentials]) { | |
sh 'ssh ' + usr_dir + '@' + ip + ' "cd public_html && rm -rf config hooks package-lock.json package.json public resources routes"' | |
} | |
} | |
stage('Deploy') { | |
echo "Summoning our lord and saviour tar to unzip files " | |
sshagent([sshCredentials]) { | |
sh 'ssh ' + usr_dir + '@' + ip + ' "cd public_html && tar -xf buildFiles.tar.gz && rm -f buildFiles.tar.gz && ./post-deployment.sh"' | |
sh 'ssh ' + usr_dir + '@' + ip + ' "cd public_html && php artisan cache:clear && php artisan view:clear"' | |
} | |
} | |
} catch (Exception e) { | |
throw e | |
error 'There was an error - sending email notification' | |
// LIGHT THE BEACONS, THE BUILD CALLS FOR AID! | |
notifyBuildStatus('Build Failed') | |
} | |
} | |
// Handles sending email | |
def notifyBuildStatus(status) { | |
String jobName = java.net.URLDecoder.decode(env.JOB_NAME, "UTF-8"); | |
// Sends email notifying of build status | |
emailext ( | |
to: "[email protected]", | |
subject: "${status}: Job '${jobName} [${env.BUILD_NUMBER}]'", | |
body: """${status}: Job '${jobName} [${env.BUILD_NUMBER}]' | |
<br/> Check Jenkins""" | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment