One .gitlab-ci.yml - two pipelines

This is my current setup:

  • one production branch
  • main branch that is sometimes merged into production
  • .gitlab-ci.yml with multiple jobs

I want that, when main is merged to production, to run additional job that will change current project version to desired one, push that to production branch and build that project.

Changing of version is for example, changing from v1.0 to v1.3 in one file

build part works fine, I’ve also created this push part:

change-version:
  stage: version-change
  image:
    name: alpine:3.17.1
  script:
    - version=$(cat path/to/config.py | grep -i "APP_VERSION = " | sed 's/APP_VERSION = //' | sed 's/\"//g')
    - |
      echo "Current version is: $version"
    - |
      echo "New version is: $APPLICATION_VERSION"

    - if [ "$version" != "$APPLICATION_VERSION" ]; then
        if [[ $(git tag | grep "dev-$APPLICATION_VERSION") ]]; then
          echo "Git tag with this name already exists"
          false;
        fi

        apk update && apk upgrade;
        apk add sed;
        apk add git;
        apk add git-lfs;

        git config --global user.email "test@test.test";

        sed -i "s/APP_VERSION = \"$version\"/APP_VERSION = \"$APPLICATION_VERSION\"/" path/to/config.py ;
        
        git stash;
        git checkout $CI_COMMIT_BRANCH;
        git pull;
        git stash pop;
        git add path/to/config.py;
        git commit -m "New version for production deployment - $APPLICATION_VERSION";
        git push https://TOKEN:$GITLAB_TOKEN@repo.git $CI_COMMIT_BRANCH;
        
        git tag -a "dev-$APPLICATION_VERSION" -m "Version -> dev-$APPLICATION_VERSION";
        git push $https://TOKEN:$GITLAB_TOKEN@repo.git "dev-$APPLICATION_VERSION";
      else
        echo "Current version and new version are the same, skipping tagging";
      fi
  rules:
    - if: $CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH == "production"

APPLICATION_VERSION is set into global CI/CD variables.

And I want to, when main is merged to production to execute this change-version job only, then after that, same pipeline with new version.

I can achieve that like this - it triggers another pipeline that reruns whole pipeline again and all jobs. Basically, everything is ran twice - consumes lot of unnecessary time and resources.

Adding pictures for clarification and easier understanding of problem.

This is current situation:
Current
Second pipeline is triggered by git push command of the change-version job.

This is desired situation:
Desired
Second pipeline is triggered by git push command of the change-version job.

Thanks!

With help of this article: Skipping Tests in GitLab CI

and carefully managing rules I managed to solve this problem.