Last active
July 2, 2018 21:01
-
-
Save BillyPurvis/b1e498f8c9ba46a16d3ae0842358c8fb to your computer and use it in GitHub Desktop.
Support Multiple Servers Jenkins
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" | |
// 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" | |
} | |
// 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"' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment