Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

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

I am very new to gitlab pipelines. I am trying to make a Linux executable file of my go language project. I have following configuration in my .config-ci.yml in my gitlab project.

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

After running this pipeline, I still get the GOOS=windows when i check in env file. How can i build my project so that the output after building is of Linux executable file. Right now, I am getting .exe file which runs on Windows only.

You can see the project details in the following gitlab:

https://gitlab.com/smilekison/test

This is the error that is shown by Pipeline Job:

$ 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:WINDOWSTEMPuild_script487326907script.ps1:207 char:1
+ GOOS=linux GOARCH=amd64 go build
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (GOOS=linux:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
question from:https://stackoverflow.com/questions/65841681/how-to-make-a-linux-executable-file-using-gitlab-go-env

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First to address your actual error: it seems you are on a windows based runner. That means you have to use windows CMD commands. It does not know ENV, etc.

You can do go env -w GOOS="linux" instead. Same with GOARCH. Then just run go build ..

You can also use a variables section to overwrite go env with environment variables:

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

It goes at the top of the gitlab file somewhere.


Here my typical build pipeline for Go projects using docker containers:

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

And test pipeline

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

This is based on a runner that uses docker images to build in.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...