如何 运行 Gitlab CI 工作与不同 运行ners 相互独立?

How to run the Gitlab CI jobs with different runners independent from each other?

描述

正如我从 and Gitlab-CI official documentation 中发现的那样,通过使用 tags 关键字,可以为单个项目触发不同的 运行 用户。所以我在我的服务器上注册了不同的 运行ners(每个(登台和生产)服务器一个 运行ner),带有 dashboard_stagingdashboard_production 标签名称。

一切正常,为了正确启动 gitlab-运行ner,我执行了以下命令:

sudo gitlab-runner verify  # Everything was ok
sudo gitlab-runner start  # It was started successfully on both servers

然后我提交了更改并将它们推送到Gitlab上并成功触发。

问题

  1. 我已经执行了上述命令,但其中一个管道仍在等待 运行ner。

  1. 由于 build 阶段尚未完全完成,因此它不会在其工作已完成的标签上进行。

代码

stages:
  - test
  - build
  - deploy

cache:
  untracked: true
  key:
    files:
      - yarn.lock
  paths:
    - node_modules/
    - .yarn

dashboard:test:staging:
  stage: test
  tags:
    - dashboard_staging
  when: manual
  before_script:
    - echo "do sth"
  only:
    - staging
  except:
    changes:
      - 'docker/**/*'
      - '*.md'
  script:
    - echo "do sth"

dashboard:test:production:
  stage: test
  tags:
    - dashboard_production
  when: manual
  before_script:
    - echo "do sth"
  only:
    - staging
  except:
    changes:
      - 'docker/**/*'
      - '*.md'
  script:
    - echo "do sth"

dashboard:build:staging:
  stage: build
  tags:
    - dashboard_staging
  only:
    - staging
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY  
  script:
    - echo "do sth"

dashboard:build:production:
  stage: build
  tags:
    - dashboard_production
  only:
    - staging
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY  
  script:
    - echo "do sth"


dashboard:deploy:staging:
  stage: deploy
  tags:
    - dashboard_staging
  except:
    changes:
      - 'docker/**/*'
      - '*.md'
  only:
    - staging
  before_script:
    - echo "do sth"
  script:
    - echo "do sth"


dashboard:deploy:production:
  stage: deploy
  tags:
    - dashboard_production
  except:
    changes:
      - 'docker/**/*'
      - '*.md'
  only:
    - staging
  before_script:
    - echo "do sth"
  script:
    - echo "do sth"

问题

如果我理解你的意思是正确的,你希望“暂存”作业 运行 独立于“生产”作业,并忽略同一阶段的其他作业可能还没有完成。

这就是 needs 关键字的用途 (reference)。由于您尚未在作业中定义任何 needs 关键字,因此每个作业都将等待整个前一个 stage 完成后才会开始。 GitLab CI 的默认行为是 运行 一个阶段内的所有作业并行,然后每个阶段串联。然后,您可以使用 needs 关键字覆盖它,以独立于它们所在的阶段启动作业。

尝试以下操作:

stages:
  - test
  - build
  - deploy

cache:
  untracked: true
  key:
    files:
      - yarn.lock
  paths:
    - node_modules/
    - .yarn

dashboard:test:staging:
  stage: test
  tags:
    - dashboard_staging
  when: manual
  before_script:
    - echo "do sth"
  only:
    - staging
  except:
    changes:
      - 'docker/**/*'
      - '*.md'
  script:
    - echo "do sth"

dashboard:test:production:
  stage: test
  tags:
    - dashboard_production
  when: manual
  before_script:
    - echo "do sth"
  only:
    - staging
  except:
    changes:
      - 'docker/**/*'
      - '*.md'
  script:
    - echo "do sth"

dashboard:build:staging:
  stage: build
  tags:
    - dashboard_staging
  only:
    - staging
  needs: ["dashboard:test:staging"]
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY  
  script:
    - echo "do sth"

dashboard:build:production:
  stage: build
  tags:
    - dashboard_production
  only:
    - staging
  needs: ["dashboard:test:production"]
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY  
  script:
    - echo "do sth"


dashboard:deploy:staging:
  stage: deploy
  tags:
    - dashboard_staging
  except:
    changes:
      - 'docker/**/*'
      - '*.md'
  only:
    - staging
  needs: ["dashboard:build:staging"]
  before_script:
    - echo "do sth"
  script:
    - echo "do sth"


dashboard:deploy:production:
  stage: deploy
  tags:
    - dashboard_production
  except:
    changes:
      - 'docker/**/*'
      - '*.md'
  only:
    - staging
  needs: ["dashboard:build:production"]
  before_script:
    - echo "do sth"
  script:
    - echo "do sth"

我会 post 为未来的读者详细说明我的问题和疑问是如何解决的。

问题 1:如何在部署阶段解决这个未决问题?

回答 1:

执行以下三个命令后问题得到解决:

sudo gitlab-runner verify
sudo gitlab-runner start
sudo gitlab-runner run

P.S. 如果你不是 root 并且想在没有 sudo 的情况下执行 gitlab-运行ner(使用你的用户权限,你可以很容易地通过 usermod -aG sudo gitlab-runner 命令将 gitlab-运行ner 添加到 sudoers。)

更多详情,您可以访问以下链接:

  • GitLab CI builds remains pending

问题2: 有没有办法运行定义的标签相互独立?

回答 2: 正如@Patrick 所提到的,gitlab-ci yaml 文件中存在一个名为 needs 的标签,它完全按预期工作。

stages:
  - test
  - build
  - deploy

cache:
  untracked: true
  key:
    files:
      - yarn.lock
  paths:
    - node_modules/
    - .yarn

dashboard:test:staging:
  stage: test
  tags:
    - dashboard_staging
  when: manual
  before_script:
    - echo "do sth"
  only:
    - staging
  except:
    changes:
      - 'docker/**/*'
      - '*.md'
  script:
    - echo "do sth"

dashboard:test:production:
  stage: test
  tags:
    - dashboard_production
  when: manual
  before_script:
    - echo "do sth"
  only:
    - staging
  except:
    changes:
      - 'docker/**/*'
      - '*.md'
  script:
    - echo "do sth"

dashboard:build:staging:
  stage: build
  tags:
    - dashboard_staging
  only:
    - staging
  needs: ["dashboard:test:staging"]
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY  
  script:
    - echo "do sth"

dashboard:build:production:
  stage: build
  tags:
    - dashboard_production
  only:
    - staging
  needs: ["dashboard:test:production"]
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY  
  script:
    - echo "do sth"


dashboard:deploy:staging:
  stage: deploy
  tags:
    - dashboard_staging
  except:
    changes:
      - 'docker/**/*'
      - '*.md'
  only:
    - staging
  needs: ["dashboard:build:staging"]
  before_script:
    - echo "do sth"
  script:
    - echo "do sth"


dashboard:deploy:production:
  stage: deploy
  tags:
    - dashboard_production
  except:
    changes:
      - 'docker/**/*'
      - '*.md'
  only:
    - staging
  needs: ["dashboard:build:production"]
  before_script:
    - echo "do sth"
  script:
    - echo "do sth"

有关详细信息,您可以阅读 official documentation of Gitlab CI