I am modifying a workflow file so that a job doesn't run when a pull request originates from another repo.
So, I am looking for something like this:
build_and_deploy_job:
if: github.repository == github.pullrequest.repo
github.pullrequest.repo is just something I made up, but the idea would be that it would return the (full) name of the repo where the pull request came from.
I've tried outputting the environment variables to see if they are somehow different if a PR comes from a different branch in the same repo, or from a different repo. Nothing stood out to me as being useful.
Is something like that possible?
Background: I am trying to avoid a build job failure because it can't access a repo secret during a pull_request event when the pull request comes from another repo.
Use the following to check if the PR is coming from the same repo. This only works when the workflow is triggered by a pull request.
on: [pull_request]
jobs:
build_and_deploy_job:
if: github.event.pull_request.head.repo.name == github.repository
Bonus: Use this to check if the repo is a fork.
on: [pull_request]
jobs:
build_and_deploy_job:
if: github.event.pull_request.head.repo.fork == true
Source: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#webhook-payload-object-34
Related
When running a github action manually, I can choose which branch to run it against. This seems like a bad idea for some actions. Especially actions along the lines of DeployToProduction - where I only ever want that to be run against the main branch.
Is there any way to restrict it to only run against main - while still making the action manually-triggered?
An example of a manually-triggered action that I have might look something like this:
---
name: DeployToStaging
on:
workflow_dispatch:
jobs:
...
I had the same issue and I solved it with a condition:
if: github.ref == 'refs/heads/master'
steps:
...
in this way the user can see that the action is skipped when selecting a different branch.
https://github.community/t/limit-branch-for-workflow-dispatch/122099/4
I'm quite a rookie with GitHub Actions so this might be a stupid question: Is there a way to link pull requests with issues (in the UI linked PRs are shown under Development) in Github Actions using Github CLI or octokit/rest.js via actions/github-script?
enter image description here
Background: there is a workflow that creates pull requests. That works fine, only thing missing is the link between issues and corresponding pull requests. I would prefer not to use keywords nor other custom actions from the marketplace.
I've searched in the octokit/rest.js documentation https://octokit.github.io/rest.js/v19 under Issues and Pulls as well as in the GitHub cli documentation https://cli.github.com/manual/gh but couldn't find a solution.
I would like to have a solution either using GitHub Script https://github.com/marketplace/actions/github-script
or the command line.
You can use the Add an issue link Action for that.
This Action allows linking issues to Pull Requests. For example:
name: 'Issue Links'
on:
pull_request:
types: [opened]
jobs:
issue-links:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: tkt-actions/add-issue-links#v1.8.1
with:
repo-token: '${{ secrets.GITHUB_TOKEN }}' # required
branch-prefix: 'issue-' # required
This workflow will trigger on Pull Request open and link related issues mentioned in the PR body.
Make sure to set the corresponding permission for the job:
permissions:
pull-requests: write
For more about these permissions visit the Permissions for the GITHUB_TOKEN.
I can't for the life of me figure out why my GitHub Action isn't triggering on push. For context: I have a data file that is updated daily and pushed to the test branch with a timestamp commit message. I am trying to use this timestamp in a Dynamic Badge for my README. Everything works fine when the workflow is run manually (except, of course, I don't get the event data I am hoping to obtain when the action runs on the trigger.)
on:
push:
branches:
- test
paths:
- 'data/Sales.csv'
env:
BADGE_MESSAGE: ${{ github.event.commits[0].message }}
jobs:
create-badge-test:
runs-on: ubuntu-latest
steps:
- name: Create Dynamic Badge
uses: schneegans/dynamic-badges-action#v1.1.0
with:
auth: ${{ secrets.GIST_PAT }}
gistID: 0123456789 #Not actual gist ID
filename: test.json
label: Last Refresh
message: $BADGE_MESSAGE
color: orange
And yes, I've tried putting the branch name in quotes and updating the paths: to - '**.csv' and the action still does not trigger.
There must be something else wrong - all you have here in this workflows is just fine.
You can see it working here:
https://github.com/grzegorzkrukowski/stackoverflow_tests/actions/runs/1860382530
For this commit:
https://github.com/grzegorzkrukowski/stackoverflow_tests/commit/f3f8dd20fa780746441c7b6623b6a2a9929aa70d
It's exact copy of workflow from your question.
I would expect you are pushing a file with different name - keep in mind some systems will be case-sensitive - so pushing data/sales.csv won't work properly.
Another idea is that you pushing it to wrong branch or you have wrong path to the file.
You have to push to test branch and with data/Sales.csv - it only triggers workflow if both are true.
Short answers is - workflow is fine as it is - no brackets needed.
I could help more seeing the repository with this workflow and exact commits being done.
The action is not running because you also need to satisfy the paths condition as explained on GitHub docs
Note: If you use both the branches filter and the paths filter, the workflow will only run when both filters are satisfied.
If you want the action to run when you push to test you have to remove the paths condition
on:
push:
branches:
- 'test'
When running a github action manually, I can choose which branch to run it against. This seems like a bad idea for some actions. Especially actions along the lines of DeployToProduction - where I only ever want that to be run against the main branch.
Is there any way to restrict it to only run against main - while still making the action manually-triggered?
An example of a manually-triggered action that I have might look something like this:
---
name: DeployToStaging
on:
workflow_dispatch:
jobs:
...
I had the same issue and I solved it with a condition:
if: github.ref == 'refs/heads/master'
steps:
...
in this way the user can see that the action is skipped when selecting a different branch.
https://github.community/t/limit-branch-for-workflow-dispatch/122099/4
My workflow file looks like this:
on:
push:
branches:
- master
paths:
- "src/*"
pull_request:
branches:
- master
paths:
- "src/*"
At some point i added the paths: "src/*" and it simply won't trigger (anymore)?
I tried different things such as the filter on branches/paths, but it's like github actions cannot do PR merge checks? I've set them up with other CI platforms just fine, but i can't get it working with github actions. I have read in some article that PR checks will not work if there is a merge conflict, but that is not the case here either
It might be because your PR is still opened.
As your workflow is implemented:
The PR event is only triggered on the initial PR.
The workflow is not triggered on subsequent pushes events to the branch that has PR (and is not master).