gitlab ci/cd管道运行/按顺序扩展其他作业

发布于 2025-01-30 05:29:51 字数 1362 浏览 2 评论 0原文

因此,我有一个由特定规则触发的作业 - 创建一个新的标签app-prod-1.0.0app-dev-1.0.0.0。每当创建新标签时,我都会调用该作业,以回报扩展了其他作业,

image: node:lts-alpine
stages:
  - install
  - build
  - deploy

.install-packages:
  stage: install
  script:
    - echo "INSTALL-PACKAGES"
    - yarn install --cache-folder .yarn-cache
  artifacts:
    paths:
      - node_modules
  cache:
    - key:
        files:
          - yarn.lock
      paths:
        - .yarn-cache/

.build-project:
  stage: build
  script:
    - echo "BUILD-PROJECT"
    - echo $ENVIRONMENT
    - yarn build
  artifacts:
    paths:
      - build

.deploy-project:
  stage: deploy
  script:
    - echo "DEPLOY-PROJECT"
    - ls -la build

build_prod:
  variables:
    PACKAGE: '/app/prod'
    ENVIRONMENT: 'prod'
  extends:
    - .install-packages
    - .build-project
    - .deploy-project
  rules:
    - if: '$CI_COMMIT_TAG =~ /^app-prod-[0-9]+\.[0-9]+\.[0-9]+$/'

build_dev:
  variables:
    PACKAGE: '/app/dev'
    ENVIRONMENT: 'dev'
  extends:
    - .install-packages
    - .build-project
    - .deploy-project
  rules:
    - if: '$CI_COMMIT_TAG =~ /^app-dev-[0-9]+\.[0-9]+\.[0-9]+$/'

我的想法是将作业以我在作业中描述的顺序调用:.install-packages。构建项目.deploy-project。但这没有发生,似乎它只是跳到最后一个作业.deploy-project,而无需安装和构建,从而破坏了我的管道。 如何顺序运行/扩展作业?

So I have a job which is triggered with specific rules - creating a new tag app-prod-1.0.0 or app-dev-1.0.0. Whenever new tag is created I call the job, which in return extends other jobs

image: node:lts-alpine
stages:
  - install
  - build
  - deploy

.install-packages:
  stage: install
  script:
    - echo "INSTALL-PACKAGES"
    - yarn install --cache-folder .yarn-cache
  artifacts:
    paths:
      - node_modules
  cache:
    - key:
        files:
          - yarn.lock
      paths:
        - .yarn-cache/

.build-project:
  stage: build
  script:
    - echo "BUILD-PROJECT"
    - echo $ENVIRONMENT
    - yarn build
  artifacts:
    paths:
      - build

.deploy-project:
  stage: deploy
  script:
    - echo "DEPLOY-PROJECT"
    - ls -la build

build_prod:
  variables:
    PACKAGE: '/app/prod'
    ENVIRONMENT: 'prod'
  extends:
    - .install-packages
    - .build-project
    - .deploy-project
  rules:
    - if: '$CI_COMMIT_TAG =~ /^app-prod-[0-9]+\.[0-9]+\.[0-9]+$/'

build_dev:
  variables:
    PACKAGE: '/app/dev'
    ENVIRONMENT: 'dev'
  extends:
    - .install-packages
    - .build-project
    - .deploy-project
  rules:
    - if: '$CI_COMMIT_TAG =~ /^app-dev-[0-9]+\.[0-9]+\.[0-9]+$/'

My thought was that jobs will be called in the order I have described inside the job: .install-packages, .build-project, .deploy-project. But that's not happening it seems that it just jumps to the last job .deploy-project, without installing and building and thus breaking my pipeline.
How to run/extend jobs in sequence?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜未央樱花落 2025-02-06 05:29:51

这是我在GitLab的工作中没有使用多个扩展的行为。

Gitlab,试图从父级合并代码。

现在,您的所有父作业都定义了脚本标签,并且在您的作业中,例如build_prod,在以下的顺序中进行的扩展

extends:
    - .install-packages
    - .build-project
    - .deploy-project

是从.Deploy-Project中的脚本代码覆盖另一个作业的脚本标签。

它在变量方面有所不同。如果使用相同的变量,它将合并所有变量并覆盖。

请参阅您自己的示例更新带有变量的示例。

image: node:lts-alpine
stages:
  - install
  - build
  - deploy

.install-packages:
  stage: install
  variables:
    PACKAGE: 'install'
    INSTALL: 'install'
  script:
    - echo "INSTALL-PACKAGES"
    - yarn install --cache-folder .yarn-cache
  artifacts:
    paths:
      - node_modules
  cache:
    - key:
        files:
          - yarn.lock
      paths:
        - .yarn-cache/

.build-project:
  stage: build
  variables:
    PACKAGE: 'build'
    BUILD: 'build'
  script:
    - echo "BUILD-PROJECT"
    - echo $ENVIRONMENT
    - yarn build
  artifacts:
    paths:
      - build

.deploy-project:
  stage: deploy
  variables:
    PACKAGE: 'deploy'
    DEPLOY: 'from deploy'
  script:
    - echo "DEPLOY-PROJECT"
    - ls -la build

build_prod:
  variables:
    PACKAGE: '/app/prod'
    ENVIRONMENT: 'prod'
  extends:
    - .install-packages
    - .build-project
    - .deploy-project
  rules:
    - if: '$CI_COMMIT_TAG =~ /^app-prod-[0-9]+\.[0-9]+\.[0-9]+$/'

build_dev:
  variables:
    PACKAGE: '/app/dev'
    ENVIRONMENT: 'dev'
  extends:
    - .install-packages
    - .build-project
    - .deploy-project
  rules:
    - if: '$CI_COMMIT_TAG =~ /^app-dev-[0-9]+\.[0-9]+\.[0-9]+$/'

现在,请注意如何用“/app/prod”的最终值覆盖包装变量,该值来自构建生产作业本身。同时,将来自单个家长作业的其他变量合并到下面的情况下,

variables:
    PACKAGE: "/app/prod"
    INSTALL: install
    BUILD: build
    DEPLOY: from deploy
    ENVIRONMENT: prod

我真的发现 view合并yaml 功能最好了解如何评估我的YML文件。

它可在CI/CD-&GT中使用。编辑

This is the behaviour for which I didn't use multiple extends so far in my work with GitLab.

GitLab, attempts to merge the code from parent job.

Now all your parent jobs defines the script tag and in your job for e.g. build_prod the extends happening in below order

extends:
    - .install-packages
    - .build-project
    - .deploy-project

the script code from .deploy-project is overwriting the other job's script tag.

It works differently for the variables. It will merge all the variables and overwrites if same variable is used.

See your own example updated with variables.

image: node:lts-alpine
stages:
  - install
  - build
  - deploy

.install-packages:
  stage: install
  variables:
    PACKAGE: 'install'
    INSTALL: 'install'
  script:
    - echo "INSTALL-PACKAGES"
    - yarn install --cache-folder .yarn-cache
  artifacts:
    paths:
      - node_modules
  cache:
    - key:
        files:
          - yarn.lock
      paths:
        - .yarn-cache/

.build-project:
  stage: build
  variables:
    PACKAGE: 'build'
    BUILD: 'build'
  script:
    - echo "BUILD-PROJECT"
    - echo $ENVIRONMENT
    - yarn build
  artifacts:
    paths:
      - build

.deploy-project:
  stage: deploy
  variables:
    PACKAGE: 'deploy'
    DEPLOY: 'from deploy'
  script:
    - echo "DEPLOY-PROJECT"
    - ls -la build

build_prod:
  variables:
    PACKAGE: '/app/prod'
    ENVIRONMENT: 'prod'
  extends:
    - .install-packages
    - .build-project
    - .deploy-project
  rules:
    - if: '$CI_COMMIT_TAG =~ /^app-prod-[0-9]+\.[0-9]+\.[0-9]+$/'

build_dev:
  variables:
    PACKAGE: '/app/dev'
    ENVIRONMENT: 'dev'
  extends:
    - .install-packages
    - .build-project
    - .deploy-project
  rules:
    - if: '$CI_COMMIT_TAG =~ /^app-dev-[0-9]+\.[0-9]+\.[0-9]+$/'

And now notice how PACKAGE variable is overwritten with the final value of '/app/prod' which comes from build-prod job itself. At the same time other variables from individual parent jobs are merged to look like below

variables:
    PACKAGE: "/app/prod"
    INSTALL: install
    BUILD: build
    DEPLOY: from deploy
    ENVIRONMENT: prod

I really found View merged YAML feature best to understand how my yml file will be evaluated.

Its available in CI/CD -> Editor

倥絔 2025-02-06 05:29:51

它实际上不是“跳到最后的作业”,而是仅执行您提供的单个作业 - 即build_prodbuild> build_dev,具体取决于提交标签。

docs ,您基本上只是合并了您指定的所有模板作业中的所有内容,因此最后一个stage关键字来自.deploy-project模板作业胜利。

您应该在每个阶段分别指定每个作业,甚至可能将您的规则放入单独的模板作业,即

.dev:
  variables:
    PACKAGE: '/app/dev'
    ENVIRONMENT: 'dev'
  rules:
    - if: '$CI_COMMIT_TAG =~ /^app-dev-[0-9]+\.[0-9]+\.[0-9]+$/'
install-dev:
  extends:
    - .dev
    - .install-packages
build-dev:
  extends:
    - .dev
    - .build-project
deploy-dev:
  extends:
    - .dev
    - .deploy-project

您应该为prod> prod env创建类似的作业,定义模板作业。产品,并创建安装程序,构建 - 构建,部署生产作业

It's not actually "jumps to the last job", but simply executes a single job you have provided - that is build_prod or build_dev, depending on commit tag.

As per docs when you are calling extends, you are basically just merging everything inside all the template jobs that you specified, so the last stage keyword, which comes from .deploy-project template job wins.

You should specify each job separately for each stage, and maybe even put your rules to a separate template job, i.e.

.dev:
  variables:
    PACKAGE: '/app/dev'
    ENVIRONMENT: 'dev'
  rules:
    - if: '$CI_COMMIT_TAG =~ /^app-dev-[0-9]+\.[0-9]+\.[0-9]+$/'
install-dev:
  extends:
    - .dev
    - .install-packages
build-dev:
  extends:
    - .dev
    - .build-project
deploy-dev:
  extends:
    - .dev
    - .deploy-project

You should create similar jobs for prod env, define template job .prod, and create install-prod, build-prod, deploy-prod jobs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文