Skip to content

Instantly share code, notes, and snippets.

@nironkoren
Last active September 23, 2023 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nironkoren/0c59f02fd0c30421542df88e98cef880 to your computer and use it in GitHub Desktop.
Save nironkoren/0c59f02fd0c30421542df88e98cef880 to your computer and use it in GitHub Desktop.
Jenkinsfile: Ocean Right Sizing Pipeline
#!/usr/bin/env groovy
podTemplate(containers: [
containerTemplate(name: 'docker',
image: 'docker',
command: 'cat',
ttyEnabled: true),
containerTemplate(name: 'python',
image: 'python',
command: 'cat',
ttyEnabled: true),
containerTemplate(name: 'kubectl',
image: 'lachlanevenson/k8s-kubectl:v1.8.8',
command: 'cat',
ttyEnabled: true)
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock',
hostPath: '/var/run/docker.sock')
]) {
node(POD_LABEL) {
stage('Fetch Git') {
try {
container('docker') {
dir('repo') {
git url: 'https://github.com/nironkoren/flask-http.git',
branch: 'master'
}
}
}
catch (exc) {
println "Failed to fetch repository from Git"
throw(exc)
}
}
stage('Build Docker Image') {
container('docker') {
try {
sh("""
cd ./repo
docker build .
""")
}
catch (exc) {
println "Failed to build docker image"
throw(exc)
}
}
}
stage('Get Ocean Right Sizing') {
container('python') {
try {
git url: 'https://github.com/nironkoren/spotinst-sdk-python.git',
branch: 'master'
sh 'pip3 install -U requests PyYaml'
sh 'wget https://raw.githubusercontent.com/nironkoren/flask-http/master/get_sizing.py'
// Generating a 'kubectl' cmd based on Right Sizing recommendations
cmd_output = sh(script: ('python ./get_sizing.py -d <DEPLOYMENT> -r <RATIO> ' +
'-t <API_TOKEN> ' +
'-a <ACCOUNT_ID> -o <OCEAN_ID> -n <NAMESPACE>'), returnStdout: true).trim()
env.CMD_OUT = cmd_output
}
catch (exc) {
println "Failed to get suggestions from Ocean"
throw(exc)
}
}
}
stage('K8S Deploy') {
container('kubectl') {
if (CMD_OUT) {
try {
sh (script: ('${CMD_OUT} ' +
'--token $(cat /var/run/secrets/kubernetes.io/serviceaccount/token) ' +
'--certificate-authority /var/run/secrets/kubernetes.io/serviceaccount/ca.crt ' +
'--server https://kubernetes.default.svc.cluster.local'))
}
catch (exc) {
println "Failed to set deployment resources"
throw(exc)
}
}
else {
println "No adjustment needed"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment