如何使用 gitlab (go env) 制作一个 linux 可执行文件?

How to make a linux executable file using gitlab (go env)?

我正在尝试为我的 Go 项目创建一个 Linux 可执行文件。我在我的gitlab项目中的.config-ci.yml中有以下配置。

demo_job_1:
    tags:
        - cpf
        - cpf-test
        - testing
        - unit-testing
    script:
        - go run test/main.go
        - GOOS=linux GOARCH=amd64 go build
        - go env
        - cd test
        - ./test
        - echo Successfully run and Built

在 运行 这条管道之后,我在签入 env 文件时仍然得到 GOOS=windows。我如何构建我的项目,以便构建后的输出是 Linux 可执行文件。现在,我正在获取仅在 Windows 上运行的 .exe 文件。

可以在下面的gitlab中查看项目详情:

https://gitlab.com/smilekison/test

这是管道作业显示的错误:

$ go run test/main.go
Hello world
$ GOOS=linux GOARCH=amd64 go build

GOOS=linux : The term 'GOOS=linux' is not recognized as the name of a cmdlet, function, script file, or operable 
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\WINDOWS\TEMP\build_script487326907\script.ps1:207 char:1
+ GOOS=linux GOARCH=amd64 go build
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (GOOS=linux:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

首先要解决您的实际错误:您似乎使用的是基于 windows 的 运行ner。这意味着您必须使用 windows CMD 命令。它不知道 ENV 等

您可以改为 go env -w GOOS="linux"。与 GOARCH 相同。然后 运行 go build ..

您还可以使用变量部分用环境变量覆盖 go env

variables:
  GOOS: "linux"
  GOARCH: "amd64"

它位于 gitlab 文件顶部的某处。


这是我使用 docker 容器的 Go 项目的典型构建管道:

build_App:
  image: golang:1.15.3
  stage: build
  allow_failure: false
  tags:
    - unix
  script:
    - go mod download
    - mkdir $CI_PROJECT_DIR/release
    - cd cmd/app
    - GOOS=linux GOARCH=amd64 go build -o $CI_PROJECT_DIR/release/app .
  artifacts:
    paths:
      - $CI_PROJECT_DIR/release

和测试管道

go_test:
  image: golang:1.15.3
  stage: verify
  allow_failure: false
  tags:
    - unix
  script:
    - go mod download
    - go test -race -cover ./...

这是基于使用 docker 图像构建的 运行ner。

我需要写 go env -w GOOS="linux" GOARCH="amd64" 来制作linux 的可执行文件,如果我想为 windows 创建可执行文件,我只需将 linux 重命名为 windows 我可以在此处使用 image 来安装 go 语言:golang:1.15.7。这样我的 .gitlab-ci.yml 文件就可以安装 GO Lang 并且可以 运行 任何 go 命令。

demo_job_1:
stages:
  -build
    
build:
  stage: build
  image : golang:1.15.7
  tags:
    - cpf
    - cpf-test
    - testing
    - unit-testing
  script:
    - go run test/main.go
    - go env -w GOOS=linux GOARCH=amd64
    - go env
    - cd test
    - ./test
    - echo Successfully run and Built