How to versioning releases in Github Actions - github

Is there a way to publish releases on GitHub using Actions with custom version numbers? Currently, I'm using github.run_number provided by GitHub Context and as mentioned in the docs:
github.run_number (string) - A unique number for each run
of a particular workflow in a repository. This number begins
at 1 for the workflow's first run, and increments with each new run.
Not every run of my workflow creates a release (e.g. when a workflow fails), resulting in inconsistent version numbers. I've created a demo repository and as you can see, the release numbers are ...38,39,40,47,49. I didn't find any solution for this in GitHub Actions Docs.
I want to have consistent incrementally growing version numbers or even a v.x.x structure if it's possible.
My complete workflow can be found here, my release-project job is:
...previous jobs: build, test, deploy...
release-project:
name: Release project
needs: deploy-project
...
- name: Create release
id: create_release_id
uses: actions/create-release#v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.run_number }}
release_name: Release ${{ github.run_number }}
- name: Upload release asset
id: upload-release-asset
uses: actions/upload-release-asset#v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release_id.outputs.upload_url }}
asset_path: ./project.zip
asset_name: project-v${{ github.run_number }}.zip
asset_content_type: application/zip

I would suggest not relying on run_number and using the latest tag from the repo to generate the next version based on it. For example, you can use the Get Latest Tag, Next SemVers, and Next Monotonic Release version GH Actions.
Semantic versioning workflow:
...
jobs:
test-next-release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v3
with:
fetch-depth: 0 # required for github-action-get-previous-tag
- name: Get previous tag
id: previoustag
uses: 'WyriHaximus/github-action-get-previous-tag#v1'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get next minor version
id: semver
uses: 'WyriHaximus/github-action-next-semvers#v1'
with:
version: ${{ steps.previoustag.outputs.tag }}
- name: Create release
id: create_release_id
uses: actions/create-release#v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.semver.outputs.patch }}
release_name: Release ${{ steps.semver.outputs.patch }}
Consecutive numbers versioning workflow:
...
jobs:
test-next-release-custom:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v3
with:
fetch-depth: 0 # required for github-action-get-previous-tag
- name: Get Previous tag
id: previoustag
uses: 'WyriHaximus/github-action-get-previous-tag#v1'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get next version
id: next
uses: 'WyriHaximus/github-action-next-release-version#1.0.0'
with:
version: ${{ steps.previoustag.outputs.tag }}
- name: Create release
id: create_release_id
uses: actions/create-release#v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.next.outputs.version }}
release_name: Release ${{ steps.next.outputs.version }}

Related

github composite action checkout detach

I have created a composite action see the link for running a Sonarcloud analysis for dotnet projects.
name: Sonarcloud
description: Sonarcloud
inputs:
sonar_project_key:
required: true
type: string
github_token:
required: true
type: string
sonar_token:
required: true
type: string
runs:
using: "composite"
steps:
- name: Set up JDK 11
uses: actions/setup-java#v1
with:
java-version: 1.11
- name: Install dotnet SonarCloud scanner
shell: powershell
run: |
dotnet tool install --global dotnet-sonarscanner
- name: Build and analyze
shell: powershell
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
SONAR_TOKEN: ${{ inputs.sonar_token }}
run: |
dotnet sonarscanner begin /k:"${{ inputs.sonar_project_key }}" /o:"my-org" /d:sonar.login="${{ inputs.sonar_token }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="**/coverage.opencover.xml"
dotnet build --configuration Release
dotnet test --no-restore --configuration Release --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
dotnet sonarscanner end /d:sonar.login="${{ inputs.sonar_token }}"
Then follow the link I have to create a release with the tag "v1" something like: "my-org/sonarcloud#v1" and then used it in another repository as follows:
name: Sonarcloud
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
workflow_call:
secrets:
SONAR_TOKEN:
required: true
workflow_dispatch: ~
jobs:
build:
name: Build
runs-on: windows-latest
steps:
- uses: actions/checkout#v3
with:
submodules: 'true'
fetch-depth: 0
- uses: microsoft/variable-substitution#v1
with:
files: 'tests/IntegrationTests/tests.settings.json'
env:
ConnectionString: ${{ secrets.CONNECTIONSTRING }} # please note that in this repo is connection string but it could vary from repo to repo,
# maybe in another repo I need to substitute a Sas token for example
# so I cannot move the variable substitution to the composite action
- uses: actions/checkout#v3
- id: sonarcloud
uses: my-org/sonarcloud#v1
with:
sonar_project_key: 'my-project'
sonar_token: ${{ secrets.SONAR_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
Check that I need to modify the "tests.settings.json" file in order to provide a valid connection string for the Tests to work.
Now the problem. The transformation is being conducted properly but here:
- uses: actions/checkout#v3
- id: sonarcloud
uses: my-org/sonarcloud#v1
git realize that "test.settings.json" has been modified and restore it to original version (that not contain the connection string) and the test fail.
here are the logs of the workflow:
2022-04-29T10:56:04.3078283Z [command]"C:\Program Files\Git\bin\git.exe" checkout --detach
2022-04-29T10:56:04.8735279Z M tests/IntegrationTests/tests.settings.json
2022-04-29T10:56:04.8736695Z HEAD is now at 5e6cf4b fix
So how can I avoid this behavior in the second checkout that is needed in order to get the composite action?.
thanks
I have found the problem! The second
uses: actions/checkout#v3
was not needed. To fix it just removed.
Replace this:
- uses: actions/checkout#v3
- id: sonarcloud
uses: my-org/sonarcloud#v1
with:
sonar_project_key: 'my-project'
sonar_token: ${{ secrets.SONAR_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
By:
- id: sonarcloud
uses: my-org/sonarcloud#v1
with:
sonar_project_key: 'my-project'
sonar_token: ${{ secrets.SONAR_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}

Unable to get release title in github actions

I've added github action that sends a message on our slack channel on every release.
I've managed to get repo name and tag from github context, but I'm also trying and failing to get release title and release notes in that message.
I've tried these combinations:
${{ github.event.payload.release.name || github.event.payload.release || github.event.payload }}
Does anyone know how to resolve this problem?
name: Release
on:
push:
tags:
- 'v*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v1
- name: Sbt
uses: olafurpg/setup-scala#v10
- name: Set library version
run: ./sbt dynver
- name: Publish stable version
run: ./sbt publish
env:
JFROG_PASSWORD: ${{ secrets.JFROG_PASSWORD }}
- name: Post to a Slack channel
id: slack
uses: slackapi/slack-github-action#v1.17.0
with:
channel-id: 'releases'
slack-message: "Release result: ${{ job.status }}\n${{ github.repository }}: ${{ github.ref_name }}\nRelease info: ${{ github.event.payload.release.name }}, ${{ github.event.payload.release }} ${{ github.event.payload }}"
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
Instead of triggering on the tag, trigger on the release creation. That way the release information will be present.
on:
release:
types: [published]
The tag will be under github.event.release.tag_name, the release under github.event.release.name.
Tags can be created independently of releases, that's why.
See:
https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#release
https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#release

A workflow is not triggering a second workflow

The workflow in file inrisk.packages.ci.yml generates a tag and a realise of the code when a push is done in the develop branch. The below works as expected.
name: Code Int
on:
push:
paths:
- 'infra/**'
jobs:
ci:
runs-on: ubuntu-latest
steps:
# Checks-out to $GITHUB_WORKSPACE
- uses: actions/checkout#v2
- name: Basic Checks
run: |
whoami
ls -lah
pwd
- uses: actions/setup-node#v1
# Create a new release when on develop which triggers the deployment
- name: Bump version and push tag
if: github.ref == 'refs/heads/develop'
uses: mathieudutour/github-tag-action#v4.5
id: tag_version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create Release
if: github.ref == 'refs/heads/develop'
id: create_release
uses: actions/create-release#v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.tag_version.outputs.new_tag }}
release_name: Release ${{ steps.tag_version.outputs.new_tag }}
draft: false
prerelease: false
The below workflow in file inrisk.packages.cd.yml and is suppose to be triggered when ever a tag/realise is created/published.
name: Code Deploy
on:
push:
tags:
- 'v*'
release:
types:
- published
- created
- released
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# Checks-out to $GITHUB_WORKSPACE
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
- name: Install Yarn
run: npm install -g yarn
- uses: chrislennon/action-aws-cli#v1.1
- name: Install, Build and Deploy
run: |
whoami
ls -lah
pwd
The second workflow Code Deploy dose not get trigger after Code Int publishes/created a tag/realise
However when I manually create a realise/tag the second workflow Code Deploy get triggered
This seems to be by design as stated here .This is to stop recursive workflow runs.
I used this article to get around the problem

Github Action: build from cache in auto-label-merge-conflicts?

In the following workflow, I want to add cache functionality so that, every time it will build from scratch. this is the workflow:
# This workflow will do
# a clean install of node deps
# build the source code
# run test across different versions of node
name: Conflict Check
on:
push:
branches:
- staging
pull_request:
branches:
- staging
jobs:
triage:
runs-on: ubuntu-latest
steps:
- uses: mschilde/auto-label-merge-conflicts#master
with:
CONFLICT_LABEL_NAME: 'has conflicts'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
How can I achieve this?
You can use actions/cache action for purposes of caching in Github Actions.
jobs:
triage:
runs-on: ubuntu-latest
steps:
- name: Cache build files
uses: actions/cache#v2
with:
path: ${{ PATH_TO_CACHE }}
key:${{ runner.os }}-${{ hashFiles(<glob_pattern_for_files>) }}
- uses: mschilde/auto-label-merge-conflicts#master
with:
CONFLICT_LABEL_NAME: 'has conflicts'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
The example above assumes you want to cache your files between runs on different refs but your actual key declaration would depend on what you are trying to do.
For example if you are trying to cache between jobs or workflow runs on same ref:
key: ${{ runner.os }}-${{ github.sha }}

Publishing NPM module to github packages registry from Github Actions?

My YML so far, kept adding bits based on other stackoverflow threads + docs:
name: Node install, build and test
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
steps:
- uses: actions/checkout#v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node-version }}
- name: Create NPMRC
run: echo "//registry.npmjs.org/:_authToken=${{ secrets.GITHUB_TOKEN }}" > ~/.npmrc
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN}}
- name: Publish to Github Packages
run: |
npm config set _auth $NODE_AUTH_TOKEN
npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN}}
In my package.json I have:
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
},
And with the above config I keep getting
E400 Bad Request
Your request could not be authenticated by the Github Pacakges service. Please ensure your access token is valid and has the appropriate scopes configured.
You are writing the wrong content to the ~/.npmrc file.
It should be //npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }} but you are doing //registry.npmjs.org/:_authToken=${{ secrets.GITHUB_TOKEN }}