我有一个本地GitLab服务器和带有Docker执行器的GitLab ci运行程序。
我想使用gitlab ci来构建(第一阶段(我的Maven项目。由于我使用buildnumber maven插件,我在gitlab-ci.yml中添加了一个Git服务,如下所示:
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
但是当执行git --version
时,我得到了command not found error
。这是我完整的GitLab CI控制台日志:
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
服务是在作业期间运行的另一个docker容器(例如数据库(。docker镜像alpine/git:latest
不是服务,它用于执行git命令。您可以在日志中看到,在没有命令的情况下调用git
。
不应该使用service
,而应该使用安装了git
的docker映像来运行作业。这样,您的脚本将能够调用git
。
如果您愿意,可以在maven
容器中安装git
。示例(存根(.gitlab-ci.yml
:
image: maven:latest
myjob:
script:
- yum install -y git
- git --version
来自CI作业日志:
$ git --version
git version 1.8.3.1
发生错误,因为git在执行构建的运行程序上不可用。
如前几篇文章所述,使用服务效率低下。
services:
- alpine/git:latest
而且,每次在映像上安装git都很耗时,而且资源也很重。
myjob:
script:
- yum install -y git
- git --version
因此,最有效的方法是使用预先安装了git的映像并执行git任务。
下面是我的例子,我使用dockerhub中alpine/git的图像https://hub.docker.com/r/alpine/git并在从该映像启动的容器上执行我的作业。
create-git-tag:
image: alpine/git
stage: tag
script:
- git --version
我在使用GitLab CI.将Go应用程序部署到AWS时遇到了类似的问题
这是我得到的错误代码:
$ 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
以下是我修复它的方法:
问题是我没有为作业(编译作业(指定图像(image: golang:latest
(:
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
我所要做的就是为作业(编译(指定一个图像(image: golang:latest
(:
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
我还可以在脚本顶部全局指定图像(image: golang:latest
(:
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