为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
呼唤远方 2022-08-01 17:30:23
我正在尝试为我的Go项目制作一个Linux可执行文件。在我的gitlab项目中,我有以下配置。.config-ci.ymldemo_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可执行文件。现在,我得到了.exe仅在Windows上运行的文件。您可以在以下 gitlab 中查看项目详细信息:https://gitlab.com/smilekison/test这是管道作业显示的错误:$ go run test/main.goHello world$ GOOS=linux GOARCH=amd64 go buildGOOS=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
查看完整描述

2 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

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


你可以这样做。与GOARCH相同。然后只需运行.go env -w GOOS="linux"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 映像进行构建的运行器。


查看完整回答
反对 回复 2022-08-01
?
qq_遁去的一_1

TA贡献1725条经验 获得超8个赞

我需要写 go env -w GOOS=“linux” GOARCH=“amd64” 来制作 linux 的可执行文件,如果我想为 Windows 制作可执行文件,我只需要将 linux 重命名为 windows,我就可以使用 image : golang:1.15.7 在这里安装 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


查看完整回答
反对 回复 2022-08-01
  • 2 回答
  • 0 关注
  • 282 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号