Created
June 11, 2020 22:35
-
-
Save unacceptable/f9312eefb70508849122922c0800d05c to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env groovy | |
def call(Map parameters) { | |
env = parameters.env | |
service = parameters.service | |
region = parameters.region | |
withCredentials([ | |
string(credentialsId: 'GITHUB_ACCESS_TOKEN', variable: 'GITHUB_ACCESS_TOKEN') | |
]) { | |
sh """ | |
set -x | |
export ON_JENKINS="true" | |
export TERM=xterm | |
main(){ | |
get_kube_config | |
remove_old_deploys | |
} | |
get_kube_config(){ | |
case ${env} in | |
data-dev) account_id=123456789012 ;; | |
data-prod) account_id=123456789012 ;; | |
dev) account_id=123456789012 ;; | |
qa) account_id=123456789012 ;; | |
prod) account_id=123456789012 ;; | |
*) echo "Invalid account, please update case statement in Jenkins scripts" && exit 1 ;; | |
esac | |
echo "Setting AWS Credentials for ${env}" | |
session_json=\$(aws sts assume-role --role-arn arn:aws:iam::\${account_id}:role/jenkins-admin-role --role-session-name local-tmp) | |
export AWS_ACCESS_KEY_ID=\$(echo "\${session_json}"|jq -r '.Credentials.AccessKeyId') | |
export AWS_SECRET_ACCESS_KEY=\$(echo "\${session_json}"|jq -r '.Credentials.SecretAccessKey') | |
export AWS_SESSION_TOKEN=\$(echo "\${session_json}"|jq -r '.Credentials.SessionToken') | |
echo "Setting kube config" | |
aws eks \ | |
update-kubeconfig \ | |
--name ${env}-${region}-eks \ | |
--region ${region} | |
} | |
remove_old_deploys(){ | |
helm list \ | |
"airflow-data-dev-*" \ | |
--output json | \ | |
jq -r --arg NAMESPACE "${env}" --arg DAYS 7 '.Releases[] | { | |
Name: .Name, | |
Updated: .Updated | strptime("%a %b %d %H:%M:%S %Y") | mktime, | |
Namespace: .Namespace | |
} | select( | |
.Updated < (now | . - 60*60*24*( \$DAYS | tonumber ) ) and .Namespace != \$NAMESPACE | |
).Name' | xargs -r -n1 helm delete --purge | |
} | |
main | |
""" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment