PHP容器化demo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.3 KiB

3 years ago
  1. // Jenkinsfile.test
  2. // @version 180903:2
  3. // @author zhangxuhong <zhangxuhong@xitu.io>
  4. //
  5. def repoName = "gq-hello"
  6. def nginxRepoName = "${repoName}-nginx"
  7. def label = "worker-${UUID.randomUUID().toString()}"
  8. def registryHost = "harbor02.juejin.id/test/"
  9. def deployLocation = "test.kube01.lobj.juejin.id"
  10. podTemplate(
  11. label: label,
  12. containers: [
  13. containerTemplate(
  14. name: 'git',
  15. image: 'harbor02.juejin.id/infrastructure/git-1.8.3.1-centos:0.0.3',
  16. command: 'cat',
  17. ttyEnabled: true
  18. ),
  19. containerTemplate(
  20. name: 'dind',
  21. image: 'harbor02.juejin.id/infrastructure/docker-18.06.0-ce-dind:0.0.2',
  22. command: 'cat',
  23. ttyEnabled: true
  24. ),
  25. containerTemplate(
  26. name: 'kubectl',
  27. image: 'harbor02.juejin.id/infrastructure/kubectl-1.11.1-centos-with-cert-cluster01.lobj:latest',
  28. command: 'cat',
  29. ttyEnabled: true
  30. ),
  31. ],
  32. volumes: [
  33. hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
  34. ],
  35. cloud: deployLocation
  36. ){
  37. node(label) {
  38. // start deploy phrase
  39. stage("[1/4] check out"){
  40. container('git'){
  41. checkout scm
  42. sh "git rev-parse --short HEAD > commit-id"
  43. tag = readFile('commit-id').replace("\n", "").replace("\r", "")
  44. imageName = "${registryHost}${repoName}:${tag}"
  45. nginxImageName = "${registryHost}${nginxRepoName}:${tag}"
  46. env.IMAGE_NAME = imageName
  47. env.NGINX_IMAGE_NAME = nginxImageName
  48. }
  49. }
  50. stage("[2/4] Build"){
  51. container('dind'){
  52. sh "docker version"
  53. sh "docker build -f gq-hello.dockerfile -t ${imageName} ./"
  54. sh "docker build -f gq-hello-nginx.dockerfile -t ${nginxImageName} ./"
  55. }
  56. }
  57. stage("[3/4] Push"){
  58. container('dind'){
  59. sh "/usr/local/bin/docker-login.sh"
  60. sh "docker push ${imageName}"
  61. sh "docker push ${nginxImageName}"
  62. }
  63. }
  64. stage("[4/4] Deploy"){
  65. container('kubectl'){
  66. // deploy nginx
  67. sh "sed -i 's#__IMAGE__#$NGINX_IMAGE_NAME#g' ./config/kubernetes/gq-hello-nginx-deployment.yaml"
  68. sh "cat ./config/kubernetes/gq-hello-nginx-deployment.yaml"
  69. sh "kubectl --kubeconfig=/root/.kube/config apply -f ./config/kubernetes/gq-hello-nginx-service.yaml"
  70. sh "kubectl --kubeconfig=/root/.kube/config apply -f ./config/kubernetes/gq-hello-nginx-ingress.yaml"
  71. sh "kubectl --kubeconfig=/root/.kube/config apply -f ./config/kubernetes/gq-hello-nginx-deployment.yaml"
  72. // deploy repo
  73. sh "sed -i 's#__IMAGE__#$IMAGE_NAME#g' ./config/kubernetes/gq-hello-deployment.yaml"
  74. sh "cat ./config/kubernetes/gq-hello-deployment.yaml"
  75. sh "kubectl --kubeconfig=/root/.kube/config apply -f ./config/kubernetes/gq-hello-service.yaml"
  76. sh "kubectl --kubeconfig=/root/.kube/config apply -f ./config/kubernetes/gq-hello-deployment.yaml"
  77. }
  78. }
  79. }
  80. }