亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用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可執行文件?,F在,我得到了.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 關注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號