I want to get project version from pom.xml in Github Actions - maven

My github actions code is as follows. I want to save the version to Environment.
name: Windows x64 Build
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout#v2
- name: Cache local Maven repository
uses: actions/cache#v2
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Set up JDK 14
uses: actions/setup-java#v2
with:
java-version: 14
distribution: 'adopt'
architecture: x64
javafx: true
- name: Get version
run: echo project_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) >> $GITHUB_ENV
when I run it I get this error and I couldn't find a solution.
Run echo project_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) >> $GITHUB_ENV
echo project_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) >> $GITHUB_ENV
shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
env:
JAVA_HOME: C:\hostedtoolcache\windows\Java_Adopt_jdk\14.0.2-12\x64
Error: Process completed with exit code 1.
I'll be happy if you can help me.

The code you have there should work correctly if it were executed on bash. The only problem is that for Windows runner the default shell is powershell, which uses slightly different syntax:
- name: Get version
run: $project_version=mvn help:evaluate -Dexpression=project.version -q -DforceStdout; echo "project_version=$project_version" >> $Env:GITHUB_ENV

This syntax didn't work either, but I found this link and I changed my actions with this:
- name: Get version
shell: bash -l {0}
run: echo project_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) >> $GITHUB_ENV
And wadaaa.

Related

run pre-commit.com script for golang in github actions

I'm trying to run pre-commit.com script with some hooks related to golang in github actions. Seems like the testing environment lack of some tools to execute go-imports and golangci-lint.
I've added steps for setting up required tools in the environment prior to pre-commit step, but it doesn't help.
.pre-commit-config.yaml:
repos:
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.0
hooks:
- id: go-imports
- id: golangci-lint
- id: go-unit-tests
github action file config:
name: pre-commit
on:
pull_request:
push:
branches: [main]
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-python#v2
- uses: actions/setup-go#v3
- run: go install golang.org/x/tools/cmd/goimports#latest
- run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.49.0
- uses: pre-commit/action#v2.0.2
Gihub Action Output:
all go invironments set-up steps completed successfully
Details of pre-commit/action#v2.0.2:
[...]
[INFO] This may take a few minutes...
go imports...............................................................Failed
- hook id: go-imports
- exit code: 127
/home/runner/.cache/pre-commit/repow0byklud/run-go-imports.sh: line 8: goimports: command not found
golangci-lint............................................................Failed
- hook id: golangci-lint
- exit code: 127
/home/runner/.cache/pre-commit/repow0byklud/run-golangci-lint.sh: 2: exec: golangci-lint: not found
go-unit-tests............................................................Passed
[...]
So, the issue was that .../go/bin directory are not being added to $PATH in the execution environment after go tools installation (so goimports and golangci-lint are not visible for BASH)
($PATH is itself being wrapped in the $GITHUB_ENV due to github actions specific.)
This statement prior to pre-commit action execution can resolve the issue (see full code in the end):
run: echo "PATH=$PATH:/home/runner/go/bin" >> $GITHUB_ENV
Thanks for #Anthony Sottile in the comments to original question
Github Action settings code:
name: pre-commit
on:
pull_request:
push:
branches: [main]
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-python#v2
- uses: actions/setup-go#v3
- run: go install golang.org/x/tools/cmd/goimports#latest
- run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s - -b $(go env GOPATH)/bin v1.49.0
- run: echo "PATH=$PATH:/home/runner/go/bin" >> $GITHUB_ENV
- uses: pre-commit/action#v2.0.2

How to bump version with GitHub Actions

I want to bump the version by running my shell script. I am manually triggering the GitHub Actions with inputs. I am using GitHub API to get the latest version and I want to run my shell script with the version taken from GitHub API.
I am able to achieve everything. It's just that I am not able to get the version output.
This is my GitHub Workflow:
#############################################################################
# GitHub Action to bump release version
#
#############################################################################
name: "Bump version and Update Milestone"
on:
workflow_dispatch:
inputs:
version:
description: 'New Version'
required: true
default: 'warning'
permissions:
contents: write
pull-requests: write
jobs:
printInputs:
runs-on: ubuntu-latest
steps:
- run: |
echo "Latest Version: ${{ github.event.inputs.version }}"
bump:
name: Bump version
runs-on: ubuntu-latest
steps:
- name: Checkout the latest code
uses: actions/checkout#v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Get the version
id: get_version
run: |
VERSION=$(curl -s https://api.github.com/repos/checkstyle/checkstyle/releases/latest \
| jq ".tag_name")
echo VERSION="$VERSION"
- name: Modify File
run: |
./.ci/bump-version.sh ${{ steps.get_version.VERSION }}
- name: Push commit
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]#users.noreply.github.com'
git commit -am "minor: Bump release version to ${{ steps.get_version.VERSION }}"
git push
- name: GitHub Milestone
run: |
.ci/update-github-milestone.sh
This is my shell script:
#!/bin/bash
set -e
VERSION=$1
echo VERSION="$VERSION"
echo "bump version in pom.xml"
mvn versions:set -DnewVersion="$VERSION" && mvn versions:commit
And these is my tested actions where I want help:
Action link: https://github.com/Rahulkhinchi03/checkstyle/runs/7785606554?check_suite_focus=true
To transfer an output from one step to another, you need to use a workflow command:
echo "::set-output name=VERSION::$VERSION"
and then reference it like this in a later step via the outputs object:
run: |
./.ci/bump-version.sh ${{ steps.get_version.outputs.VERSION }}

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 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 }}

Make the build failure when code coverage is less than x%

I'm working with github actions and in my tests I need to make myt build fail when my code coverage percentage is less than 80%. I looked up some github actions in github marketplace but dont find anything. Can I do it ? I'm linking my workflow file if it migth help
---
name: lint build and test
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout#v2
- name: Set up Go
uses: actions/setup-go#v2
with:
go-version: 1.15
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Super-Linter
uses: github/super-linter#v3.14.0
env:
VALIDATE_GO: false
VALIDATE_JSCPD: false
VALIDATE_ALL_CODEBASE: true
DEFAULT_BRANCH: master
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: golangci-lint
uses: golangci/golangci-lint-action#v2
with:
version: v1.29
- name: Build
run: go build -o apiDateTime -v ./...
- name: Test
run: go test ./... -coverprofile cover.out -covermode atomic
- name: Coverage result
run: go tool cover -func cover.out
I would replace invocation of go test with an invocation of a shell script as explained here.
The shell script would look something like this
!#/bin/sh
set -e -u
go test ./... -coverprofile cover.out -covermode atomic
perc=`go tool cover -func=cover.out | tail -n 1 | sed -Ee 's!^[^[:digit:]]+([[:digit:]]+(\.[[:digit:]]+)?)%$!\1!'`
res=`echo "$perc >= 80.0" | bc`
test "$res" -eq 1 && exit 0
echo "Insufficient coverage: $perc" >&2
exit 1
Where:
The encantation involving sed extracts the coverage percentage (see here).
The next line asks the calculator to compare the percentage with your configured threshold.
Then the next line makes the script exit successfully if the test passed.
The rest of the script blows up if the coverage requirements were not met.
This script expects the bc tool is installed in that ubuntu-latest package (which I don't know).
If it isn't, the whole thing can be scripted in any language available in the image—such as Perl or Python.

Resources