github `registry_package` event doesn’t trigger - github

I managed to create two actions on 1 private repository:
The first one builds the image and push the docker image to GitHub
Container Registry
The second one needs to be triggered when newer
image is published to the GitHub container registry and deploy the
image
The issue is that the second one it doesn't get triggered and doesn't run. I use GitHub Repo Token, and I found this that says triggering new workflows should be done using a personal access token. Is this the real issue or there is some workaround? Personally I don't want to put my github token there.
As reference here is the yml code for the fist github action:
name: Build Docker Image
on:
push:
branches:
- feature/ver-64/service-template
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout#v2
-
name: Docker meta
id: meta
uses: docker/metadata-action#v3
with:
images: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=sha
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action#v1
-
name: Login to Github Container Repository
if: github.event_name != 'pull_request'
uses: docker/login-action#v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Build and push
uses: docker/build-push-action#v2
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
And this is the yml for the second one that needs to be trigered once the first one publish new image to the registry:
name: Deploy to Azure
on:
registry_package:
types: [ published, updated ]
jobs:
debug:
runs-on: ubuntu-latest
steps:
- uses: hmarr/debug-action#v2

GitHub actions prevents triggering more actions. Sort of to protect against infinite loops. Hence why the token used by GitHub Actions has a special flag on it which causes the 2nd workflow not to trigger.
You have a few options:
Use a PAT to push to GitHub Container Registry. (as per the docs)
Have a 2nd stage that depends on the first one in your existing workflow to perform the deployment.
A variation on 2, use a template to extract the deploy logic to a single template, use the same template action in both the workflow that pushes the image as well as the workflow that triggers when an image is pushed

Related

Github Actions workflow not triggered although schedule should trigger the job

I'm trying to auto-assign issues and PRs in Github from a Github Actions workflow. The respective steps work fine when an issue / a PR is opened. So this trigger is fine.
Recently I added Dependabot to my repo. Since Dependabot cannot access my secrets I cannot assign any issue in a Dependabot-triggeres pipeline. So I just thought I run this pipeline with a scheduler one per day to "clean up" every issue and PR which is unassigned. But this schedule config does not trigger the pipeline. It simply does nothing, not even show as a pipeline run which does nothing (with all jobs skipped). Seems like the trigger is completely ignored.
This is my workflow file.
---
name: "Organize: Assign Issues + Pull Requests"
on:
issues:
types:
- opened
pull_request:
types:
- opened
schedule:
- cron: '0 9 * * *' # https://crontab.guru/#0_11_*_*_*
permissions:
contents: read
issues: write
pull-requests: write
jobs:
add-to-project:
name: Add to project
runs-on: ubuntu-latest
steps:
- name: Add to project (issues and PRs)
uses: actions/add-to-project#main
with:
project-url: https://github.com/users/sebastian-sommerfeld-io/projects/1
github-token: ${{ secrets.GH_TOKEN_REPO_AND_PROJECT }}
assign-to-user:
name: Assign to user
runs-on: ubuntu-latest
steps:
- name: Assign issue to user when moved into column
uses: pozil/auto-assign-issue#v1
# https://github.com/marketplace/actions/auto-assign-issue
with:
assignees: ${{ github.actor }}
numOfAssignee: 1
allowSelfAssign: true
abortIfPreviousAssignees: true
new-pull-request-chat-message:
runs-on: ubuntu-latest
needs: ['add-to-project', 'assign-to-user']
if: github.event_name == 'pull_request'
steps:
- name: Send message to Google Chat
uses: Co-qn/google-chat-notification#releases/v1
with:
name: New Pull Request "${{ github.event.pull_request.title }}" (raised by ${{ github.actor }})
url: ${{ secrets.GOOGLE_CHAT_WEBHOOK }}
status: ${{ job.status }}
on-failure:
runs-on: ubuntu-latest
needs: ['add-to-project', 'assign-to-user', 'new-pull-request-chat-message']
if: failure()
steps:
- name: Send Pipeline Status to Google Chat
if: always()
uses: Co-qn/google-chat-notification#releases/v1
with:
name: ${{ github.workflow }}
url: ${{ secrets.GOOGLE_CHAT_WEBHOOK }}
status: failure
What bugs me is that the scheduler setting is copied from another workflow where it works just fine. So I cannot think of a reason why this pipeline is not triggered at 09:00 in the morning.
Found a way :-)
I use the Github CLI to get all PRs with a certain label and assign a user. This is a new dedicated pipeline.
---
name: "Organize: Dependabot Pull Requests"
on:
schedule:
- cron: '30 * * * *' # https://crontab.guru
permissions:
contents: read
issues: write
pull-requests: write
jobs:
assign-user:
name: Aassign PRs with label 'dependencies'
runs-on: ubuntu-latest
steps:
- name: Get PR and assign user (filtered by github cli)
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN_REPO_AND_PROJECT }}
label: dependencies
assignee: sebastian-sommerfeld-io
run: |
OUTPUT=""
pr_ids="$(gh pr list --repo "$GITHUB_REPOSITORY" --label "$label" --json number --jq '.[].number')"
for id in $pr_ids; do
gh pr edit "$id" --repo "$GITHUB_REPOSITORY" --add-assignee "$assignee"
done
Then I updated the triggers of my original pipeline to
on:
issues:
types:
- opened
pull_request:
types:
- opened
- assigned

How to associate status of a Github action workflow with a commit or PR

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.

GitHub Actions re-run and who is the author

I need an additional field with information on who is the author of the trigger re-run button in GitHub actions.Any help + bless you 🙏
Below is an example.
name: CI
#Only for master
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
check_run:
types: [rerequested]
#Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
notify:
runs-on: ubuntu-latest
steps:
# Ms Teams Deploy Cards step linked to repository in a dedicated created Ms teams channel
- name: teams notification
uses: patrickpaulin/ms-teams-deploy-card#master
if: always()
with:
github-token: ${{ github.token }}
webhook-uri: ${{ secrets.MS_TEAMS_WEBHOOK_URI }}
and this part of the code
check_run:
types: [requested]
This step is to identify re-run action but in this example just duplicate MS Deployment Card and provide all information with was when the author did push or pull request.
P.S
Friendly advice: Don't use toko-bifrost repo because do not work
correct, explaining in the link below.
https://github.com/toko-bifrost/ms-teams-deploy-card/issues/44
SOLVED
- name: teams notification
uses: patrickpaulin/ms-teams-deploy-card#master
if: always()
with:
github-token: ${{ github.token }}
webhook-uri: ${{ secrets.MS_TEAMS_WEBHOOK_URI }}
custom-facts: |
- name: GiitHub Action
value: ${{ github.actor }}

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

GitHub action for issue_comment doesn't shown in checks for PR

I created a GitHub action on:issue_comment, I can see the flow running only in the action tab, but not in the PR, where I made the comment.
I want to comment in a PR and trigger a check on that PR (not on master)
here is my workflow:
name: issue-comment-CI-test
on:
issue_comment:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Run a one-line script
run: echo Hello, world!
- name: Run a multi-line script
run: echo ${{ github.event.comment.body }}
currently, I'm just printing the comment body, But I plan to check the body, and if it is equal to "run integration tests" then I'll run my integration tests (maven)
Basically you need to checkout to the PR origin. For that, first make a API request to the pr url and fetch all ref.
Then do the checkout on the fetched repo and branch.
Step1
- name: Github API Request
id: request
uses: octokit/request-action#v2.0.0
with:
route: ${{ github.event.issue.pull_request.url }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Step 2
- name: Checkout PR Branch
uses: actions/checkout#v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
repository: ${{ fromJson(steps.request.outputs.data).head.repo.full_name }}
ref: ${{ steps.pr_data.outputs.branch }}
You can follow the following example, specially the GitHub API Request part.
I've also implemented it in one of our workflows, you can take reference from that as well.
https://github.com/adrianjost/workflow-trigger-comment-example/blob/master/.github/workflows/demo.yml
https://github.com/TeamAmaze/AmazeFileManager/blob/master/.github/workflows/android-debug-artifact-ondemand.yml
You need to checkout the Pull Request. You can get the PR ID by using {{ GITHUB_REF }}.
You can checkout the PR with:
git fetch origin pull/{{ GITHUB_REF }}/head:PR
git checkout PR
See https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/checking-out-pull-requests-locally, https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#onevent_nametypes, https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables
and https://developer.github.com/v3/pulls/ for reference.
Used built-in gh command to fetch the branch instead of using actions/checkout.
name: issue-comment-CI-test
on:
issue_comment:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- run: |
gh pr checkout ${{ github.event.issue.number }}
# do your job...
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required for gh