Jenkins pipeline example
pipeline{
agent any
environment {
DOCKER_USER='robot_config'
DOCKER_PASS='xxxxxxxx'
DOCKER_REGISTRY='harbor.google.com'
DOCKER_PROJECT='config-server-test'
IMAGE_NAME='config-server'
TAG='latest'
SERVICE='config-server'
ENV='beta'
NAMESPACE='jenkins'
SERVER_URL='https://vks-api/v1/k8s/97afc070-xxxx-4171-xxxx-yyyyy'
SVC_TOKEN='yyyyyy'
}
stages{
stage('Checkout config-server git repository') {
steps {
git branch: 'feature/jenkins',
credentialsId: 'git.x.com_credential_for_Github_Organization_pipeline',
url: 'git@git.com:harmmy/cs.git'
withCredentials([string(credentialsId: 'password', variable: 'SECRET')]) { //set SECRET with the credential content
echo "My secret text is '${SECRET}'"
script {
def secret = "${SECRET}"
echo secret
if(secret == "xxx"){
echo 'xxx'
} else {
echo 'yyy'
}
}
}
}
}
stage('Build jar file') {
steps {
sh './gradlew :config-server:build -x test'
}
}
stage('Docker build and push to harbor') {
steps {
sh 'docker build -t $DOCKER_REGISTRY/$DOCKER_PROJECT/$IMAGE_NAME-$ENV:$TAG -f ./config-server/Dockerfile .'
sh 'echo $DOCKER_PASS | docker login $DOCKER_REGISTRY -u $DOCKER_USER --password-stdin'
sh 'docker push $DOCKER_REGISTRY/$DOCKER_PROJECT/$IMAGE_NAME-$ENV:$TAG'
}
}
stage('Helm deploy') {
agent {
docker {
image 'dtzar/helm-kubectl:3.9.2'
// Run the container on the node specified at the
// top-level of the Pipeline, in the same workspace,
// rather than on a new node entirely:
reuseNode true
args '-u root'
}
}
steps {
sh '''
kubectl config set-cluster jenkins --server=$SERVER_URL --insecure-skip-tls-verify
kubectl config set-context jenkins --cluster=jenkins
kubectl config set-credentials user --token=$SVC_TOKEN
kubectl config set-context jenkins --user=user
kubectl config use-context jenkins
'''
sh '''
echo "Run Helm upgrade $SERVICE on $ENV (namespace:$NAMESPACE, image_tag:$TAG, branch/tag:$BRANCH)"
helm upgrade $NAMESPACE $SERVICE/chart \
-n $NAMESPACE \
-i \
-f $SERVICE/chart/values/$ENV/values.yaml \
--timeout 2m0s \
--atomic \
--cleanup-on-fail \
--set image.tag=$TAG \
'''
}
}
}
}
Comments
Post a Comment