1. stages:
    2. build
    3. update
    4. deploy
    5. test
    6. variables:
    7. REGISTRY: harbor.lazypg.com
    8. REGISTRY_IMAGE_TAG: harbor.lazypg.com/sloth/coupons-mgmt:test
    9. cache:
    10. key: modules
    11. paths:
    12. - node_modules/
    13. - dist/
    14. -------安装依赖及编译-------
    15. build:
    16. 切换到vue环境的镜像中 在该镜像下完成编译工作
    17. image: ebiven/vue-cli
    18. stage: build
    19. script:
    20. - npm install
    21. - npm run build-dev
    22. 只有代码pushtest分支时才会触发该步骤
    23. only:
    24. - test
    25. 使用tagtestgitlab-runner去构建
    26. tags:
    27. - test
    28. -------制作镜像并上传到私有镜像仓库-------
    29. update:
    30. 切换到docker环境的镜像中 在该镜像下完成编译镜像并推送镜像的工作
    31. image: docker:latest
    32. stage: update
    33. script:
    34. # 登入私有镜像仓库
    35. - docker login -u $HARBOR_USERNAME -p $HARBOR_PASSWORD $REGISTRY
    36. # 根据Dockerfile文件编译image镜像
    37. - docker build -t $REGISTRY_IMAGE_TAG .
    38. # push镜像到私有仓库
    39. - docker push $REGISTRY_IMAGE_TAG
    40. only:
    41. - test
    42. tags:
    43. - test
    44. ----------------部署-----------------
    45. deploy:
    46. 切换ubuntu作为deploy任务的镜像
    47. image: kroniak/ssh-client
    48. stage: deploy
    49. script:
    50. # Run ssh-agent (inside the build environment)
    51. - eval (ssh-agent -s) ## ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store ## We're using tr to fix line endings which makes ed25519 keys work ## without extra base64 encoding. ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556 ## - echo "SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    52. # 创建SSH目录并给它正确的权限
    53. - mkdir -p ~/.ssh
    54. - chmod 700 ~/.ssh
    55. # 给runner配置ssh登录不验证HostKey
    56. - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    57. # 采用docker-compose的方式部署服务,提前将项目中的docker-compose-ui.yml文件上传到目标服务器
    58. - scp -P SSHTESTSERVERPORT./dockercomposeui.ymlroot@SSH_TEST_SERVER_IP:/data/docker/
    59. # 使用ssh远程登录目标服务器,并拉取之前build上传好的镜像进行部署
    60. - ssh root@$SSH_TEST_SERVER_IP -p $SSH_TEST_SERVER_PORT "docker-compose -f /data/docker/docker-compose-ui.yml pull && docker-compose -f /data/docker/docker-compose-ui.yml up -d;"
    61. manual表示需要手动触发
    62. when: manual
    63. allow_failure: false
    64. only:
    65. - test
    66. tags:
    67. - test
    68. -------安装依赖及编译-------
    69. test:
    70. 切换到vue环境的镜像中 在该镜像下完成编译工作
    71. image: ebiven/vue-cli
    72. stage: build
    73. script:
    74. -
    75. 只有代码pushtest分支时才会触发该步骤
    76. only:
    77. - test
    78. 使用tagtestgitlab-runner去构建
    79. tags:
    80. - test