无法使用 gitlab 管道构建在构建期间由管道推送的最新提交

Unable to build latest commit that pushed by pipeline during build using gitlab pipeline

我有一个管道来提升 package.json 版本,构建 angular,然后发布它。

bump 作业中,我成功地增加了我的包版本并将其推送到 master 在下一个作业 build 中,管道没有拉取最新版本,而是在我启动管道的最后一个作业上进行构建。

问题是,如何在 build 作业中提取由管道推送的最后一次提交?

我有这个管道:

stages:
  - bump
  - build
  - release

bump:
 stage: bump
 image:
    entrypoint: ['']
    name: registry.gitlab.com/orgs/registries/node:16
 only:
    - tags
 before_script:
    # Clone the repository via HTTPS inside a new directory
    - git clone "https://$GITLAB_USERNAME:$GITLAB_TOKEN@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" "${CI_COMMIT_SHA}"

    # Set the displayed user with the commits that are about to be made
    - git config --global user.email "${GIT_USER_EMAIL:-$GITLAB_USER_EMAIL}"
    - git config --global user.name "${GIT_USER_NAME:-$GITLAB_USER_NAME}"
 script:
    - cd "${CI_COMMIT_SHA}/path/to/angular/"
    - npm version from-git
    - git push origin "${CI_DEFAULT_BRANCH}" -o ci.skip



build:
  stage: build
  image: registry.gitlab.com/orgs/registries/php:7.4.29
  only:
    - tags
  before_script:
    - echo $CI_JOB_ID
    # Writing GE_JOB_ID variable to environment file, will need the value in the next stage.
    - echo GE_JOB_ID=$CI_JOB_ID >> build.env
    - npm install -g @angular/cli
    - npm install -g nx
    - composer install
  script:
    - cd public/src/frontend/
    - npm install --force
    - echo "Building Angular app and Making Plugin"
    - npm run build:all
    - echo "Build finished"
    - echo "Angular Files"
    - ls -lah
    - cd ../../../
    - echo "Plugin Files"
    - ls -lah
  artifacts:
    paths:
      - release/file.zip
    reports:
      dotenv: build.env

  tags:
    - docker
    - gce

release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  script:
    - echo 'running release_job'
    - echo 'Previous Job ID is printed below'
    - echo $GE_JOB_ID
  needs:
    - job: build
      artifacts: true
  release:
    name: 'Release File $CI_COMMIT_TAG'
    description: 'Created using the release-cli'
    # tag_name is a mendatory field and can not be an empty string
    tag_name: '$CI_COMMIT_TAG'
    ref: '$CI_COMMIT_TAG'
    assets:
      links:
        - name: 'Artifact'
          url: 'https://gitlab.com/orgs/somerepo/-/jobs/${GE_JOB_ID}/artifacts/raw/release/file.zip'
  only:
    - tags

回答您的问题:您无法更改 GitLab 在管道中间检出的 git ref(提交 SHA)。管道中的所有作业将始终使用相同的 ref.

提供另一种方法:您可能想要做的是将您的工作编排到 运行 跨管道协调一致。例如,当 CI_COMMIT_AUTHOR 是您用来提升版本的用户时,您可能只对 运行 执行条件作业之类的操作。

variables:
  BUMP_EMAIL: "special@example.com"

bump:
  rules:
    - if: $CI_COMMIT_TAG  # run this job on tags
  # ...
  script:
    # ...
    git config --global user.email "$BUMP_EMAIL"
    - git push -u origin "$CI_DEFAULT_BRANCH"  # trigger branch pipeline

# These jobs will only run when the pipeline is triggered by the push from the `bump` job
build:
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_AUTHOR == $BUMP_EMAIL
  # ...

release:
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_AUTHOR == $BUMP_EMAIL
  #...

或者您可以选择任何其他方式来区分哪些作业应该 运行。标签名称模式也可能像这样工作:

bump:
  rules:
    - if: $CI_COMMIT_TAG && $CI_COMMIT_TAG !~ /\d+\.\d+\.\d+/
  script:
    # ... do all the commits and push with ci.skip
    - git tag -a "$NEW_VERSION" -m "bump version"  # in the form of x.y.z or whatever matches the pattern
    - git push --tags  # push tags to trigger tag pipeline for build and release

build:
  rules:
    - if: $CI_COMMIT_TAG =~ /\d+\.\d+\.\d+/
  # ...

release:
  rules:
    - if: $CI_COMMIT_TAG =~ /\d+\.\d+\.\d+/

或者,如果您真的只想使用相同的管道,您可以只使用git并在作业中检查它,但要注意同时对默认分支的更新可能会导致这做一些意想不到的事情。您可以使用工件来确保签出正确的引用。

build:
  variables:
    GIT_STRATEGY: none  # prevent normal checkout
  before_script:
    - git clone "https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH_SLUG}.git" .