Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute git commands in gitlab-ci scripts

Tags:

gitlab-ci

I want to change a file and commit changes inside a gitlab-ci pipeline

I tried writing normal git commands in script

script:
    - git clone [email protected]
    - cd project file
    - touch test.txt
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - git add .
    - git commit -m "testing autocommit"
    - git push

I get cannot find command git or something along those lines, I know it has something to do with tags, but if I try add a git tag it says no active runner. anyone has an idea how to run git commands on gitlab-ci ?

like image 568
Paul Avatar asked Feb 19 '26 04:02

Paul


1 Answers

First you need to make sure you can actually use git, so either run your jobs on a shell executor located on a system that has git or use a docker executor and use an image that has git installed.

Next problem you will encounter is that you can't push to Git(lab) since you can't enter credentials.

So the solution is to create a ssh keypair and load the ssh private key into your CI environment through CI/CD variables, also add the corresponding public key to you your Git(lab) account.

Source: https://about.gitlab.com/2017/11/02/automating-boring-git-operations-gitlab-ci/

Your .gitlab-ci.yml will then look like this:

job-name:
  stage: touch
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$GIT_SSH_PRIV_KEY")
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - mkdir -p ~/.ssh
    - cat gitlab-known-hosts >> ~/.ssh/known_hosts
  script:
    - git clone [email protected]
    - cd project file
    - touch test.txt
    - git add .
    - git commit -m "testing autocommit"
    - git push
like image 74
Stefan van Gastel Avatar answered Feb 22 '26 04:02

Stefan van Gastel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!