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
Related
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 }}
I have a project where I have two GitHub actions yml file where the first file is called build.yml and it contains instructions to compile, build and test the project. It is as simple as this:
name: build my-project
on:
push:
paths-ignore:
- 'images/**'
- README.md
branches:
- master
pull_request:
branches:
- master
release:
types: [ created ]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: cache ivy2
uses: actions/cache#v1
with:
path: ~/.ivy2/cache
key: ${{ runner.os }}-sbt-ivy-cache-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }}
- name: sbt Test
run: sbt clean test
I now have another yml file that contains the instructions to do a release based on annotated tags. It is like this:
name: release my-project
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v[0-9]+.[0-9]+.[0-9]+-[a-zA-Z]*'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
build:
uses: ./.github/workflows/build.yml
publish:
runs-on: ubuntu-latest
needs: test # See build.yml file where the test job is defined
# If there is a tag and if that tag comes from master branch
if: startsWith(github.ref, 'refs/tags/v')
steps:
- name: checkout
uses: actions/checkout#v3
- name: capture changelog
id: changelog
uses: metcalfc/changelog-generator#v4.0.1
with:
myToken: ${{ secrets.GITHUB_TOKEN }}
- name: sbt ci-publish-github
run: sbt publish
- name: ci-release-github
id: create-release
uses: actions/create-release#latest
with:
allowUpdates: true
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
body: |
## What's Changed
${{ steps.changelog.outputs.changelog }}
draft: false
prerelease: false
I just created an annotated tag which then resulted in an error like this:
Invalid workflow file: .github/workflows/publish.yml#L14
error parsing called workflow "./.github/workflows/build.yml": workflow is not reusable as it is missing a `on.workflow_call` trigger
So basically what I want is, when I push an annotated tag, I want to first run the test job from build.yml and then once that succeeds, I would like to run the publish job. Any suggestions on how to get this straight?
So basically what I want is, when I push an annotated tag, I want to first run the test job from build.yml and then once that succeeds, I would like to run the publish job. Any suggestions on how to get this straight?
You almost got it right with your implementation. You just need a few modifications:
The build job needs to depends on the publish job:
name: release my-project
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v[0-9]+.[0-9]+.[0-9]+-[a-zA-Z]*'
jobs:
publish:
[ ... ]
build:
needs:
- publish
uses: ./.github/workflows/build.yml
The build needs the workflow_call trigger (as stated by the error message - Reference):
on:
workflow_call:
push:
[ ... ]
Note: You could even share the tag value from the previous workflow, sending it as input to the second one by using:
on:
workflow_call:
inputs:
tag:
required: true
type: string
Calling the reusable workflow that way from the main workflow:
build:
needs:
- publish
uses: ./.github/workflows/build.yml
with:
tag: 'MY TAG'
I was able to fix it by adding the following in my publish.yml:
jobs:
tests:
uses: ./.github/workflows/build.yml
publish:
runs-on: ubuntu-latest
needs: [tests] # See build.yml file where the test job is defined
In my build.yml, I had to add the following:
on:
push:
paths-ignore:
- 'images/**'
- README.md
branches:
- master
pull_request:
branches:
- master
release:
types: [ created ]
workflow_call:
Notice that workflow_call: entry that needs to be added explicitly.
I have a Github action workflow which is set to trigger on issue_comment, for example the following
name: Testing actions
on:
issue_comment:
types: [created, edited]
jobs:
testing-actions:
if: github.event.issue.pull_request && contains(github.event.comment.body, '#test')
name: Just testing
runs-on: ubuntu-latest
steps:
- uses: xt0rted/pull-request-comment-branch#v1
id: comment-branch
- uses: actions/checkout#v3
with:
ref: ${{ steps.comment-branch.outputs.head_ref }}
- name: Print stuff
run: echo "Hello dudes!"
The workflow is working, it is getting triggered when I comment "#test" on a PR. But I have to check it from the actions tab, the workflow doesn't get associated with any commit or PR, so it doesn't get shown in the checks tab in the PR, or the commits don't show any commit status.
How do I get the workflow status and details in the PR page, so that I don't have to go to actions tab manually to see the results?
I solved this by manually setting the commit status using #myrotvorets/set-commit-status-action. The workflow file ended up being like the following
name: Testing actions
on:
issue_comment:
types: [created, edited]
jobs:
testing-actions:
if: github.event.issue.pull_request && contains(github.event.comment.body, '#test')
name: Just testing
runs-on: ubuntu-latest
steps:
- name: Get PR details
uses: xt0rted/pull-request-comment-branch#v1
id: comment-branch
- name: Set commit status as pending
uses: myrotvorets/set-commit-status-action#master
with:
sha: ${{ steps.comment-branch.outputs.head_sha }}
token: ${{ secrets.GITHUB_TOKEN }}
status: pending
- uses: actions/checkout#v3
with:
ref: ${{ steps.comment-branch.outputs.head_ref }}
- name: Print stuff
run: echo "Hello dudes!"
- name: Set final commit status
uses: myrotvorets/set-commit-status-action#master
if: always()
with:
sha: ${{ steps.comment-branch.outputs.head_sha }}
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
This works pretty well, the workflow runs get associated with the PR's head commit, and thus it shows up on the PR page as a check too. Thanks #rethab for the alpha.
Similar tho this question How to run Github Actions with the 'deployment_status' kitty and only on the QAS branch?
I want to execute a workflow only on a specific branch but combined with the deployment_status trigger.
So I want to execute my end to end test only if the deployment is done and we are on the develop branch.
The problem here is: On the trigger deployment_status the github.ref variable is not set. It is also written in docs. So I can not do a manual check if I am on the right branch.
name: E2E
on:
deployment_status:
# does not work because it is ignored on deployment_status
branches:
- develop
- stage
- master
- main
jobs:
chrome:
if: github.event.deployment_status.state == 'success'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Run Cypress
uses: cypress-io/github-action#v2
with:
browser: chrome
cache-key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
env:
CYPRESS_BASE_URL: ${{ github.event.deployment_status.target_url }}
On the other way: If i use the deployment_status trigger I can not use branches or branches-ignore in combination. This is not working at all.
name: E2E
on:
deployment_status:
jobs:
chrome:
# does not work because github.ref is not defined
if: github.event.deployment_status.state == 'success' && github.ref == 'refs/heads/develop' /
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Run Cypress
uses: cypress-io/github-action#v2
with:
browser: chrome
cache-key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
env:
CYPRESS_BASE_URL: ${{ github.event.deployment_status.target_url }}
Any ideas how to solve this?
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