Skip to content

Instantly share code, notes, and snippets.

@lepffm
Last active April 17, 2024 12:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lepffm/3f574cec27661a1f8da478d909243cfe to your computer and use it in GitHub Desktop.
Save lepffm/3f574cec27661a1f8da478d909243cfe to your computer and use it in GitHub Desktop.
jenkins pipeline script for cloudfront invalidattion
// 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
}
@ameyshah11
Copy link

ameyshah11 commented Mar 24, 2023

Hi,

Where have you installed jq, because i don't see any command related to jq installation in your code.
And when I tried apt-get or sudo commands to install jq I am getting error as command not found.

Or is there any other way to get the invalidation Id on run time without jq -r Inavalidation.Id

@lepffm
Copy link
Author

lepffm commented Mar 25, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment