Skip to content

Instantly share code, notes, and snippets.

@keopx
Forked from juampynr/script.pipeline
Created October 11, 2017 07:09
Show Gist options
  • Save keopx/aa989eb7150bccdb802cbb112c59e032 to your computer and use it in GitHub Desktop.
Save keopx/aa989eb7150bccdb802cbb112c59e032 to your computer and use it in GitHub Desktop.
Jenkins job to sync Production into Develoment
String pro_domain = '2017.drupalcamp.es'
String dev_domain = 'dev.drupalcamp.es'
String drush_pro = "drush -l ${pro_domain} -r /var/www/${pro_domain}/current/web"
String drush_dev = "drush -l ${dev_domain} -r /var/www/${dev_domain}/current/web"
String files_pro = "/var/www/${pro_domain}/shared/web/sites/default/files/"
String files_dev = "/var/www/${dev_domain}/shared/web/sites/default/files/"
String rsyncOpts = '-azv --delete --exclude .php --exclude php --exclude styles'
String deploySshKeyId = 'some-key-id'
lock(resource: "dev.drupalcamp.es") {
parallel (
Files: {
node('master') {
//TODO: consider locking to avoid io contention
//TODO: consider unison instead of rsync for performance (a log cleanup process will be necesary)
echo '---- Sync Files ----------------------------------------------------------------'
stage('Sync files') {
tempSyncPlaybookFile = "ansible-sync-files_playbook.yml"
tempSyncInventoryFile = "ansible-sync-files_inventory"
//TODO: Improve
// Create playbook
writeFile encoding: 'utf8', file: "${tempSyncPlaybookFile}", text: """---
- name: 'sync files'
hosts: all
vars:
- src: ${files_pro}
- dst: ${files_dev}
- rsync_opts: ${rsyncOpts}
tasks :
- name: 'synchronize'
command: "rsync {{ rsync_opts }} {{ src }} {{ dst }}"
register: sync_files
- name: "debug synchronize"
debug: var=sync_files
"""
// Create inventory
writeFile encoding: 'utf8', file: "${tempSyncInventoryFile}", text: """
127.0.0.1 ansible_user=deploy ansible_ssh_extra_args='-o ForwardAgent=yes' # Switch to deploy user and forward keys for remote access
"""
ansiColor('xterm') {
// We need ssh-agent for remote access
sshagent(["${deploySshKeyId}"]) {
ansiblePlaybook extra: '-vvv', colorized: true, credentialsId: "${deploySshKeyId}", forks: 5, inventory: "${tempSyncInventoryFile}", playbook: "${tempSyncPlaybookFile}", sudoUser: null
}
}
//Cleanup
sh "rm ${tempSyncPlaybookFile} -f"
sh "rm ${tempSyncInventoryFile} -f"
}
}
},
DB: {
node('master') {
echo '---- Load DB ----------------------------------------------------------------'
stage('Load DB') {
sshagent(["${deploySshKeyId}"]) {
sh "ssh [email protected] -C '${drush_pro} sql-dump | ${drush_dev} sql-cli'"
}
}
echo '---- Sanitize DB ----------------------------------------------------------------'
stage('Sanitize DB') {
sshagent(["${deploySshKeyId}"]) {
String sanitizePass = 'some-password'
sh "ssh [email protected] -C '${drush_dev} sql-sanitize -y --sanitize-email=drupalcamp+user_%[email protected] --sanitize-password=${sanitizePass}'"
}
}
echo '---- Dump DB ----------------------------------------------------------------'
stage('Dump DB') {
String dumpFile = 'dcamp2017.sql.gz'
sshagent(["${deploySshKeyId}"]) {
sh "ssh [email protected] -C '${drush_dev} sql-dump' | gzip > ${dumpFile}"
}
archiveArtifacts artifacts: "${dumpFile}", fingerprint: true
sh "rm ${dumpFile}"
}
}
},
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment