Paul
Paul

Reputation: 157

how to execute git commands in gitlab-ci scripts

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 ?

Upvotes: 11

Views: 25879

Answers (2)

Qutory
Qutory

Reputation: 109

Gitlab CI/CD will clone the repository inside the running job automatically. What you need is the git command installed. You could use bitnami/git image to run the job in a container having the command installed.

This worked for me (trying to verify if a tag is available):

tag-available:
stage: .pre
image: bitnami/git:2.37.1
script:
    # list all tags
    - git tag -l
    # check existence of tag "v1.0.0"
    - >
        if [ $(git tag -l "v1.0.0") ]; then
            echo "yes"
        else
            echo "no."

If you need to authorize the job agains some gitlab registry (or api) please note that there are some predefined variables for user and password (tokens). For this kind of actions you might be most interested in these variables:

$CI_REGISTRY_PASSWORD
$CI_REGISTRY_USER
$CI_REGISTRY
$CI_REPOSITORY_URL
$CI_DEPLOY_PASSWORD
$CI_DEPLOY_USER 

The CI_DEPLOY_USER must have a deploy user created and named "gitlab-deploy-token" to have his password loaded in the CI/CD. Read here more.

Upvotes: 4

Stefan van Gastel
Stefan van Gastel

Reputation: 4478

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

Upvotes: 22

Related Questions