'Gitlab-CI with Docker executor /usr/bin/bash: line 90: git: command not found

I have a local GitLab server and gitlab-ci runner with Docker executor.

I want to use gitlab-ci to build (for the first stage) my Maven project. Since I use buildnumber-maven-plugin I added a Git service to my gitlab-ci.yml like this:

image: maven:latest

services:
  - alpine/git:latest

# Cache goes here
cache:
  paths:
    - .m2/repository
    - frontend-app/node_modules/

# ENV variables goes  here

# Before script goes here

# Stages definition goes here
stages:
  - build

# Build stage
build:
  stage: build
  before_script:
    - mvn --version
    - git --version
  script:
    - mvn clean install --settings .ci/settings.xml
  artifacts:
    paths:
      - backend-app/target/*.jar

But when git --version is executed I got command not found error. This is my complete GitLab CI console log:

Running with gitlab-runner 12.9.0 (4c96e5ad)
   on securteck jrEY4-V2
Preparing the "docker" executor
 Using Docker executor with image maven:latest ...
 Starting service alpine/git:latest ...
 Pulling docker image alpine/git:latest ...
 Using docker image sha256:fe3295a34a932155290fdbba14d770c11f3dfe0e1e732d8e355cecbeec59e877 for alpine/git:latest ...
 Waiting for services to be up and running...
 *** WARNING: Service runner-jrEY4-V2-project-1-concurrent-0-alpine__git-0 probably didn't start properly.
 Health check error:
 ContainerStart: Error response from daemon: Cannot link to a non running container: /runner-jrEY4-V2-project-1-concurrent-0-alpine__git-0 AS /runner-jrEY4-V2-project-1-concurrent-0-alpine__git-0-wait-for-service/service (docker.go:1322:0s)
 Service container logs:
 2020-04-10T19:05:54.826814609Z usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
 2020-04-10T19:05:54.826879457Z            [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
 2020-04-10T19:05:54.826895945Z            [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
 2020-04-10T19:05:54.826910244Z            [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
 2020-04-10T19:05:54.826923359Z            <command> [<args>]
 2020-04-10T19:05:54.826935482Z 
 2020-04-10T19:05:54.826947063Z These are common Git commands used in various situations:
 2020-04-10T19:05:54.826959005Z 
 2020-04-10T19:05:54.826971038Z start a working area (see also: git help tutorial)
 2020-04-10T19:05:54.826982664Z    clone     Clone a repository into a new directory
 2020-04-10T19:05:54.826994349Z    init      Create an empty Git repository or reinitialize an existing one
 2020-04-10T19:05:54.827006426Z 
 2020-04-10T19:05:54.827017832Z work on the current change (see also: git help everyday)
 2020-04-10T19:05:54.827029626Z    add       Add file contents to the index
 2020-04-10T19:05:54.827040891Z    mv        Move or rename a file, a directory, or a symlink
 2020-04-10T19:05:54.827052743Z    restore   Restore working tree files
 2020-04-10T19:05:54.827064196Z    rm        Remove files from the working tree and from the index
 2020-04-10T19:05:54.827075878Z 
 2020-04-10T19:05:54.827087305Z examine the history and state (see also: git help revisions)
 2020-04-10T19:05:54.827099305Z    bisect    Use binary search to find the commit that introduced a bug
 2020-04-10T19:05:54.827111245Z    diff      Show changes between commits, commit and working tree, etc
 2020-04-10T19:05:54.827125099Z    grep      Print lines matching a pattern
 2020-04-10T19:05:54.827137183Z    log       Show commit logs
 2020-04-10T19:05:54.827149113Z    show      Show various types of objects
 2020-04-10T19:05:54.827160630Z    status    Show the working tree status
 2020-04-10T19:05:54.827172589Z 
 2020-04-10T19:05:54.827183674Z grow, mark and tweak your common history
 2020-04-10T19:05:54.827195529Z    branch    List, create, or delete branches
 2020-04-10T19:05:54.827207168Z    commit    Record changes to the repository
 2020-04-10T19:05:54.827218764Z    merge     Join two or more development histories together
 2020-04-10T19:05:54.827230406Z    rebase    Reapply commits on top of another base tip
 2020-04-10T19:05:54.827241931Z    reset     Reset current HEAD to the specified state
 2020-04-10T19:05:54.827253929Z    switch    Switch branches
 2020-04-10T19:05:54.827265678Z    tag       Create, list, delete or verify a tag object signed with GPG
 2020-04-10T19:05:54.827277537Z 
 2020-04-10T19:05:54.827300897Z collaborate (see also: git help workflows)
 2020-04-10T19:05:54.827314706Z    fetch     Download objects and refs from another repository
 2020-04-10T19:05:54.827327136Z    pull      Fetch from and integrate with another repository or a local branch
 2020-04-10T19:05:54.827339235Z    push      Update remote refs along with associated objects
 2020-04-10T19:05:54.827351266Z 
 2020-04-10T19:05:54.827362364Z 'git help -a' and 'git help -g' list available subcommands and some
 2020-04-10T19:05:54.827374869Z concept guides. See 'git help <command>' or 'git help <concept>'
 2020-04-10T19:05:54.827387595Z to read about a specific subcommand or concept.
 2020-04-10T19:05:54.827401411Z See 'git help git' for an overview of the system.
 *********
 Pulling docker image maven:latest ...
 Using docker image sha256:32cd4e857a56c5cfddaafaebc877764e2547e7bc23b4bc63cfc6a163c4ee3361 for maven:latest ...
Preparing environment
00:02
 Running on runner-jrEY4-V2-project-1-concurrent-0 via PAR-174582...
Getting source from Git repository
00:03
 Fetching changes with git depth set to 50...
 Reinitialized existing Git repository in /builds/securteck/securteck/.git/
 From http://195.154.233.19:8000/securteck/securteck
  * [new ref]         refs/pipelines/29 -> refs/pipelines/29
    b8a890e..17b4d1c  develop           -> origin/develop
 Checking out 17b4d1cd as develop...
 Skipping Git submodules setup
Restoring cache
00:02
 Checking cache for default-4...
 No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted. 
 Successfully extracted cache
Downloading artifacts
00:02
Running before_script and script
00:02
 $ mvn --version
 Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
 Maven home: /usr/share/maven
 Java version: 14, vendor: Oracle Corporation, runtime: /usr/java/openjdk-14
 Default locale: en_US, platform encoding: UTF-8
 OS name: "linux", version: "4.15.0-76-generic", arch: "amd64", family: "unix"
 $ git --version
 /usr/bin/bash: line 90: git: command not found
Running after_script
00:02
Uploading artifacts for failed job
00:02
 ERROR: Job failed: exit code 1


Solution 1:[1]

If you'd like, you can install git in your maven container. Example (stub) .gitlab-ci.yml:

image: maven:latest

myjob:
  script:
  - yum install -y git
  - git --version

From the CI job log:

$ git --version
 git version 1.8.3.1

Solution 2:[2]

A service is another docker container running during your job (a database for example). The docker image alpine/git:latest is not a service, it is used to execute a git command. You can see in your logs that git is called without a command.

Instead of using a service, you should use a docker image with git installed in it to run your job. This way, your script will be able to call git.

Solution 3:[3]

The error is occurring, because the git is not available on the runner which picked up your build for execution.

As discussed in previous posts, using services is not efficient.

services:
  - alpine/git:latest 

And also, installing git every-time on your image is time consuming and resource heavy.

myjob:
  script:
  - yum install -y git
  - git --version

So, the most efficient approach is to use the image with git installed upfront and perform your git task.

Below is my example, where I am using an image of alpine/git from dockerhub https://hub.docker.com/r/alpine/git and executing my job on a container launched from this image.

create-git-tag:
  image: alpine/git
  stage: tag
  script:
    - git --version
   

Solution 4:[4]

I had a similar issue when working to deploy a Go app to AWS using GitLab CI.

Here's the error code I got:

$ echo "Check the go version"
Check the go version
$ go version
/bin/bash: line 128: go: command not found
Cleaning up project directory and file based variables 00:00
ERROR: Job failed: exit code 1

Here's how I fixed it:

The issue was that I did not specify an image (image: golang:latest) for the job (compile job):

stages:
  - test
  - build
  - deploy

format:
  stage: test
  image: golang:latest
  script:
    - go fmt $(go list ./... | grep -v /vendor/)
    - go vet $(go list ./... | grep -v /vendor/)
    - go test -race $(go list ./... | grep -v /vendor/)  # Test code

compile:
  stage: build
  script:
    - echo "Check the go version"
    - go version  # Display the go version
    - go get -d -v   # Get all go dependencies
    - mkdir -p mybinaries  # Create binaries directory
    - go build -o -v mybinaries ./...  # Build go application and save to a directory
  artifacts:
    paths:
      - mybinaries
  environment:
    name: development
  only:
    - dev

All I had to do was to specify an image (image: golang:latest) for the job (compile):

stages:
  - test
  - build
  - deploy

format:
  stage: test
  image: golang:latest
  script:
    - go fmt $(go list ./... | grep -v /vendor/)
    - go vet $(go list ./... | grep -v /vendor/)
    - go test -race $(go list ./... | grep -v /vendor/)  # Test code

compile:
  stage: build
  image: golang:latest
  script:
    - echo "Check the go version"
    - go version  # Display the go version
    - go get -d -v   # Get all go dependencies
    - mkdir -p mybinaries  # Create binaries directory
    - go build -o -v mybinaries ./...  # Build go application and save to a directory
  artifacts:
    paths:
      - mybinaries
  environment:
    name: development
  only:
    - dev

I could also specify the image globally (image: golang:latest) at the top of the script:

image: golang:latest

stages:
  - test
  - build
  - deploy

format:
  stage: test
  script:
    - go fmt $(go list ./... | grep -v /vendor/)
    - go vet $(go list ./... | grep -v /vendor/)
    - go test -race $(go list ./... | grep -v /vendor/)  # Test code

compile:
  stage: build
  script:
    - echo "Check the go version"
    - go version  # Display the go version
    - go get -d -v   # Get all go dependencies
    - mkdir -p mybinaries  # Create binaries directory
    - go build -o -v mybinaries ./...  # Build go application and save to a directory
  artifacts:
    paths:
      - mybinaries
  environment:
    name: development
  only:
    - dev

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aleksey Tsalolikhin
Solution 2 blatinox
Solution 3 Sanjay Bharwani
Solution 4 Promise Preston