Gitlab-ci:如何实现dayli自动化测试和手动部署

时间:2018-07-02 08:31:17

标签: angular gitlab gitlab-ci

所以我有这个Angular 6 / Material 6应用程序,并且我在它周围建立了一个gitlab来处理连续集成。

我不确定如何处理所需的处理: 我主要需要两个阶段:测试和部署

  • 禁用基于推送的gitlab-ci
  • 测试阶段必须由cron触发dayli,但此cron不得触发部署
  • 必须手动触发部署阶段,但随后应运行测试,并且在因果报应测试失败的情况下,不得部署部署阶段。

此刻我得到了这个会议:

image: node:8.9.4

stages:
  - test
  - deploy

cache:
  paths:
    - node_modules/

before_script:
  - echo -e [`date "+%d/%m/%y %H:%M:%S"`] '\E[0;36m=======> Starting before scripts commands'
  - apt-get update -qq && apt-get install -y -qq sshpass
  - apt-get --yes install npm
  - echo -e [`date "+%d/%m/%y %H:%M:%S"`] '\E[0;36m=======> Setting registry to point verdaccio'
  - npm set registry http://192.168.1.20:4873/
  - echo [`date "+%d/%m/%y %H:%M:%S"`] '//192.168.1.20:4873/:_authToken=${NPM_TOKEN}'>.npmrc
  - apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3
    libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4
    libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1
    libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6
    ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
  - echo -e [`date "+%d/%m/%y %H:%M:%S"`] '\E[0;36m=======> Installing all project libs'
  - npm install

# ********************************************* TESTS STAGE *********************************************
test_stage:
  stage: test
  environment: Staging
  script:
    - echo -e [`date "+%d/%m/%y %H:%M:%S"`] '\E[0;36m=======> Starting script part for test_stage'
    - npm install
    - ./node_modules/.bin/ng test --karma-config=karma.conf.js --watch=false
  only:
    - master
    - triggers
    # You want a cron to trigger the test build :
    # Include this line to crontab :
    # 0 4 * * * curl -X POST -F token=FOUND_IN_GITLAB -F ref=master -F "variables[IF YOU WANT SOME]=true" http://GITLAB_URL/api/v3/projects/PROJECT_NUMBER/trigger/builds
    # Get token in in gitlab section Triggers
    # Get PROJECT _NUMBER in gitlab section Triggers

  except:
    - pushes

# ********************************************* DEPLOY STAGE *********************************************
deploy_stage:
  stage: deploy
  environment: Staging
  only:
    - master

  script:
    - echo -e [`date "+%d/%m/%y %H:%M:%S"`] '\E[0;36m=======> Starting script part for deploy_stage'
    - echo -e [`date "+%d/%m/%y %H:%M:%S"`] '\E[0;36m=======> Compiling with NG BUILD'
    - ./node_modules/.bin/ng build --configuration=recette --prod
    - sshpass -V
    - export SSHPASS=$USER_PASS
    - echo -e [`date "+%d/%m/%y %H:%M:%S"`] '\E[0;36m=======> Deleting everything in archive folder'
    - sshpass -e ssh -o StrictHostKeyChecking=no USER@XX.XX.XX.XX -pPORT "rm -rf /CHEMIN_ARCHIVE/archive/*"
    - echo -e [`date "+%d/%m/%y %H:%M:%S"`] '\E[0;36m=======> Copying server files into archive folder'
    - sshpass -e ssh -o StrictHostKeyChecking=no USER@XX.XX.XX.XX -pPORT "cp -a /CHEMIN_SERVER/server/. /CHEMIN_ARCHIVEt/archive/"
    - echo -e [`date "+%d/%m/%y %H:%M:%S"`] '\E[0;36m=======> Deleting everything in server folder'
    - sshpass -e ssh -o StrictHostKeyChecking=no USER@XX.XX.XX.XX -pPORT "rm -rf /CHEMIN_SERVER/server/*"
    - echo -e [`date "+%d/%m/%y %H:%M:%S"`] '\E[0;36m=======> Sending dist folder to livraison folder'
    - sshpass -e scp -P PORT -o StrictHostKeyChecking=no -r ./dist USER@XX.XX.XX.XX:/CHEMIN_LIVRAISON/livraison
    - echo -e [`date "+%d/%m/%y %H:%M:%S"`] '\E[0;36m=======> Copying livraison files to server folder'
    - sshpass -e ssh -o StrictHostKeyChecking=no USER@XX.XX.XX.XX -pPORT "cp -a /CHEMIN_LIVRAISON/livraison/. /CHEMIN_SERVER/server/"
    - echo -e [`date "+%d/%m/%y %H:%M:%S"`] '\E[0;36m=======> Deleting everything in livraison folder'
    - sshpass -e ssh -o StrictHostKeyChecking=no USER@XX.XX.XX.XX -pPORT "rm -rf /CHEMIN_LIVRAISON/livraison/*"
  when: manual

但是如您所见,当:手册正在运行时。 意味着cron触发了构建,它进入了Tests阶段,然后跳过了部署阶段:

enter image description here

当然,按照计划,我可以手动触发此部署阶段,但随后不会触发测试。

那我该如何到达目标:当我手动触发部署构建时,如果可以运行测试,并且如果测试失败,则应该不启动部署,那将是一个很好的选择?

非常感谢。

1 个答案:

答案 0 :(得分:1)

这是一个可能的解决方案:

stages:
- test
- deploy

.test-template: &test-definition
  image: alpine:3.6
  stage: test
  script:
  - bash launch_test.sh

trigger-test:
  <<: *test-definition
  only:
  - triggers

manual-test:
  <<: *test-definition
  when: manual
  only:
  - master
  allow_failure: false

deploy:
  image: alpine:3.6
  stage: deploy
  script:
  - bash deploy.sh
  only:
  - master
  dependencies:
  - manual-test

您手动启动manual-test作业,如果成功,则启动deploy作业。

您还可以触发trigger-test仅启动测试。