Answers: <p>My approach nowadays, always package your build process into Dockerfile. I.e. here is Dockerfile for one of my open-sourced Go projects:</p>
  <p><a href="https://github.com/relizaio/relizaGoClient/blob/master/Dockerfile" rel="nofollow noreferrer">https://github.com/relizaio/relizaGoClient/blob/master/Dockerfile</a></p>
  <p><em>Note</em>: if you're using go modules (which seems to be the case), replace <code>RUN go get -d -v ./...</code> with <code>RUN go mod download</code> in the build-stage. BTW, missing <code>go mod</code> instruction might be the reason for your build failure in the current setup.</p>
  <p>Then in your CI, simply do <code>docker build</code> instruction, and then copy your artifact from the container if you need it outside.</p>
  <p><em>Key benefit</em>: you can verify build process against same Dockerfile locally and you can port it to any CI tool whatsoever.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-06-18T23:45:19.287Z
Answers: 2020-06-18T23:45:19.287Z
Answers: taleodor
Body: <p>Go and devops are both new things for me. I'd like to setup a CI that tests and builds my module, then stores artifacts for download. In addition it'd be good to cache dependencies for next builds.</p>
  <p>I don't get how to do it using subdirectory for the go product. I want my git repository to look like that:</p>
  <pre><code>+ repository_root/
    + go/
      - go.mod
      - go.sum
      + hello/
        - main.go
    + cpp/
    + python/
    + .circleci/
  </code></pre>
  <p>go.mod</p>
  <pre><code>module github.com/{USER}/{REPOSITORY_ROOT}/go
  
  go 1.14
  </code></pre>
  <p>Of course {USER} is replace with my github user name and {REPOSITORY_ROOT} is the repo name.</p>
  <p>For now I have a following config.yml that doesn't work.</p>
  <pre><code>version: 2
  jobs:
    build:
      working_directory: ~/project/go
      docker:
        - image: circleci/golang:1.9
  
      steps:
        - checkout:
            path: ~/project/
  
        - run: mkdir bin
        - run: go get -v -t -d ./...
        - run: go test -v ./...
        - run: go build -o bin/hello.exe github.com/{USER}/{REPOSITORY_ROOT}/go/hello
  
        - store_artifacts:
            path: bin/
            destination: hello-app
  </code></pre>
  <p>Error on build command:</p>
  <pre><code>#!/bin/bash -eo pipefail
  go build -o ./bin/hello.exe github.com/{USER}/{REPO_ROOT}/go/hello
  can't load package: package github.com/{USER}/{REPO_ROOT}/go/hello: cannot find package &quot;github.com/{USER}/{REPO_ROOT}/go/hello&quot; in any of:
      /usr/local/go/src/github.com/{USER}/{REPO_ROOT}/go/hello (from $GOROOT)
      /go/src/github.com/{USER}/{REPO_ROOT}/go/hello (from $GOPATH)
  
  Exited with code exit status 1
  CircleCI received exit code 1
  </code></pre>
  <p>I know that the module is outside of <code>GOPATH</code> but I have the same setup on my local machine and the same build command works just fine.</p>
  
Tags: <cicd><circleci><go>
Title: golang and circle CI setup
UserDisplayName: LukeN
ThreadText: # golang and circle CI setup
  
  (Asked by: username_0 on 2020-06-18)
  
  Go and devops are both new things for me. I'd like to setup a CI that tests and builds my module, then stores artifacts for download. In addition it'd be good to cache dependencies for next builds.
  
  I don't get how to do it using subdirectory for the go product. I want my git repository to look like that:
  
  ```
  + repository_root/
    + go/
      - go.mod
      - go.sum
      + hello/
        - main.go
    + cpp/
    + python/
    + .circleci/
  
  ```
  
  go.mod
  
  ```
  module github.com/{USER}/{REPOSITORY_ROOT}/go
  
  go 1.14
  
  ```
  
  Of course {USER} is replace with my github user name and {REPOSITORY\_ROOT} is the repo name.
  
  For now I have a following config.yml that doesn't work.
  
  ```
  version: 2
  jobs:
    build:
      working_directory: ~/project/go
      docker:
        - image: circleci/golang:1.9
  
      steps:
        - checkout:
            path: ~/project/
  
        - run: mkdir bin
        - run: go get -v -t -d ./...
        - run: go test -v ./...
        - run: go build -o bin/hello.exe github.com/{USER}/{REPOSITORY_ROOT}/go/hello
  
        - store_artifacts:
            path: bin/
            destination: hello-app
  
  ```
  
  Error on build command:
  
  ```
  #!/bin/bash -eo pipefail
  go build -o ./bin/hello.exe github.com/{USER}/{REPO_ROOT}/go/hello
  can't load package: package github.com/{USER}/{REPO_ROOT}/go/hello: cannot find package "github.com/{USER}/{REPO_ROOT}/go/hello" in any of:
      /usr/local/go/src/github.com/{USER}/{REPO_ROOT}/go/hello (from $GOROOT)
      /go/src/github.com/{USER}/{REPO_ROOT}/go/hello (from $GOPATH)
  
  Exited with code exit status 1
  CircleCI received exit code 1
  
  ```
  
  I know that the module is outside of `GOPATH` but I have the same setup on my local machine and the same build command works just fine.
  
  **Tags:** cicd, circleci, go
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-06-18. Score: 2
  
  My approach nowadays, always package your build process into Dockerfile. I.e. here is Dockerfile for one of my open-sourced Go projects:
  
  <https://github.com/relizaio/relizaGoClient/blob/master/Dockerfile>
  
  *Note*: if you're using go modules (which seems to be the case), replace `RUN go get -d -v ./...` with `RUN go mod download` in the build-stage. BTW, missing `go mod` instruction might be the reason for your build failure in the current setup.
  
  Then in your CI, simply do `docker build` instruction, and then copy your artifact from the container if you need it outside.
  
  *Key benefit*: you can verify build process against same Dockerfile locally and you can port it to any CI tool whatsoever.
  
  
  ---
  
  