Home  >  Article  >  Backend Development  >  When making golang deb in gitlab-ci/cd, there are no Go files in...

When making golang deb in gitlab-ci/cd, there are no Go files in...

王林
王林forward
2024-02-13 13:50:09504browse

在 gitlab-ci/cd 中制作 golang deb 时,...中没有 Go 文件

php editor Xigua will introduce you to a common problem when making golang deb in gitlab-ci/cd. Sometimes, when we try to make golang deb package in gitlab-ci/cd, we may encounter an error that "No Go file in...". The reason for this error is because there are no Go files in the root directory of the project. Next, we will explain to you in detail how to solve this problem so that you can successfully create golang deb packages.

Question content

This is about golang, whose code I use in the gitlab-ci.yml file. This is the error I'm getting for no go files in /builds/release_management like this:

$ 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

This is my working code

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 also tried giving $ci_project_dir/$basepathforbinaryfile1 but that didn't work either.

Although, this only works when I first do a cd and then use dot(.) to build it from the current one

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 .

This is my folder structure:

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

Edit 1: Also, when doing cd $ci_project_dir/$basepathforbinaryfile and then ls, it does not go into the directory and still only shows Contents of the base directory:

$ 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

Solution

There are several problems:

  1. There is no binaryname1
  2. in your configuration
goos=$goos goarch=$goarch go build -o $binaryname1 $basepathforbinaryfile1

become

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

The source file should be located in the current directory, but does not exist. You need to fix the configuration to have binaryname1 and binaryname2 instead of binaryname twice.

  1. You need to specify the src directory as ./cmd/main_1/.
  2. In edit 1 section cd does not work because the environment name is incorrect, it should be $basepathforbinaryfile1 but it is $basepathforbinaryfile.

The above is the detailed content of When making golang deb in gitlab-ci/cd, there are no Go files in.... For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete