Last active
May 1, 2022 02:34
-
-
Save BillyPurvis/e10360d9050a0c512ba2df3fd34ef19d to your computer and use it in GitHub Desktop.
Jenkinsfile Boomerang Finished
This file contains 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" | |
// Emails | |
String devEmails = "[email protected], [email protected]" | |
String qaEmauls = "[email protected], [email protected]" | |
// 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) { | |
// UTF-8 Encoded strings aren't great for emails | |
// Be sure to allow these methods which Jenkins will prompt you due to security aspects | |
String jobName = java.net.URLDecoder.decode(env.JOB_NAME, "UTF-8"); | |
String branchName = env.BRANCH_NAME | |
String attachments = '' | |
if (branchName == "server/staging" || branchName == "master") { | |
createGitLog() | |
attachments = "build/CHANGELOG.md, build/git-log.log" | |
sendTo = devEmails + qaEmails // Defined Earlier | |
} | |
// Sends email notifying of build status | |
emailext ( | |
to: devEmails, | |
attachmentsPattern: attachments, | |
subject: "${status}: Job '${jobName} [${env.BUILD_NUMBER}]'", | |
body: """${status}: Job '${jobName} [${env.BUILD_NUMBER}]' | |
<br/> Check Jenkins""" | |
) | |
} | |
// Creates timestamp for git log | |
def createTime() { | |
def dateFormat = new SimpleDateFormat("EEE MMM dd") | |
def yearFormat = new SimpleDateFormat("YYYY") | |
def date = new Date() | |
return dateFormat.format(date) + ' 00:00:00 ' + yearFormat.format(date) | |
} | |
// Creates git log file since current day | |
def createGitLog() { | |
String time = createTime() | |
dir('build') { | |
sh "git log --name-only --since=\"${time}\" > git-log.log" | |
} | |
echo 'Git Log Created...' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment