Last active
April 17, 2024 12:40
-
-
Save lepffm/3f574cec27661a1f8da478d909243cfe to your computer and use it in GitHub Desktop.
jenkins pipeline script for cloudfront invalidattion
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
// reference : https://stackoverflow.com/questions/56294317/how-to-programmatically-get-distribution-id-to-invalidate-cloudfront-cache | |
// prerequisite : aws cli, jq , aws credentials setting | |
pipeline { | |
parameters { | |
stringParam(description: 'your domain name', name: 'domain') | |
} | |
agent any | |
stages{ | |
stage('init'){ | |
steps{ | |
// sh 'brew install jq' | |
echo params.domain | |
} | |
}//stage | |
stage('run'){ | |
steps{ | |
script{ | |
cmd = "aws cloudfront list-distributions | jq '.DistributionList.Items[]|[ .Id, .Status, .Origins.Items[0].DomainName, .Aliases.Items[0] ] | @tsv ' -r" | |
def list = sh(script: cmd, returnStdout: true) | |
echo list | |
writeFile file: 'cloudfront_list.txt', text: list | |
if(params.domain){ | |
// get distribution id | |
get_cmd = """grep ${params.domain} 'cloudfront_list.txt' | awk '{print \$1}'""" | |
DISTRIBUTION_ID = sh(script: get_cmd, returnStdout: true) | |
println "$key = $DISTRIBUTION_ID" | |
// create invalidation id | |
create_cmd = """aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths "/*" | jq -r .Invalidation.Id""" | |
echo create_cmd | |
INVALIDATION_ID = sh(script: create_cmd, returnStdout: true) | |
// invalidate distribution and wait for finish | |
wait_cmd = """aws cloudfront wait invalidation-completed --distribution-id $DISTRIBUTION_ID --id $INVALIDATION_ID""" | |
sh(wait_cmd) | |
} | |
}//script | |
}//steps | |
}//stage | |
}//stages | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see jq download site