简体   繁体   中英

Go GitHub Actions clone private repository to be used from main code

I am trying to automatically build and test my go code on pushes to the master branch of my GitHub repository using GitHub actions.

The basic configuration to do so works perfectly fine:

name: Build & Test

on:
  push:
    branches:
      - master

jobs:
  test:
    ## We want to define a strategy for our job
    strategy:
      ## this will contain a matrix of all of the combinations
      ## we wish to test again:
      matrix:
        go-version: [1.14.x] #[1.12.x, 1.13.x, 1.14.x]
        platform: [ubuntu-latest] #[ubuntu-latest, macos-latest, windows-latest]
    
    ## Defines the platform for each test run
    runs-on: ${{ matrix.platform }}
    
    ## the steps that will be run through for each version and platform
    ## combination
    steps:
    ## sets up go based on the version
    - name: Install Go
      uses: actions/setup-go@v2
      with:
        go-version: ${{ matrix.go-version }}
      env:
        GOPATH: /home/runner/work/project
        GO111MODULE: "on"

    ## runs a build
    - name: Build
      run: go build src/
      env:
        GOPATH: /home/runner/work/project
        GO111MODULE: "on"

    ## runs go test ./...
    - name: Test
      run: go test ./...
      env:
        GOPATH: /home/runner/work/project
        GO111MODULE: "on"

However, the repository has another (private) repository as direct dependency, so I need to clone this as well after checking out the main repository.

- name: Check out dependency
  uses: actions/checkout@v2
  with:
      repository: mydependency
      token: ${{ secrets.GIT_ACCESS_TOKEN }}
      path: src/dependency

Then it will not find the dependency as it is not accessible from GOROOT.

src/main.go:20:2: package dependency is not in GOROOT (/opt/hostedtoolcache/go/1.14.15/x64/src/dependency)

The issue here is that the path is not accessible within the GitHub action, so I cannot clone the repository to this specific path.

I of course can specify the GOROOT as env variable for setup-go , build and test in such a way that the dependency is found, but then it will not find native go packages anymore.

env:
    GOPATH: /home/runner/work/project/go
    GOROOT: /home/runner/work/project
    GO111MODULE: "on" 
package bufio is not in GOROOT (/home/runner/work/project/src/bufio)

So, I managed to solve the issue myself. The checkout@v2 action does only allow relative paths, however, we can just clone the dependency manually.

- name: Checkout dependencies
  run: |
      git clone https://${{ secrets.GIT_ACCESS_TOKEN }}@github.com/myorg/dependency.git ${GOROOT}/src/dependency

In this way, it will also work with different Go versions yielding in a different GOROOT.

The full pipeline steps:

steps:
## sets up go based on the version
- name: Install Go
  uses: actions/setup-go@v2
  with:
    go-version: ${{ matrix.go-version }}
  env:
    GO111MODULE: "on"
- name: Checkout dependencies
  run: |
    git clone https://${{ secrets.GIT_ACCESS_TOKEN }}@github.com/myorg/dependency.git 

## checks out our code locally so we can work with the files
- name: Checkout code
  uses: actions/checkout@v2

## runs a build
- name: Build
  run: go build src

## runs go test ./...
- name: Test
  run: go test ./...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM