How to bump version with GitHub Actions - bash

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

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

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

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.

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

How create a badge with test coverage (jacoco) on github (actions)

In spring project, I use jacoco plugin to measure test coverage.
I see the html report like below:
Now I would like to add a badge to github project with this percentage, something like this:
Any idea how can I combine jacoco with github actions?
You can use GitHub actions to generate a badge using GitHub Workflow (no need to other servers). You could write your own jobs/steps or use my just published action: https://github.com/marketplace/actions/badge-action .
First, you need to parse the coverage result file and extract the value (81 in your example). Here, I used parse-coverage-report as an example command (you'll need to create it by yourself). Finally, save this value as a GitHub workflow output:
on: [push]
jobs:
coverage:
runs-on: ubuntu-latest
name: Generate test coverage badge
steps:
- name: Generate a coverage value
id: coverage
# Generates a GitHub Workflow output named `lines`
run: |
COVERAGE="$( parse-coverage-report )"
echo "##[set-output name=lines;]${COVERAGE}%"
# Use the output from the `coverage` step
- name: Generate the badge SVG image
uses: emibcn/badge-action#v1
with:
label: 'Test coverage'
status: ${{ steps.coverage.outputs.lines }}
color: 'blue,555,daf'
path: badge.svg
This saves the badge as file badge.svg. Now, you decide wether to upload this badge to the same repository, to an S3 or whatever you prefer. Being that a coverage report, I suppose you'll like to upload that to same's repo 1) same branch it was extracted from or 2) dedicated branch badges:
1) Push to same branch it was extracted from
- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch
- name: Create badges dir if necessary
run: mkdir -p .github/badges
- name: Generate the badge SVG image
uses: emibcn/badge-action#v1
with:
label: 'Test coverage'
status: ${{ steps.coverage.outputs.lines }}
color: 'blue,555,daf'
path: .github/badges/badge.svg
- name: Commit badge
run: |
git config --local user.email "action#github.com"
git config --local user.name "GitHub Action"
git add .github/badges/badge.svg
git commit -m "Add/Update badge"
- name: Push badge commit
uses: ad-m/github-push-action#master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ steps.extract_branch.outputs.branch }}
The extract_branch step has been taken from https://stackoverflow.com/a/58035262/2928168 .
2) Push to dedicated branch badges
First, create and push the dedicated branch badges with (extracted from StackOverflow):
git checkout master
# Use a fresh start
git checkout --orphan badges
# Unstage all the files in your working tree.
git rm --cached $(git ls-files)
# Create a dedicated README file, so it's clear what's going on here
echo '# Badges branch' > README.md
git add README.md
git commit -m 'Add dedicated README'
git push origin badges
- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch
- uses: actions/checkout#v1
with:
ref: badges
path: badges
- name: Create badges dir if necessary
env:
BRANCH: ${{ steps.extract_branch.outputs.branch }}
run: mkdir -p badges/${BRANCH}
- name: Generate the badge SVG image
uses: emibcn/badge-action#v1
with:
label: 'Test coverage'
status: ${{ steps.coverage.outputs.lines }}
color: 'blue,555,daf'
path: badges/${{ steps.extract_branch.outputs.branch }}/badge.svg
- name: Commit badge
env:
BRANCH: ${{ steps.extract_branch.outputs.branch }}
run: |
pushd badges
git config --local user.email "action#github.com"
git config --local user.name "GitHub Action"
git add "${BRANCH}/badge.svg"
git commit -m "Add/Update badge"
popd
- name: Push badge commit
uses: ad-m/github-push-action#master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: badges
directory: badges
UPDATE
If you coverage report is a typical clover coverage.xml file, you can use this action to parse and output the coverage value. For example:
- name: Check test coverage
uses: johanvanhelden/gha-clover-test-coverage-check#v1
id: coverage
with:
percentage: "50"
filename: "coverage.xml"
# Use the output from the `coverage` step
- name: Generate the badge SVG image
uses: emibcn/badge-action#v1
id: badge
with:
label: 'Coverage'
status: ${{ steps.coverage.outputs.coverage }}
path: ./badges/test-coverage.svg
color: ${{ steps.coverage.outputs.coveragelines > 75 && 'green' || 'red' }}
UPDATE 2
You can make your badge change its background color depending on the coverage value, even using gradients:
# Use the output from the `coverage` step
- name: Generate the badge SVG image
uses: emibcn/badge-action#v1
id: badge
with:
label: 'Coverage'
status: ${{ steps.coverage.outputs.coverage }}
path: ./badges/test-coverage.svg
color: ${{
steps.coverage.outputs.coverage > 90 && 'green' ||
steps.coverage.outputs.coverage > 80 && 'yellow,green' ||
steps.coverage.outputs.coverage > 70 && 'yellow' ||
steps.coverage.outputs.coverage > 60 && 'orange,yellow' ||
steps.coverage.outputs.coverage > 50 && 'orange' ||
steps.coverage.outputs.coverage > 40 && 'red,orange' ||
steps.coverage.outputs.coverage > 30 && 'red,red,orange' ||
steps.coverage.outputs.coverage > 20 && 'red,red,red,orange' ||
'red' }}
UPDATE 3
Updated the 2 workflows:
Same branch: Save badges into .github/badges/
Dedicated branch: Use a sub directory in the workflow to manage the badges, so workflow environment remains usable for further steps (for example, saving some cache).
UPDATE 4: Working examples
You can see working examples in some repositories workflows (add yours by editing the answer or commenting on it):
https://github.com/emibcn/covid/blob/master/.github/workflows/node.js.yml
https://github.com/emibcn/Rac1.js/blob/master/.github/workflows/node.js.yml
You can use codecov seeing as they support every CI provider.
You will need two things:
An account from codecov and a token.
The codecov gh-action.
After you create your account and have access to a token, store the token as a secret in github actions. Call it CODECOV_TOKEN.
In your workflow, create a step that looks something like this and configure as needed:
- name: Upload coverage to Codecov
uses: codecov/codecov-action#v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
See example workflow
In your README, create the status badge using the format:
[![codecov](https://codecov.io/gh/<your-name>/<project-name>/branch/master/graph/badge.svg)](https://codecov.io/gh/<your-name>/<project-name>)
Sources: Integrating Codecov with a GitHub project
You'll need to publish your coverage stats to a service such as Coveralls as part of your continuous integration build. CI servers such as CircleCI and TravisCI have built in support for Github and Coveralls
Is your project open source? Coveralls, Travis and CircleCI are all free for open source. Once you've got github triggering CI and publishing to coveralls there's an image tag you can embed in your readme.md

How to push nuget package in GitHub actions

I am trying to use GitHub actions to generate a NuGet package from my project and push it to the (private) GitHub registry.
My script ([NAME] fields redacted):
name: Update NuGet
on: [push]
jobs:
build:
runs-on: ubuntu-latest
name: Update NuGet
steps:
- uses: actions/checkout#master
- uses: actions/setup-dotnet#v1
with:
dotnet-version: '2.2.105'
- name: Package Release
run: |
cd [SOLUTION_FOLDER]
dotnet pack -c Release -o out
- name: Publish Nuget to GitHub registry
run: dotnet nuget push ./[SOLUTION_FOLDER]/[PROJECT_FOLDER]/out/$(ls ./[SOLUTION_FOLDER]/[PROJECT_FOLDER]/out) -s https://nuget.pkg.github.com/[USERNAME]/index.json -k ${GITHUB_TOKEN}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Log output:
info : Pushing [PROJECT_FOLDER].3.4.23.nupkg to 'https://nuget.pkg.github.com/[USERNAME]'...
info : PUT https://nuget.pkg.github.com/[USERNAME]/
info : An error was encountered when fetching 'PUT https://nuget.pkg.github.com/[USERNAME]/'. The request will now be retried.
info : An error occurred while sending the request.
info : The server returned an invalid or unrecognized response.
info : PUT https://nuget.pkg.github.com/[USERNAME]/
info : An error was encountered when fetching 'PUT https://nuget.pkg.github.com/[USERNAME]/'. The request will now be retried.
info : An error occurred while sending the request.
info : The server returned an invalid or unrecognized response.
info : PUT https://nuget.pkg.github.com/[USERNAME]/
error: An error occurred while sending the request.
error: The server returned an invalid or unrecognized response.
##[error]Process completed with exit code 1.
This is the coresponding GitHub issue (with a workaround option): https://github.com/NuGet/Home/issues/8580
Second Update:
I got an answer in the GitHub issue from jcansdale that says (haven't tested this):
Support for the dotnet nuget push --api-key option has now been added to GitHub Packages. For some reason this works consistently, but using basic auth (password in nuget.config file) fails randomly!
Example:
- name: Publish Nuget to GitHub registry
run: dotnet nuget push ./<project>/out/*.nupkg -k ${GITHUB_TOKEN} -s https://nuget.pkg.github.com/<organization>/index.json --skip-duplicate --no-symbols
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Update:
Based on Dids answer on GitHub my configuration works now like this:
name: NuGet Generation
on:
push:
branches:
- master
pull_request:
types: [closed]
branches:
- master
jobs:
build:
runs-on: ubuntu-18.04
name: Update NuGet package
steps:
- name: Checkout repository
uses: actions/checkout#v1
- name: Setup .NET Core # Latest
uses: actions/setup-dotnet#v1
with:
source-url: https://nuget.pkg.github.com/<organization>/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Build solution and generate NuGet package
run: |
cd <project>
dotnet pack -c Release -o out
- name: Push generated package to GitHub registry
run: dotnet nuget push ./<project>/out/*.nupkg --skip-duplicate --no-symbols true
Note: At the time of writing I needed to use --no-symbols true instead of --no-symbols to prevent exceptions in the dotnet NuGet client.
Old answer:
I switched to the Windows image and got it to work based on the example of #anangaur. This is my final code:
name: NuGet Generation
on:
push:
branches:
- master
jobs:
build:
runs-on: windows-latest
name: Update NuGet
steps:
- name: Checkout repository
uses: actions/checkout#master
# latest image has .NET already installed!
# - name: Setup .NET environment
# uses: actions/setup-dotnet#v1
# with:
# dotnet-version: '2.2.105'
- name: Build solution and generate NuGet package
run: |
cd SOLUTION_FOLDER
dotnet pack -c Release -o out
- name: Install NuGet client
uses: warrenbuckley/Setup-Nuget#v1
- name: Add private GitHub registry to NuGet
run: nuget sources add -name "GPR" -Source https://nuget.pkg.github.com/ORGANIZATION_NAME/index.json -Username ORGANIZATION_NAME -Password ${{ secrets.GITHUB_TOKEN }}
- name: Push generated package to GitHub registry
run: nuget push .\SOLUTION_FOLDER\PROJECT_FOLDER\out\*.nupkg -Source "GPR" -SkipDuplicate
Here is a workaround that works on all platforms:
name: prerelease NuGet
on: [push]
jobs:
build:
runs-on: ubuntu-latest
# also works with windows-latest and macos-latest
steps:
- name: Checkout repository
uses: actions/checkout#v1
- name: Build with dotnet
run: dotnet build --configuration Release --version-suffix prerelease-$(date +%Y%m%d%H%M%S)
shell: bash
- name: Publish nuget
run: |
for f in ./[repository]/bin/Release/*.nupkg
do
curl -vX PUT -u "[user]:${{ secrets.GHPackagesToken }}" -F package=#$f https://nuget.pkg.github.com/[user]/
done
shell: bash
Notes:
this creates a datestamped prerelease build for every git push and uploads it to nuget
for the suffix to work, you need to set <VersionPrefix> instead of <Version> in your .csproj
if you don't want the prerelease suffix, remove the --version-suffix parameter
the shell is explicitly set as bash in order to allow compatibility with building on windows
you will need to replace [user] and [repository] above with your own specific values
you will need to create a personal access token with the permissions of write:packages
then create a GitHub Secret named GHPackagesToken and put the token created above in there
using GitHub Secrets eliminates the need for a separate file containing your token
this assumes you're have <GeneratePackageOnBuild>true</GeneratePackageOnBuild> in your .csproj
if you don't, then you will need an additional step running dotnet pack
make sure to specify <RepositoryUrl>...</RepositoryUrl> in your .csproj
for a working example if you can't get the above code working, see https://github.com/vslee/IEXSharp/blob/master/.github/workflows/dotnetcore.yml, which pushes to https://github.com/vslee/IEXSharp/packages (ignore all of my extraneous comments there)
I posted this bc I tried both the examples from jwillmer above, as well as #anangaur and #marza91 on the GH issue thread but neither worked for me (on any platform)
once GitHub fixes the issue of not being able to use the API key directly in the dotnet nuget push command (see initial post of GH issue), then we won't need this workaround anymore
You can use the dotnet nuget add source command:
- name: NuGet push
run: |
dotnet nuget add source https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json --name github --username ${{ github.repository_owner }} --password ${{ github.token }} --store-password-in-clear-text
dotnet nuget push **/*.nupkg --source github
The --store-password-in-clear-text option was required for me when running in a linux environment.
With this method, there's no need to modify the actions/setup-dotnet task.
Also, this method would allow you to push to multiple NuGet streams if needed.
Make sure you project file has the following
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Library</OutputType>
<PackageId>Example.PackageName</PackageId>
<Version>1.0.0</Version>
<Authors>Author Engineering</Authors>
<Company>Company Inc</Company>
<PackageDescription>This package for ...!</PackageDescription>
<RepositoryUrl>
https://github.com/YOUR_ACCOUNT/Example.PackageName</RepositoryUrl>
</PropertyGroup>
This should be your main.yaml for building, packaging, publishing, and versioning:
name: Continuous Integration
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Setup Dotnet Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: 3.1.100
source-url: https://nuget.pkg.github.com/YOUR_ACCOUNT/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Setup Nuget Config
run: sed 's/GITHUB_TOKEN/${{ secrets.GITHUB_TOKEN }}/g' .nuget.config > nuget.config
- name: Build
run: dotnet build --configuration Release
- name: Version and Tag
id: bump_version
uses: mathieudutour/github-tag-action#v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Prep Version String
run: echo ::set-env name=VERSION_NUMBER::$(echo ${{ steps.bump_version.outputs.new_tag }} | sed 's/[v]//g')
- name: Define Package Name
run: echo ::set-env name=PACKAGE_NAME::$"Example.PackageName/bin/Release/Example.PackageName.${{ env.VERSION_NUMBER }}.nupkg"
- name: Set Nuget Package Version
uses: roryprimrose/set-vs-sdk-project-version#v1
with:
version: ${{ env.VERSION_NUMBER }}
- name: Pack
run: dotnet pack --configuration Release Example.PackageName
- name: Publish Package
run: dotnet nuget push Example.PackageName/bin/Release/*.nupkg --source https://nuget.pkg.github.com/YOUR_ACCOUNT/index.json
- name: Create Release
uses: actions/create-release#v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.bump_version.outputs.new_tag }}
release_name: Release ${{ github.ref }}
The other answers were so long, I don't know why. This is what I do:
For NuGet.org:
- name: Push Package to NuGet.org
run: dotnet nuget push *.nupkg -k ${{ secrets.NUGET_ORG_API_KEY }} -s https://api.nuget.org/v3/index.json
For GitHub.com:
- name: Push Package to GitHub.com
run: dotnet nuget push *.nupkg -k ${{ secrets.GITHUB_TOKEN }} -s https://nuget.pkg.github.com/USERNAME/index.json
My working solution:
replace 'usernamecompanyname' with a valid value for your repo
I kept the build and pack separated to allow for easier debugging if something goes wrong
you can set ACTIONS_RUNNER_DEBUG variable in your github secrets to true to allow for more detailed debuging
Change dotnet-version to your desired dotnet-sdk version
No need to specify GITHUB_TOKEN in your github repo secrests, this token is present by default
build_and_publish_nuget.yml:
name: Build and publish package
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches: [ master ]
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout#master
- name: Setup .NET environment
uses: actions/setup-dotnet#v1
with:
dotnet-version: '3.1.102'
source-url: https://nuget.pkg.github.com/usernamecompanyname/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Build project
run: dotnet build -c Release
- name: Generate a NuGet package
run: dotnet pack --no-build -c Release -o .
- name: Push to GitHub package registry
run: dotnet nuget push *.nupkg
GitHub has been having intermittent issues with publish NuGet Packages to GitHub Packages. I reached out to support and they gave me two options.
Option 1: CURL
curl -vX PUT -u "<username>:<TOKEN>" -F package=#PATH-TO-PKG-FILE.nupkg https://nuget.pkg.github.com/<OWNER>/
Option 2: DOTNET GPR TOOL
https://github.com/jcansdale/gpr
dotnet tool install gpr -g
gpr push PATH-TO-PKG-FILE.nupkg -k <TOKEN>
I went with Option 2 in my GitHub Action Workflow:
$file = Get-ChildItem -Path <where I output my nupkg file to> -Recurse -Filter *.nupkg | Select -First 1
gpr push $file.FullName -k ${{secrets.GITHUB_TOKEN}}

Resources