Jenkinsfile 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. def label = "jenkins-slave-yunti_web"
  2. podTemplate(label: label, cloud: 'kubernetes', imagePullSecrets:['registryharbor'],
  3. idleMinutes:'1440',
  4. nodeSelector:'kubernetes.io/hostname=gke-yunti-cluster-1-pool-2-7499bd72-b5x7',
  5. slaveConnectTimeout:'600',
  6. containers: [
  7. containerTemplate(name: 'docker', image: 'docker:20.10.3-git', command: 'cat', ttyEnabled: true),
  8. containerTemplate(name: 'helm', image: 'registry-platform.linkerplus.com/google/cloud-sdk-helm:378.0.0-alpine', command: 'cat', ttyEnabled: true)],
  9. serviceAccount: 'jenkins',
  10. volumes: [
  11. hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')]
  12. ) {
  13. node(label) {
  14. container('docker')
  15. {
  16. stage('Prepare') {
  17. echo '1.Prepare Stage'
  18. checkout scm
  19. docker_host = 'registry-platform.linkerplus.com'
  20. script {
  21. build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
  22. if (env.BRANCH_NAME != null) {
  23. build_tag = "${env.BRANCH_NAME}-${build_tag}"
  24. }
  25. }
  26. docker_img_name = "${docker_host}/${env.JOB_NAME}:${build_tag}"
  27. echo "********** docker-img-name: ${docker_img_name} **********"
  28. }
  29. stage('Build') {
  30. echo '2.Build Docker Image Stage'
  31. sh "docker build -t ${docker_img_name} --target prod -f Dockerfile-gcp ."
  32. }
  33. stage('Push') {
  34. echo '3.Push Docker Image Stage'
  35. withCredentials([usernamePassword(credentialsId: 'ba682ee1-c90b-4248-843b-b6f5277104a3', passwordVariable: 'dockerPassword', usernameVariable: 'dockerUser')]) {
  36. sh "docker login -u ${dockerUser} -p ${dockerPassword} ${docker_host}"
  37. sh "docker push ${docker_img_name}"
  38. }
  39. }
  40. stage('clone k8s deploy') {
  41. echo '4.clone k8s deploy'
  42. branch_name = "v2.0.0-gcp"
  43. withCredentials([sshUserPrivateKey(credentialsId: '6984acee-7988-4e00-9768-b4af0c69ff0f', keyFileVariable: 'GITHUB_KEY')]) {
  44. sh 'echo ssh -i $GITHUB_KEY -l git -o StrictHostKeyChecking=no \\"\\$@\\" > run_ssh.sh'
  45. sh 'chmod +x run_ssh.sh'
  46. withEnv(["GIT_SSH=${WORKSPACE}/run_ssh.sh"]) {
  47. sh """
  48. cd ..
  49. rm -rf k8s
  50. git clone git@122.112.209.52:YunTi/k8s.git
  51. cd k8s
  52. git checkout ${branch_name}
  53. sed -i "s/tag:.*\\".*\\"/tag: \\"${build_tag}\\"/g" ./yunti/charts/dove/values.yaml
  54. git add -A
  55. if ! git diff-index --quiet HEAD; then
  56. git config --global user.email "leo@amtxts.com"
  57. git config --global user.name "leo"
  58. git commit -m "docker image [${build_tag}] build"
  59. git push origin ${branch_name}
  60. fi
  61. """
  62. }
  63. }
  64. }
  65. }
  66. container('helm'){
  67. stage('k8s helm upgrade') {
  68. echo '5.k8s helm upgrade'
  69. withCredentials([file(credentialsId: 'yunti-20220127', variable: 'GC_KEY')]) {
  70. sh """
  71. gcloud auth activate-service-account --key-file=${GC_KEY}
  72. gcloud container clusters get-credentials yunti-cluster-1 --zone asia-east1-a --project yunti-20220127
  73. cd ${WORKSPACE}/../k8s
  74. helm upgrade -i yunti yunti
  75. """
  76. }
  77. }
  78. }
  79. }
  80. }