Issue with SSH Keys & Protected branche

Dear community,
Please find bellow my problem.
Many thanks in advance for your help :slight_smile:

Problem to solve

  • I got the following error while pushing code via CI pipeline.
    remote: GitLab: You are not allowed to push code to this project. To gitlab.xxx.be:xx/xxx/xxx.git ! [remote rejected] main -> main (pre-receive hook declined)
  • You’ll see behind, that I was able to fix the issue. But this is not the best practice. I would like to keep my branch protected & avoid using --force option.

Steps to reproduce

I was able to fix the issue by

  • Removing branch protection.
  • Use git push –force

Configuration

There is a pipeline who push code from the current repo to another repo.
The public key is configured as follow (on the destionation repo):

push-updated-repository:
  stage: push
  needs: ["update-repository"]
  variables:
    COMMIT_MSG: "Gitlab pipeline: Pushed with CI_JOB_ID $CI_JOB_ID"
  script:
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - rm -rf sencha-packages-registry
    - git clone gitlab.xxx.be:xx/xxx/xxx.git
    - cp -r repo/* xxx/xxx
    - cd xxx
    - git config --global user.email "xxx"
    - git config --global user.name "self-gitlab"
    - git add .
    - git commit -m "$COMMIT_MSG"
    - git push -u origin main -vvv --force
  only:
    - master
  tags:
    - jdk-8

Versions

  • Self-managed
  • Self-hosted Runners

Versions

  • GitLab 16.6
  • GitLab Runner - 16.66