Last active
March 29, 2018 16:51
-
-
Save layerlre/2417ab11c63b590750fbac90366aa53d to your computer and use it in GitHub Desktop.
Jenkinsfile with docker and kubernetes
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
#!groovy | |
if(env.BRANCH_NAME == 'master'){ | |
// ################# Production ###################### | |
node { | |
stage('Preparation') { // for display purposes | |
//Get code from a Git repository | |
git branch: 'master', url: '[email protected]:myaccount/my_repo.git' | |
} | |
stage('Build') { | |
//get git tag | |
tag = sh(returnStdout: true, script: "git tag --sort version:refname | tail -1").trim() | |
//cleanup | |
try { | |
sh "docker rmi myregistry.azurecr.io/myapp:${tag}" | |
sh 'docker rmi myregistry.azurecr.io/myapp:latest' | |
} catch (err) { | |
echo "Docker image name myregistry.azurecr.io/myapp not exist!" | |
} | |
//build image | |
sh "docker build -f Dockerfile-prod -t myregistry.azurecr.io/myapp:${tag} -t myregistry.azurecr.io/myapp:latest ." | |
//push image | |
withCredentials([azureServicePrincipal('xxxxxxxxxxxxxxxxxxxxxxxxxxxxx')]) { | |
sh 'az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID' | |
sh 'az acr login -n myregistry' | |
sh "docker push myregistry.azurecr.io/myapp:${tag}" | |
sh "docker push myregistry.azurecr.io/myapp:latest" | |
sh "kubectl apply -f k8s/myapp-deployment.yaml" | |
} | |
} | |
stage('deploy'){ | |
//sh 'kubectl' | |
// line notify | |
info = sh ( | |
script: 'git log -1 --branches=master', | |
returnStdout: true | |
) | |
sh "curl -X POST -H 'Content-Type:application/x-www-form-urlencoded' -H 'Authorization:Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' -d 'message=MyApp Release ${tag} completed [http://myapp.myaccount.com]\n---------------------\n${info}' https://notify-api.line.me/api/notify" | |
} | |
stage('Test') { | |
//todo test | |
} | |
} | |
}else{ | |
// ################# Develop ###################### | |
node { | |
stage('Preparation') { // for display purposes | |
//Get code from a Git repository | |
git branch: 'develop', url: '[email protected]:myaccount/my_repo.git' | |
} | |
stage('Build') { | |
//init .env | |
sh 'cat env/env_dev > .env' | |
//build image | |
sh 'COMPOSE_HTTP_TIMEOUT=200 docker-compose up --build -d' // build and run container by docker-compose | |
//line notify | |
info = sh ( | |
script: 'git log -1 --branches=develop', | |
returnStdout: true | |
) | |
sh "curl -X POST -H 'Content-Type:application/x-www-form-urlencoded' -H 'Authorization:Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' -d 'message=MyApp completed [http://myappdev.myaccount.com]\n---------------------\n${info}' https://notify-api.line.me/api/notify" | |
} | |
stage('Test') { | |
//todo test | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment