gpt4 book ai didi

Gitlab CI - 在 before_script 中指定阶段

转载 作者:行者123 更新时间:2023-12-04 22:54:51 24 4
gpt4 key购买 nike

我想运行我的 test_integration 和构建阶段所需的脚本。有没有办法在 before 脚本中指定它,这样我就不必写两次了。

before_script:
stage: ['test_integration', 'build']

这似乎不起作用我在 gitlab ci linter 中收到以下错误。

Status: syntax is incorrect

Error: before_script config should be an array of strings



.gitlab-ci.yml
stages:
- security
- quality
- test
- build
- deploy

image: node:10.15.0

before_script:
stage: ['test_integration', 'build']
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


services:
- mongo
- docker:dind

security:
stage: security
script:
- npm audit

quality:
stage: quality
script:
- npm install
- npm run-script lint

test_unit:
stage: test
script:
- npm install
- npm run-script unit-test

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

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

deploy:
stage: deploy
script: echo 'deploy'

最佳答案

before_script语法不支持 stages部分。您可以使用 before_script正如您在没有 stages 的情况下所做的那样部分,但是before_script stage 将为管道中的每个作业运行。

相反,你可以做的是使用 GitLab 的 anchor's功能,允许您在 .gitlab-ci 上复制内容文件。

所以在你的场景中,它看起来像:

stages:
- security
- quality
- test
- build
- deploy

image: node:10.15.0

.before_script_template: &build_test-integration
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


services:
- mongo
- docker:dind

security:
stage: security
script:
- npm audit

quality:
stage: quality
script:
- npm install
- npm run-script lint

test_unit:
stage: test
script:
- npm install
- npm run-script unit-test

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

build:
stage: build
<<: *build_test-integration
script:
- npm install
- export VERSION=`git describe --tags --always`
- docker build -t $CI_REGISTRY_IMAGE:$VERSION .
- docker push $CI_REGISTRY_IMAGE

deploy:
stage: deploy
script: echo 'deploy'

编辑:还有另一种方法,除了使用 anchor ,您还可以使用 extends句法:
.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

关于Gitlab CI - 在 before_script 中指定阶段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54074433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com