Commit 59346074 authored by 卢浩元's avatar 卢浩元

Add Jenkinsfile-withCredentials

parent e4d99eb3
#!/usr/bin/env groovy
def projectProperties = [
[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '5']],
parameters([
string(name: 'REGISTRY_URL', defaultValue: 'harbor.poc.paas.com', description: 'docker仓库地址')
])
]
properties(projectProperties)
podTemplate(cloud: 'kubernetes', containers: [
containerTemplate(name: 'maven', image: 'harbor.k2software.com.cn/build/maven:3.6.0-jdk-8', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'docker', image: 'harbor.k2software.com.cn/build/docker:latest', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'kubectl', image: 'harbor.k2software.com.cn/build/k8s-kubectl:v1.14.0', command: 'cat', ttyEnabled: true)
],
volumes: [
hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock'),
hostPathVolume(hostPath: '/root/.kube', mountPath: '/root/.kube'),
hostPathVolume(hostPath: '/root/.m2', mountPath: '/root/.m2')
],
annotations: [
podAnnotation(key: "sidecar.istio.io/inject", value: "false")
]
) {
node(POD_LABEL) {
def gitCommit
def shortGitCommit
def previousGitCommit
container('maven') {
stage('checkout') {
checkout scm
sh 'printenv'
gitCommit = sh(script: "git rev-parse HEAD", returnStdout: true).trim()
shortGitCommit = "${gitCommit[0..10]}"
previousGitCommit = sh(script: "git rev-parse ${gitCommit}~", returnStdout: true)
echo "gitCommit = ${gitCommit}"
echo "shortGitCommit = ${shortGitCommit}"
echo "previousGitCommit = ${previousGitCommit}"
}
stage('pacakge') {
sh 'mvn clean package -Dmaven.test.skip=true -s ./settings.xml'
}
}
container('docker') {
stage('docker') {
withCredentials([usernamePassword(credentialsId: 'REGISTRY_URL', passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USER')]) {
sh "docker login -u ${DOCKER_USER} -p ${DOCKER_PASSWORD} ${params.REGISTRY_URL}"
sh "docker build . -t ${params.REGISTRY_URL}/demo/k8s-example:${shortGitCommit}"
sh "docker push ${params.REGISTRY_URL}/demo/k8s-example:${shortGitCommit}"
}
sh "docker rmi ${params.REGISTRY_URL}/demo/k8s-example:${shortGitCommit}"
}
}
//container('kubectl') {
// stage('k8s deploy') {
// sh "sed -i \"s/lusyoe\\/k8s-example/${params.REGISTRY_URL}\\/demo\\/k8s-example:${shortGitCommit}/g\" k8s-example.yaml"
// sh "kubectl --kubeconfig=/root/.kube/config apply -f k8s-example.yaml"
// }
//}
}
}
// vim: ft=groovy
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment