Using a gitlab runner to change a file and push this change to the main repository

32 Views Asked by At

I am new to using Gitlab runners and am trying to write a pipeline that changes a file and then commits and pushes this file to main. I have run into a multitude of different errors while trying to do this and trying other solutions. If anyone has suggestions of solutions that have worked for them, that would be greatly appreciated.

This is what I have in my gitlab-ci.yml file:

    `stages:
  - build

write_logfile:
  stage: build
  script:
    - echo "This is a log message" > logfile.txt
    - git config user.email "[email protected]"
    - git config user.name "access-token-name"
    - git remote add gitlab_origin
    - https://oauth2:[email protected]/path-to-project.git
    - git add .
    - git commit -m "push back from pipeline"
    - git push gitlab_origin HEAD:main -o ci.skip


  rules:
     - changes: 
       - 'Source/*'`

I found this from this page but I keep receiving the following error: remote: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2FA enabled and you must use a personal access token instead of a password.

My account does not have 2FA so I am not sure what I'm doing wrong.

1

There are 1 best solutions below

0
Jamie Anderson On

I changed a few things to solve this problem:

  1. Setting the GIT_STRATEGY variable to Clone

  2. Checking out a branch to avoid detached head, and correctly using the project access token (see below)

    git checkout $CI_COMMIT_REF_NAME
    git config user.name "CI Pipeline"
    git config user.email "[email protected]"
    echo "This is a log message" >> logfile.txt
    git add .
    
    git commit -m "updating logfile [skip ci]"
    
    # Here we set the URL of the remote we want to push to
    
    git remote set-url --push origin "https://token-name:[email protected]/${CI_PROJECT_PATH}.git"
    git push --set-upstream origin $CI_COMMIT_REF_NAME