Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gitlab 相关 #320

Open
oakland opened this issue Sep 5, 2023 · 2 comments
Open

Gitlab 相关 #320

oakland opened this issue Sep 5, 2023 · 2 comments

Comments

@oakland
Copy link
Owner

oakland commented Sep 5, 2023

gitlab-runner 并发运行多个 pipeline

git-runner 默认的配置只会运行一个 pipeline,例如一个同事在 dev 更新了,dev 开始打包。在 dev 打包还没有完成的时候,另一个同事在 staging 更新了代码,那么 staging 的打包就会排队等在 dev 打包之后进行。如果想要同时运行多个 pipeline,例如这个时候 staging 也可以立即打包,就需要修改一下配置文件。

  • 通过 ps -ef | grep -i gitlab 命令查看配置文件的地址,/etc/gitlab-runner/config.toml
  • 通过 vim 打开 config.toml 文件,将 concunrrent 配置从 1 修改成 2,或者任何你想要的数字,但是应该是要少于计算机的核数。
  • 通过 :wq 保存文件,然后执行 gitlab-runner restart 命令重启服务
  • 就可以了

这个 .toml 的配置文件还有很多高级的配置,但是我都没有用到,可以参考这个文档

关于如何监控 gitlab-runner 的运行状态,还可以参考这篇 文章

如何让 pipeline 同时执行多个 stage

在比较简单的情况下,.gitlab-ci.yml 文件的所有 stages 都是串行的,但是有些可以并行。例如打包之后,需要同时 rsync 到多个测试机上,这个时候只需要把他们的 stage 修改成同一个就行了。例如:

stages:
  - install
  - build
  - sync

install:
  stage: install
  cache:
    paths:
      - node_modules/
  script:
    - echo "Installing packages"
    - cd /opt/fe/my-project
    - git add .
    - git stash
    - git checkout dev
    - git pull origin dev
    - npm install
  rules:
    - if: '$CI_COMMIT_BRANCH == "dev"'
      changes:
        - package-lock.json
        - package.json
  tags:
    - xxx

build:
  stage: build
  script:
    - echo "Building"
    - cd /opt/fe/my-project
    - git add .
    - git stash
    - git checkout dev
    - git pull origin dev
    - npm run deploy:prod
    - echo "Success!"
  rules:
    - if: '$CI_COMMIT_BRANCH == "dev"'
    - if: '$CI_DEPLOY_FREEZE == null'
  tags:
    - xxx

syncToServer1:
  stage: sync
  script:
    - echo "Sync to server 1"
    - cd /opt/fe/my-project
    - rsync --delete -rzh ./dist/ [email protected]:/path/to/project
    - echo "Success!"
  rules:
    - if: '$CI_COMMIT_BRANCH == "dev"'
  tags:
    - xxx

syncToServer2:
  stage: sync
  script:
    - echo "Sync to server 1"
    - cd /opt/fe/my-project
    - rsync --delete -rzh ./dist/ [email protected]:/path/to/project
    - echo "Success!"
  rules:
    - if: '$CI_COMMIT_BRANCH == "dev"'
  tags:
    - xxx

在上面的配置中,syncToServer1 和 syncToServer2 就会同时并发执行。

@oakland
Copy link
Owner Author

oakland commented Sep 6, 2023

@oakland
Copy link
Owner Author

oakland commented Oct 27, 2023

stage

不同的 stage 之间是串行的,同一个 stage 是并行的。
artifacts 是可以被下载的内容,并且可以设置过期时间。
needs 可以设置 job 的依赖关系,可以跳过 stage 的顺序。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant