3

I have a pipeline to bump package.json version, build angular, and release it.

In the bump job I successfully increased my package version and push it into the master in the next job which is build, the pipeline didn't pull the newest version, and doing build on the last one that I started the pipeline.

The question is, How can I pull the last commit that pushed by pipeline in the build job?

I have this pipeline:

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
sytech
  • 29,298
  • 3
  • 45
  • 86
Adnan
  • 814
  • 12
  • 38

1 Answers1

2

To answer your question: you can't change the git ref (commit SHA) that is checked out by GitLab in the middle of a pipeline. All jobs in a pipeline will always use the same ref.

To offer an alternative approach: what you probably want to do is orchestrate your jobs to run in concert across pipelines. For example, you might do something like condition jobs to only run when CI_COMMIT_AUTHOR is the user you use to bump the version.

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
  #...

Or you may choose any other way to differentiate which jobs should run. Tag name patterns might also work like:

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+/

Alternatively, if you really just want to use the same pipeline, you can just use git and check it out in the job, but be aware that simultaneous updates to the default branch may result in this doing something unexpected. You could use artifacts to make sure the correct ref is checked out.

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" .
sytech
  • 29,298
  • 3
  • 45
  • 86
  • 1
    Hey, @sytech Thanks! I get an idea! The thing is, I need to separate pipeline for bump and it can be achieved by creating a new branch let's call it release, and whenever I pushed the master to that branch the bump job will be triggered and do all the work and finally, push and create a new tag and then another pipeline will be started because of the tag and it will build with the new tag version. – Adnan Apr 21 '22 at 10:26
  • 1
    Yeah, that sounds like it will work for your use case. – sytech Apr 21 '22 at 10:29