Skip to content

Instantly share code, notes, and snippets.

@cmaggiulli
Last active November 4, 2021 20:36
Show Gist options
  • Select an option

  • Save cmaggiulli/2732dea9c879f61ca9a19e24bf8f0901 to your computer and use it in GitHub Desktop.

Select an option

Save cmaggiulli/2732dea9c879f61ca9a19e24bf8f0901 to your computer and use it in GitHub Desktop.
Sample declarative pipeline that copies a file from s3, encrypts the file using GPG, then SFTP's a file using an expect script
pipeline {
agent {
label 'jdk11'
}
triggers {
cron('30 7 * * 1-5')
}
options {
buildDiscarder( logRotator( numToKeepStr: "30" ) )
disableConcurrentBuilds()
timeout(time: 20, unit: 'MINUTES')
}
environment {
ENVIRONMENT = "live"
DATE = sh(returnStdout: true, script: 'date --date="${OFFSET} days ago" +%Y%m%d').trim()
BUCKET = "${env.ENVIRONMENT.toLowerCase() == "live" ? "files-prod" : "files-${env.ENVIRONMENT.toLowerCase()}"}"
KEY = "output/reports/files/"
JOB_NAME = "${env.JOB_NAME.substring(env.JOB_NAME.lastIndexOf('/') + 1)}"
}
stages {
stage('download') {
steps {
dir("$JOB_NAME") {
withAWS(region:'us-east-1') {
sh "aws s3 cp s3://$BUCKET/$KEY . --recursive"
}
}
}
}
stage('encrypt, sftp, and remove') {
when {
expression { findFiles(glob: "**/*.xlsx").size() > 0 }
}
stages {
stage('encrypt' ) {
steps {
script {
currentBuild.description = 'Files Found'
}
dir("$JOB_NAME") {
sh '''
zip -r files.zip ./*.xlsx
gpg --import file-key.asc
gpg --encrypt --recipient "Recipient 1" --trust-model always files.zip
'''
}
}
}
stage('sftp') {
steps {
dir("$JOB_NAME") {
sh 'chmod 0755 transfer.sh'
withCredentials([usernamePassword(credentialsId: 'sftp', passwordVariable: 'password', usernameVariable: 'username')]) {
sh './transfer.sh $username $password'
}
}
}
}
stage('remove') {
steps {
withAWS(region:'us-east-1') {
sh "aws s3 rm s3://$BUCKET/$KEY --recursive --exclude '*.txt' --include '*.xlsx'"
}
}
}
}
}
}
post {
always {
dir("$JOB_NAME") {
deleteDir()
}
}
}
}
#!/usr/bin/expect
set username [lindex $argv 0];
set password [lindex $argv 1];
spawn sftp -o StrictHostKeyChecking=no [email protected]
expect "*password:"
send "$password\n"
expect "*sftp>"
send "cd 'To Data' \r"
expect "*sftp>"
send "mput files.zip.gpg \r"
expect "*sftp>"
send "quit \r"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment