pipeline-build-flask.jenkinsfile 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. pipeline {
  2. agent any
  3. environment {
  4. CODING_DOCKER_REG_HOST = "${CCI_CURRENT_TEAM}-docker.pkg.${CCI_CURRENT_DOMAIN}"
  5. CODING_DOCKER_IMAGE_NAME = "${PROJECT_NAME.toLowerCase()}/${DOCKER_REPO_NAME}/${DOCKER_IMAGE_NAME}"
  6. }
  7. stages {
  8. stage("检出") {
  9. steps {
  10. checkout(
  11. [$class: 'GitSCM',
  12. branches: [[name: GIT_BUILD_REF]],
  13. userRemoteConfigs: [[
  14. url: GIT_REPO_URL,
  15. credentialsId: CREDENTIALS_ID
  16. ]]]
  17. )
  18. }
  19. }
  20. stage('安装依赖') {
  21. steps {
  22. sh "pip3.7 install -r requirements.txt"
  23. }
  24. }
  25. stage('单元测试') {
  26. steps {
  27. sh "pytest --junitxml=reports/test-result.xml"
  28. }
  29. post {
  30. always {
  31. // 收集测试报告
  32. junit 'reports/**/*.xml'
  33. }
  34. }
  35. }
  36. stage('构建镜像并推送到 CODING Docker 制品库') {
  37. steps {
  38. sh "docker build -t ${CODING_DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_VERSION} -f ${DOCKERFILE_PATH} ${DOCKER_BUILD_CONTEXT}"
  39. useCustomStepPlugin(
  40. key: 'coding-public:artifact_docker_push',
  41. version: 'latest',
  42. params: [
  43. image:"${CODING_DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_VERSION}",
  44. repo:"${DOCKER_REPO_NAME}"
  45. ]
  46. )
  47. }
  48. }
  49. stage("部署到远端服务") {
  50. steps {
  51. script {
  52. def remoteConfig = [:]
  53. remoteConfig.name = "my-remote-server"
  54. remoteConfig.host = "${REMOTE_HOST}"
  55. remoteConfig.port = "${REMOTE_SSH_PORT}".toInteger()
  56. remoteConfig.allowAnyHosts = true
  57. withCredentials([
  58. sshUserPrivateKey(
  59. credentialsId: "${REMOTE_CRED}",
  60. keyFileVariable: "privateKeyFilePath"
  61. ),
  62. usernamePassword(
  63. credentialsId: "${CODING_ARTIFACTS_CREDENTIALS_ID}",
  64. usernameVariable: 'CODING_DOCKER_REG_USERNAME',
  65. passwordVariable: 'CODING_DOCKER_REG_PASSWORD'
  66. )
  67. ]) {
  68. // SSH 登陆用户名
  69. remoteConfig.user = "${REMOTE_USER_NAME}"
  70. // SSH 私钥文件地址
  71. remoteConfig.identityFile = privateKeyFilePath
  72. // 请确保远端环境中有 Docker 环境
  73. sshCommand(
  74. remote: remoteConfig,
  75. command: "docker login -u ${CODING_DOCKER_REG_USERNAME} -p ${CODING_DOCKER_REG_PASSWORD} ${CODING_DOCKER_REG_HOST}",
  76. sudo: true,
  77. )
  78. sshCommand(
  79. remote: remoteConfig,
  80. command: "docker rm -f python-flask-app | true",
  81. sudo: true,
  82. )
  83. // DOCKER_IMAGE_VERSION 中涉及到 GIT_LOCAL_BRANCH / GIT_TAG / GIT_COMMIT 的环境变量的使用
  84. // 需要在本地完成拼接后,再传入到远端服务器中使用
  85. DOCKER_IMAGE_URL = sh(
  86. script: "echo ${CODING_DOCKER_REG_HOST}/${CODING_DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_VERSION}",
  87. returnStdout: true
  88. )
  89. sshCommand(
  90. remote: remoteConfig,
  91. command: "docker run -d -p 5000:5000 --name python-flask-app ${DOCKER_IMAGE_URL}",
  92. sudo: true,
  93. )
  94. echo "部署成功,请到 http://${REMOTE_HOST}:5000 预览效果"
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }