devops - 在 Gitlab-ci 中,有一种方法仅在规则匹配后才在脚本之前运行

标签 devops gitlab-ci pipeline

在 gitlab-ci 中我有

before_script:
  - apt update && apt upgrade -y
  - apt install -y 

在我的舞台工作中我添加了一条规则

merge_request:
  stage: test
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master"
    - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"

发生的情况是,当之前的脚本运行时,该作业在他的阶段被触发,然后它到达规则并发现没有什么可做的。 这会减慢管道速度。 有没有办法在“before_script”之前添加规则

这是管道 pipeline 谢谢

最佳答案

找到答案here

.before_script_template:
  before_script:
    - apt-get update
    - apt-get -y install apt-transport-https ca-certificates curl gnupg2 software-properties-common
    - curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
    - add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
    - apt-get update
    - apt-get -y install docker-ce
    - curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    - chmod +x /usr/local/bin/docker-compose
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY

test_integration:
  extends: .before_script_template
  stage: test 
  script:
    - docker-compose -f CI/backend-service/docker-compose.yml up -d
    - npm install
    - npm run-script integration-test

build:
  extends: .before_script_template
  stage: build
  script:
    - npm install
    - export VERSION=`git describe --tags --always`
    - docker build -t $CI_REGISTRY_IMAGE:$VERSION .
    - docker push $CI_REGISTRY_IMAGE

etc

关于devops - 在 Gitlab-ci 中,有一种方法仅在规则匹配后才在脚本之前运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72457620/

相关文章:

angular - 在 Angular 5 环境中使用 process.env

Gitlab CI - 找到使用的管道变量

powershell - PowerShell 是否具有 "window"函数?

delphi - 如何使用 DELPHI 将文件写入 STDIN Stream?

python - 如何在 BigQuery Web UI 中创建隐藏数据集,同时保持数据工作室连接可用?

docker - Docker Jenkins容器因严重的jenkins而崩溃。InitReactorRunner $ 1#onTaskFailed:失败的ConfigurationAsCode.init

docker-compose - 通过覆盖删除 docker-compose 上的服务

amazon-web-services - 如何触发step函数并获取失败或成功状态

angularjs - 使用GitLab CI通过ftp部署应用

gitlab - Gitlab-CI 又名管道依赖项中的上游触发