GitHub Action unable to execute build executable - go

Try to use GitHub Actions but getting error when trying to run executable build. I can find executable when doing ls and also changing mod of file executable but didn't got anything. Attached log/output of GitHub action.
Here is my github action workflow file
on:
push:
branches:
- master
name: Build For platforms
jobs:
build:
# 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]
platform: [ubuntu-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 }}
# checks out our code locally so we can work with the files
- name: Checkout code
uses: actions/checkout#v2
- name: Download modules
run: go mod download
- name: Build
run: go build -o executable main.go
- name: Chmod
run: chmod +x executable
- name: List Files
run: ls && pwd
- name: Run
run: ./executable

Related

Build command not found. 'go'

....
jobs:
setup:
name: Setup
runs-on: [self-hosted, Linux, X64]
steps:
- name: Set up Go 1.17
uses: actions/setup-go#v2
with:
go-version: 1.17
id: Go
- name: work around permission issue
run: git config --global --add safe.directory /__w/****-***/****-***
- name: Checkout code into go module directory
uses: actions/checkout#v2
- name: Make Directory
run: mkdir build
- name: Build
run: |
CGO_ENABLED=0 go build -ldflags "-linkmode external" -o main
- name: Upload Build
uses: actions/upload-artifact#v2
with:
name: binary
path: main
scans :
needs: setup
name: all scans
runs-on: [self-hosted, Linux, X64]
steps:
- name: Download build for build and test
uses: actions/download-artifact#v2
with:
name: build
- name: Coverity Scan
env:
AUTH_DATA: ${{ secrets.COVERITY_KEY_FILE }}
run: |
export PATH=$PATH:/opt/coverity/coverity-base/bin
mkdir coverity
cov-build --dir coverity go build main.go
cov-analyze --dir coverity --strip-path=`pwd`
touch auth_key_file
chmod 600 auth_key_file
echo $( printf "%s" "$AUTH_DATA" ) > auth_key_file
cov-commit-defects --dir coverity --url `suspicious_url` --auth-key-file auth_key_file --stream ****-overrides
- name: Upload Coverity
uses: actions/upload-artifact#v2
with:
name: coverity
path: coverity
I have this workflow file for my Golang project. I want to run the Coverity scan. But when I am running this it's throwing an error :
I have set up go in setup step. I don't know why it is telling that it did not found the go command. Please help me with this.
Go may not be in the path of the process executing that command. You may need to modify the $PATH variable to include the path to your go binary.
Looks like you're only updating the $PATH in scans, but this ought to be in setup I think:
export PATH=$PATH:/opt/coverity/coverity-base/bin

How to goreleaser to build sub-folder in github

I use goreleaser to build in github action.
Because my main.go in ./cmd/tes_cli, it always show error in github action.
repo does not contain a main function.
I check the original document, it seems "builds" could works. my configuration could not add builds
name: Release Go project
on:
push:
tags:
- "*" # triggers only if push new tag version, like `0.8.4` or else
jobs:
build:
name: GoReleaser build
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout#v2
with:
fetch-depth: 0 # See: https://goreleaser.com/ci/actions/
- name: Set up Go 1.14
uses: actions/setup-go#v2
with:
go-version: 1.14
id: go
- name: Run GoReleaser
uses: goreleaser/goreleaser-action#master
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Add an extra configuration file to resolve it.
Refer https://github.com/kkdai/disqus-importor-go/pull/4/files
args: release -f .goreleaser.yml --rm-dist
link to another config file.
# .goreleaser.yml
project_name: import_disqus_cli
builds:
- env: [CGO_ENABLED=0]
goos:
- linux
- windows
- darwin
goarch:
- amd64
- arm64
id: "import_disqus_cli"
dir: .
main: ./cmd/test_cli

How to use Go Release Binary GitHub Action

Anyone able to get Go Release Binary GitHub Action working? which is supposed to
Automate publishing Go build artifacts for GitHub releases through GitHub Actions
The readme looks rather simple, but I've tried all my imaginations to get it working but not avail. Similar questions has been asked in its issue tracks but got no answer.
Somebody help please.
BTW, while searching for the answer, I came upon this commit logs, which is quite interesting/amusing to read. I.e., it seems to be quite a battle to get it working, but the author gave up eventually (no any releases from his/her latest commits/tags)
Conclusion:
Turns out that my project does not have go mod and there were issues in Go Release which stops it from working. It was then fixed by this and this.
I actually wrote my own release workflow for generating Go binaries.
The only non-obvious steps from my point of view are:
I have a release note generation step where I include a list of non-merge commits since the last release tag.
I use a matrix build for GOOS and GOARCH pairs and do some Bash string manipulation in the "Get OS and arch info" step.
The nice thing about softprops/action-gh-release is that you can keep adding artifacts to the same release as long as the workflow run is triggered by a push to the same tag.
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
name: Latest Release
defaults:
run:
shell: bash
jobs:
lint:
name: Lint files
runs-on: 'ubuntu-latest'
steps:
- uses: actions/checkout#v2
- uses: actions/setup-go#v2
with:
go-version: '1.16.3'
- name: golangci-lint
uses: golangci/golangci-lint-action#v2.5.2
with:
version: latest
test:
name: Run tests
runs-on: 'ubuntu-latest'
needs: lint
steps:
- uses: actions/checkout#v2
- uses: actions/setup-go#v2
with:
go-version: '1.16.3'
- run: go test -v -cover
release:
name: Create Release
runs-on: 'ubuntu-latest'
needs: test
strategy:
matrix:
# List of GOOS and GOARCH pairs from `go tool dist list`
goosarch:
- 'aix/ppc64'
# etc
steps:
- name: Checkout code
uses: actions/checkout#v2
with:
fetch-depth: 0
- uses: actions/setup-go#v2
with:
go-version: '1.16.3'
- name: Get OS and arch info
run: |
GOOSARCH=${{matrix.goosarch}}
GOOS=${GOOSARCH%/*}
GOARCH=${GOOSARCH#*/}
BINARY_NAME=${{github.repository}}-$GOOS-$GOARCH
echo "BINARY_NAME=$BINARY_NAME" >> $GITHUB_ENV
echo "GOOS=$GOOS" >> $GITHUB_ENV
echo "GOARCH=$GOARCH" >> $GITHUB_ENV
- name: Build
run: |
go build -o "$BINARY_NAME" -v
- name: Release Notes
run:
git log $(git describe HEAD~ --tags --abbrev=0)..HEAD --pretty='format:* %h %s%n * %an <%ae>' --no-merges >> ".github/RELEASE-TEMPLATE.md"
- name: Release with Notes
uses: softprops/action-gh-release#v1
with:
body_path: ".github/RELEASE-TEMPLATE.md"
draft: true
files: ${{env.BINARY_NAME}}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

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 ./...

Can you have multiple working directories with github actions?

So I have a repo with multiple directories for multiple go projects. Is there a way to run github actions on multiple working directories so I don't have to have redundant yaml with github actions? To automate an error check with golang, I currently have:
errcheck:
name: Errcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#master
- name: check
uses: grandcolline/golang-github-actions#v1.1.0
working-directory: ./app1
with:
run: errcheck
token: ${{ secrets.GITHUB_TOKEN }}
But I'd like to have:
errcheck:
name: Errcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#master
- name: check
uses: grandcolline/golang-github-actions#v1.1.0
working-directory: [./app1, ./app2]
with:
run: errcheck
token: ${{ secrets.GITHUB_TOKEN }}
In order to run something in more than one working directory, I believe you have two options:
Option 1: Matrix
Use GitHub Action's jobs.<job_id>.strategy.matrix option. This will create multiple jobs, each with its own matrix (directory) value.
Here is a sample workflow:
name: Test
on:
push: { branches: master }
jobs:
test:
name: Matrix test
runs-on: ubuntu-latest
strategy:
matrix: { dir: ['some-dir', 'other-dir'] }
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Do something with the matrix value
working-directory: ${{ matrix.dir }}
run: pwd
Running this will create two jobs:
Option 2: Custom Shell Script
If the matrix option is not suitable for your needs, a simple shell script that loops through and tests all your nested applications (directories) might be appropriate. In this case, you ignore the working-direcoty directive in the workflow YAML, and let the script cd to each of them.
For example:
#!/usr/bin/env bash
dirs=( some-dir other-dir )
for dir in "${dirs[#]}"; do
pushd "$dir"
pwd # Do something more significant here
popd
done

Resources