在使用GitLab CI/CD制作Golang deb时,…中没有Go文件。

huangapple go评论73阅读模式
英文:

no Go files in ... when on making golang deb in gitlab-ci/cd

问题

这是关于golang的一些内容,我在gitlab-ci.yml文件中使用的代码。
这是我得到的错误信息no Go files in /builds/release_management,如下所示:

$ pwd
/builds/release_management
$ echo $BasePathForBinaryFile1
cmd/main_1/
$ ls
COPYING
DebPackageGitLabDocker
README.md
cmd
deb-build
ermbuild
go.mod
publishToRemote.sh
usr
working_gitlab-ci_ableToCreateDebPackageWithNoBinary.yml
$ echo $CI_PROJECT_DIR/$BasePathForBinaryFile1
/builds/release_management/cmd/main_1/
$ GOOS=$GOOS GOARCH=$GOARCH go build -o $BinaryName1 $BasePathForBinaryFile1
no Go files in /builds/release_management
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 1

这是我的作业代码:

variables:
  GOOS: linux
  GOARCH: amd64
  TagName: 1.0.71
  DebFileName: $TagName
  BasePathForBinaryFile1: cmd/main_1/
  BinaryName: main1
  BasePathForBinaryFile2: cmd/main_2/
  BinaryName: main2

build_binary:
  stage: build
  image: golang:latest
  artifacts:
    untracked: true
  script:
    - cd cmd/main_1
    - GOOS=$GOOS GOARCH=$GOARCH go build -o $BinaryName1 $BasePathForBinaryFile1
#    - GOOS=$GOOS GOARCH=$GOARCH go build -o $BinaryName1 $CI_PROJECT_DIR/$BasePathForBinaryFile1

请注意:我也尝试过使用$CI_PROJECT_DIR/$BasePathForBinaryFile1,但也没有起作用。

然而,当我先使用cd命令进入目录,然后使用点号(.)从当前目录构建时,它可以工作:

variables:
  GOOS: linux
  GOARCH: amd64
  TagName: 1.0.71
  DebFileName: $TagName
  BasePathForBinaryFile1: cmd/main_1/
  BinaryName: main1
  BasePathForBinaryFile2: cmd/main_2/
  BinaryName: main2

build_binary:
  stage: build
  image: golang:latest
  artifacts:
    untracked: true
  script:
    - cd cmd/main_1
    - GOOS=$GOOS GOARCH=$GOARCH go build -o $BinaryName .

这是我的文件夹结构:

在使用GitLab CI/CD制作Golang deb时,…中没有Go文件。

有任何想法可以解决这个golang错误吗?

编辑1: 另外,当执行cd $CI_PROJECT_DIR/$BasePathForBinaryFile然后ls时,它不会进入该目录,仍然只显示基本目录的内容:

$ echo $CI_PROJECT_DIR/$BasePathForBinaryFile1
/builds/SugarBox/edge_release_management/cmd/main_1/
$ cd $CI_PROJECT_DIR/$BasePathForBinaryFile
$ ls
COPYING
DebPackageGitLabDocker
README.md
cmd
deb-build
ermbuild
go.mod
publishToRemote.sh
usr
working_gitlab-ci_ableToCreateDebPackageWithNoBinary.yml
英文:

This is something regarding golang, code of which I am using in gitlab-ci.yml file.
This is the error which I am getting no Go files in /builds/release_management as shown:

$ pwd
/builds/release_management
$ echo $BasePathForBinaryFile1
cmd/main_1/
$ ls
COPYING
DebPackageGitLabDocker
README.md
cmd
deb-build
ermbuild
go.mod
publishToRemote.sh
usr
working_gitlab-ci_ableToCreateDebPackageWithNoBinary.yml
$ echo $CI_PROJECT_DIR/$BasePathForBinaryFile1
/builds/release_management/cmd/main_1/
$ GOOS=$GOOS GOARCH=$GOARCH go build -o $BinaryName1 $BasePathForBinaryFile1
no Go files in /builds/release_management
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 1

Here is the code of my job

variables:
  GOOS: linux
  GOARCH: amd64
  TagName: 1.0.71
  DebFileName: $TagName
  BasePathForBinaryFile1: cmd/main_1/
  BinaryName: main1
  BasePathForBinaryFile2: cmd/main_2/
  BinaryName: main2

build_binary:
  stage: build
  image: golang:latest
  artifacts:
    untracked: true
  script:
    - cd cmd/main_1
    - GOOS=$GOOS GOARCH=$GOARCH go build -o $BinaryName1 $BasePathForBinaryFile1
#    - GOOS=$GOOS GOARCH=$GOARCH go build -o $BinaryName1 $CI_PROJECT_DIR/$BasePathForBinaryFile1

Please note: I have also tried by giving $CI_PROJECT_DIR/$BasePathForBinaryFile1 and that is also not working.

While, this works when I am doing cd first then building it from current using dot(.)

variables:
  GOOS: linux
  GOARCH: amd64
  TagName: 1.0.71
  DebFileName: $TagName
  BasePathForBinaryFile1: cmd/main_1/
  BinaryName: main1
  BasePathForBinaryFile2: cmd/main_2/
  BinaryName: main2

build_binary:
  stage: build
  image: golang:latest
  artifacts:
    untracked: true
  script:
    - cd cmd/main_1
    - GOOS=$GOOS GOARCH=$GOARCH go build -o $BinaryName .

Here is my folder structure:

在使用GitLab CI/CD制作Golang deb时,…中没有Go文件。

Any idea what thing should I fix to fix this golang error?

Edit 1: Also, when doing cd $CI_PROJECT_DIR/$BasePathForBinaryFile and then ls it is not going into that directory and still showing content of base directory only:

$ echo $CI_PROJECT_DIR/$BasePathForBinaryFile1
/builds/SugarBox/edge_release_management/cmd/main_1/
$ cd $CI_PROJECT_DIR/$BasePathForBinaryFile
$ ls
COPYING
DebPackageGitLabDocker
README.md
cmd
deb-build
ermbuild
go.mod
publishToRemote.sh
usr
working_gitlab-ci_ableToCreateDebPackageWithNoBinary.yml

答案1

得分: 0

有几个问题:

  1. 在你的配置中没有BinaryName1,所以
GOOS=$GOOS GOARCH=$GOARCH go build -o $BinaryName1 $BasePathForBinaryFile1

变成了

GOOS=$GOOS GOARCH=$GOARCH go build -o cmd/main_1/

而源文件应该在当前目录中,但实际上并不存在。你需要修复配置,将BinaryName1BinaryName2分别替换为BinaryName

  1. 你需要将源目录指定为./cmd/main_1/
  2. Edit 1部分,cd命令无法工作,因为环境变量名称不正确,应该是$BasePathForBinaryFile1,而不是$BasePathForBinaryFile
英文:

There are a few problems:

  1. There is no BinaryName1 in your config so
GOOS=$GOOS GOARCH=$GOARCH go build -o $BinaryName1 $BasePathForBinaryFile1

Becomes

GOOS=$GOOS GOARCH=$GOARCH go build -o cmd/main_1/

and the source files are expected in current dir, while they are not there. You need to fix config to have BinaryName1 and BinaryName2 instead of having BinaryName twice.

  1. You need to specify src dir as ./cmd/main_1/.
  2. In Edit 1 part cd doesn't work because the env name is incorrect it should be $BasePathForBinaryFile1 but it is $BasePathForBinaryFile.

huangapple
  • 本文由 发表于 2022年12月16日 17:47:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/74822708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定