Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Switch `products` Elasticsearch alias between index versions on Jenkinsfile. Switching happens from `products_v1` to `products_v2`
#!/usr/bin/env groovy
node {
stage('checkout') {
checkout scm
}
stage("Switch alias between index versions") {
String esHost = params.esHost //example: localhost:9200
String oldIndexVersion = params.oldIndexVersion //example: v1
String newIndexVersion = params.newIndexVersion //example: v2
def productIndices = sh(script: "curl -f -XGET \"$esHost/_alias/products\"", returnStdout: true)
def allIndexNames = new groovy.json.JsonSlurper().parseText(productIndices).keySet()
for (idx in allIndexNames) {
String newIdx = idx.replaceAll(oldIndexVersion, newIndexVersion)
println("Switching products alias from ${idx} to ${newIdx}")
sh """
curl -f -XPOST "$esHost/_aliases" -H 'Content-Type: application/json' -d'
{
"actions" : [
{ "add" : { "index" : "'${newIdx}'", "alias" : "products" } },
{ "remove" : { "index" : "'${idx}'", "alias" : "products" } }
]
}
'
"""
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.