首页 > 解决方案 > Gitlab Ci 在第二个工作之后运行一个工作

问题描述

我有 2 个生产服务器和 2 个手动作业,现在我想在第一个作业之后运行第二个作业。我尝试使用密钥:之后,需要但不工作

请告诉我如何在 PROD_1 作业完成后运行 PROD_2 作业并且不要when: manual在第二个作业中使用?

Deploy_Build_STAGE:
  variables:
    DEPLOY_DIR: "/var/www/my_stage_app"
  environment:
    name: STAGE
    url: https://stage.example.com/
  stage: Deploy
  tags:
    - stage
  only:
    - stage
  script:
    - preparing
    - cd drupal/
    - composer install -n --prefer-dist --ignore-platform-reqs --no-dev
    - yarn install
    - yarn build --no-progress
    - copy_files -s "./" -d "$DEPLOY_DIR/public_html/"
    - generate_drush_config
    - cd $DEPLOY_DIR/public_html/web/
    - drush cr
    - drush updb -y
    - drush cim -y
    - drush updb -y
    - drush cr
    - sudo systemctl restart nginx php7.1-fpm varnish


Deploy_Build_PROD_1:
  variables:
    DEPLOY_DIR: "/var/www/my_app/"
  environment:
    name: PROD_1
    url: https://example.com/
  stage: Deploy
  when: manual
#  allow_failure: false
  tags:
    - app1
  only:
    - new_prod
  script:
    - sudo systemctl stop nginx
    - preparing
    - cd drupal/
    - composer install -n --prefer-dist --ignore-platform-reqs --no-dev
    - yarn install
    - yarn build --no-progress
    - copy_files -s "./" -d "$DEPLOY_DIR/public_html/"
    - generate_drush_config
    - cd $DEPLOY_DIR/public_html/web/
    - test -d sites/default/files/ || mkdir sites/default/files/
    - drush cr
    - drush updb -y
    - drush cim -y
    - drush updb -y
    - drush cr
    - sudo systemctl restart nginx php7.1-fpm varnish




Deploy_Build_PROD_2:
  variables:
    DEPLOY_DIR: "/var/www/my_app/"
  environment:
    name: PROD_2
    url: https://example.com/
  stage: Deploy
  when: manual
  tags:
    - app2
  only:
    - new_prod
  script:
#    - sudo systemctl stop nginx
    - preparing
    - cd drupal/
    - composer install -n --prefer-dist --ignore-platform-reqs --no-dev
    - yarn install
    - yarn build --no-progress
    - copy_files -s "./" -d "$DEPLOY_DIR/public_html/"
    - generate_drush_config
    - cd $DEPLOY_DIR/public_html/web/
    - test -d sites/default/files/ || mkdir sites/default/files/
    - sudo systemctl restart nginx php7.1-fpm varnish

标签: continuous-integrationgitlabgitlab-ci

解决方案


为了保留已执行作业的顺序,请在您的 yaml 中定义阶段,然后作业将以相同的顺序执行。

stages:
  - first_stage
  - second_stage

job_1:
  stage: first_stage
...

job_2:
  stage: second_stage
...

或者您可以使用按顺序执行的默认阶段:

stages:
  - build
  - test
  - deploy

推荐阅读