Is there any way to check that your current branch is up to date with master using rules?

,

Hi!
I have a question.
I need ci that checks that current branch is up to date with master in MR.
What I created:

stages:
  - merge-master

.ci_rules:
  mr_to_master:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
  not_on_master:
    - if: '$CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH'
      when: never

# merge master to the current branch to be up to date
merge_master_to_the_current_branch:
  stage: merge-master
  tags:
    - awesometag
  before_script:
    - git config --global http.sslVerify false
    - git config --global push.default simple
    - git config --global user.email "${GIT_USER_EMAIL:-$BOT_NAME}"
    - git config --global user.name "merge $CI_DEFAULT_BRANCH to $CI_COMMIT_REF_NAME by ${GIT_USER_NAME:-$BOT_NAME}"
    - git remote set-url origin "https://$BOT_NAME:${PRE_COMMIT_ACCESS_TOKEN:-$REPO_ACCESS_TOKEN}@${CI_SERVER_HOST}/${CI_MERGE_REQUEST_SOURCE_PROJECT_PATH}.git"
    - git fetch --quiet
  script:
    - |+
      git checkout $CI_DEFAULT_BRANCH
      git pull
      git checkout $CI_COMMIT_REF_NAME
      status=$(git merge $CI_DEFAULT_BRANCH -m "sync_with_master_by_$BOT_NAME")
      if [ "$status" == "Already up to date." ]; then
        echo "Nothing to do."
        exit 0
      else
        echo "Found discrepancies in the configuration. Merging $CI_DEFAULT_BRANCH to $CI_COMMIT_REF_NAME ... "
        git push
      fi
  rules:
    - !reference [.ci_rules, not_on_master]
    - !reference [.ci_rules, mr_to_master]

But when bot push merged changes to current branch, it trigger CI.
I disabled it by this way:
status=$(git merge $CI_DEFAULT_BRANCH -m "sync_with_master_by_$BOT_NAME [ci skip]") and git push -o ci.skip
All works fine, but this behaviour confuses developers:


They see that pipeline skipped and panic:)

Is there any way to check that your current branch is up to date with master using rules?
For example: rule checks that I have changes in master that missed in my branch, rule triggers stage merge-master, bot makes commit, main CI triggered by bot (merge-master skips).

Tank you in advance!

Hi @EsDmitrii

rules does not work with GIT, so short answer is no.

However, you can use git diff to check if there are any differences before trying to merge and push.

For further alternative solutions I would need to know your use-case for keeping the branches in sync with default branch.

Hi!
I think I’ll create any additional job that will check git diff and set specific var which I’ll use in rules to trigger other pipeline steps.
May be I’m wrong but I can create rule that will check username and skip ci when bot makes commit:

You cannot use dotenv variables created in job scripts in rules, because rules are evaluated before any jobs run.

The rules based on GitLab user details might work.

thank you for your expertise
appreciate your assistance