【问题标题】:How do I create an artifact so it will be available for download in .gitlab-ci.yml如何创建一个工件,以便它可以在 .gitlab-ci.yml 中下载
【发布时间】:2019-11-01 08:54:21
【问题描述】:

我有一个 GitLab ci 管道,但我不知道如何让它在构建阶段生成一个带有二进制文件的工件。

这是我的 yml 文件...

stages:
  - test
  - build
  - art

image: golang:1.9.2

variables:
  BIN_NAME: example
  ARTIFACTS_DIR: artifacts
  GO_PROJECT: example


before_script:
  - mkdir -p ${GOPATH}/src/${GO_PROJECT}
  - mkdir -p ${CI_PROJECT_DIR}/${ARTIFACTS_DIR}
  - go get -u github.com/golang/dep/cmd/dep
  - cp -r ${CI_PROJECT_DIR}/* ${GOPATH}/src/${GO_PROJECT}/
  - cd ${GOPATH}/src/${GO_PROJECT}

test:
  stage: test


  script:
    # Run all tests
    go test -run ''


build:
  stage: build

  script:
    # Compile and name the binary as `hello`
    - go build -o hello
    # Execute the binary
    - ./hello

art:
  script:
  artifacts:
    paths:
    - ./hello

测试和构建阶段运行良好,但艺术阶段在添加到 yml 文件时却没有。

我在网上找到了很多示例,但很难将它们转换为我的确切情况。

我只想让工件以下载的形式出现在管道上,就像在这个链接中一样。

Downloading artifacts

在尝试了建议的解决方案后,我得到了以下...

$ go build -o hello
$ ./hello
Heldfgdfglo 2
Uploading artifacts...
WARNING: ./hello: no matching files                
ERROR: No files to upload                          
Job succeeded

尝试添加..

GOPATH: /go

还有……

- cd ${GOPATH}/src/${GO_PROJECT}

现在出现以下错误...

Uploading artifacts...
WARNING: /go/src/example/hello: no matching files  
ERROR: No files to upload                          
Job succeeded

按要求共享输出...

 mkdir -p ${GOPATH}/src/${GO_PROJECT}
$ mkdir -p ${CI_PROJECT_DIR}/${ARTIFACTS_DIR}
$ go get -u github.com/golang/dep/cmd/dep
$ cp -r ${CI_PROJECT_DIR}/* ${GOPATH}/src/${GO_PROJECT}/
$ cd ${GOPATH}/src/${GO_PROJECT}
$ go build -o hello
$ pwd
/go/src/example
$ ls -l hello
-rwxr-xr-x. 1 root root 1859961 Jun 19 08:27 hello
$ ./hello
Heldfgdfglo 2
Uploading artifacts...
WARNING: /go/src/example/hello: no matching files  
ERROR: No files to upload                          
Job succeeded

【问题讨论】:

    标签: gitlab gitlab-ci


    【解决方案1】:

    ./hello 与您的工件路径不匹配,因为您在运行脚本之前更改了目录。

    您需要将生成的可执行文件移动到 gitlab 运行器的原始工作目录,因为工件路径只能相对于构建目录:

    build:
      stage: build
    
      script:
        # Compile and name the binary as `hello`
        - go build -o hello
        # Execute the binary
        - ./hello
        # Move to gitlab build directory
        - mv ./hello ${CI_PROJECT_DIR}
    
      artifacts:
        paths:
        - ./hello
    

    https://gitlab.com/gitlab-org/gitlab-ce/issues/15530

    【讨论】:

    • 对于图像golang:1.9.2,你有GOPATH=/go
    • 仍然收到错误我已将错误详细信息添加到问题中。
    • 你能在go build -o hello之后分享pwdls -l hello的输出吗?
    • 我已经添加了新的输出
    • 查看新答案。
    【解决方案2】:

    您需要在创建它们的作业中指定您的工件路径,因为每个作业都会触发一个新的空环境(或多或少考虑缓存):

    build:
      stage: build
    
      script:
        # Compile and name the binary as `hello`
        - go build -o hello
        # Execute the binary
        - ./hello
    
      artifacts:
        paths:
        - ./hello
    

    【讨论】:

    • 谢谢,但我试过了,虽然 yaml 现在有效,但它仍然无法创建供下载的工件,有什么想法吗?
    • 尝试删除./。如果这不起作用,请在脚本中添加对 lsfind 的调用。
    猜你喜欢
    • 2018-04-13
    • 2019-09-27
    • 2016-07-16
    • 2017-07-27
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    相关资源
    最近更新 更多