We have workflow in GitHub Actions which has both types that trigger on event, e.g.pull-request, and on schedule.
Most of the steps are the same except for running test step. What we would like is:
On pull-request without specific label provided: execute smoke test
On pull-request with specific label OR on schedule: execute full test
The check for label can be done with contains(github.event.pull_request.labels.*.name, 'full-tests').
The question is how to check when the it is run on schedule? From my understanding of the documentation, when on schedule there is no payload for github.event for schedule. However checking github.event == null doesn't seems to work.
Is there a specific way to check whether it is running on schedule?
What you want to use in that case is the github.event_name variable, which represents the name of the event that triggered the workflow run in the github context.
In your case, to run a job or a step if the workflow run triggered on a scheduled event, you will need to use:
if: ${{ github.event_name == 'schedule' }}
Reference
Related
I have a workflow with 2 jobs, One job should run Terraform Plan when a pull request is created without a label and the other job should run Terraform Apply when a pull request is merged and does not have a label. currently my workflow is running on both occasions. The issue I'm having is that Terraform Plan is also running when the pull request is closed without merge. I only want Terraform Plan to run when a pull request is created without a label.
Below is a snippet of the triggers if I am missing something
name: Plan and merge to Main
on:
pull_request:
types: [ labeled, closed, opened ]
jobs:
Plan:
name: "Terraform Plan"'
if: github.base_ref == 'main' && join(github.event.pull_request.labels.*.name, '') == ''
Apply:
name: "Run Terraform Apply"
if: github.base_ref == 'main' && github.event.pull_request.merged == true && join(github.event.pull_request.labels.*.name, '') == ''
There’s no way to specify that a workflow should be triggered when a pull request is merged. However, because a merged pull request always results in a push, you can use the push event to accomplish your goal.
For example, let’s say that you want to run a workflow whenever a pull request is merged to your main branch. You can do something like this:
on:
push:
branches:
- main
also if you want to prevent push directly to main it's part of github pro plan.
Can I use check_run trigger in Github Actions the following way?
on:
check_run:
types: [completed]
conclusion: [success]
name: [mytest]
Is there anyone who can give me a correct example to follow?
I am writing a GitHub Action. I want some of my steps to run only on certain branches.
The whole action is set to run only on master and on branches beginning with features/lr.
on:
push:
branches:
- master
- features/lr*
I have a "deploy" step that I want to run on master and on branches beginning with features/lrd. (So for example if my branch is named features/lr-foo, then the deployment step should be skipped.)
I know I can do if conditionals like this:
- name: Deploy application
if: github.ref == 'refs/heads/master'
Can I also check whether github.ref matches a certain prefix or pattern? What is the syntax for that?
Something like this pseudocode:
- name: Deploy application
if: github.ref == 'refs/heads/master' || github.ref.matches('refs/heads/lrd*')
Thanks in advance!
The branches, branches-ignore, tags, and tags-ignore keywords accept glob patterns. You can check details in docs - filter pattern.
As for using expressions, docs don't mention matches function, but maybe you could use something like contains, startsWith or endsWith. See here for details.
Inspired by the answer from frennky, I ended up doing the following in my step, which is ugly but works:
- name: Deploy application
if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/features/lrd')
I have a project with Github Actions that implements multiple workflows that can be triggered by a single push event (depending on path filter).
So a push with a single commit can trigger multiple workflows, so far so good.
In each workflow I am running actions/github-script to create dynamic run-checks with the following step:
- uses: actions/github-script#v4
with:
github-token: ${{ inputs.github-token }}
script: |
const date = new Date();
const check = await github.checks.create({
owner: "${{ steps.vars.outputs.owner }}",
repo: "${{ steps.vars.outputs.repo }}",
name: "Custom Script",
started_at: date.toISOString(),
completed_at: date.toISOString(),
head_sha: "${{ inputs.sha }}",
external_id: "${{ github.run_id }}",
status: "completed",
conclusion: "success",
output: {
title: "Some funny title",
summary: "Build successful",
text: "Image pushed to https://${{ inputs.region }}.console.aws.amazon.com/ecr/repositories/private/${{ inputs.customer-id }}/modix/base/${{ inputs.image }}"
}
});
It is working like a charm, when a single workflow is triggered, but as soon as a push triggers multiple workflows, then only the first one that runs is showing the added check. all others but the first don't show the check but also no error?
Before I have tried the LouisBrunner/checks-action and it had the same problem so I created an issue: https://github.com/LouisBrunner/checks-action/issues/26. But now that it also fails by directly using octokit with github-script action, it feels like the problem is somewhere else...
UPDATE:
According to Gregors answer, I have tried giving the check a different name in each workflow by appending the run-id, I found that each parallel workflow is adding the check to the workflow that runs first... so the question now is, how to send it to a specific workflow run?
according to these docs, there is no dedicated parameter for that, it seems that it automatically detects the workflow using the head_sha?
name: "Custom Script ${{ github.run_id }}",
Try setting Custom Script to something different for each check run you create. I think multiple check runs with the same names are collapsed into only showing the last one. The reason is that that way you can override an status on a commit, by using the same name.
Sadly I found that it is simply impossible to attach a check to a specific workflow-run or check-suite. The problem is known for over a year now, but they didn't provide any solution yet. See in this thread.
In the name of a big automotive company, I have now submitted a feature request in the official feedback form of github.
PS: If the feature will be implemented in the future, I am going to create and accept another answer here.
I have an action job which upload the context to other website. The token was set and stored in the secret.MY_TOKEN.
But others who make the pull request also trigger this action job using the token I set.
How to limit the privilege of executing the jobs that only I can run this action job.
fyi my ci.yml as follow:
name: foobar
on: [push, pull_request]
jobs:
upload:
runs-on: ubuntu-latest
steps:
....
- name: execute upload
env:
TOKEN: ${{ secrets.MYTOKEN }}
run:
upl --token ${TOKEN}
I assume there are two security problems here.
The token is printed in log file.
others who can use this private token by trigger action with their own purpose.
Use the github.repository_owner context
https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context
The syntax should be something like:
- if: github.repository_owner == 'owner_name'
There is a new feature which could help, since July 2022:
Differentiating triggering actor from executing actor
Starting next week, workflow re-runs in GitHub Actions will use the initial run’s actor for privilege evaluation.
The actor who triggered the re-run will continue to be displayed in the UI, and can be accessed in a workflow via the triggering_actor field in the GitHub context.
Currently, the privileges (e.g. – secrets, permissions) of a run are derived from the triggering actor.
This poses a challenge in situations where the actor triggering a re-run is different than the original executing actor.
The upcoming change will differentiate the initial executing actor from the triggering actor, enabling the stable execution of re-runs.
For more details see Re-running workflows and jobs.
I don't believe allowing actions to run only for certain users is a native feature.
However, you could simply check the action context actor and exit early if the actor is not the yourself (or the owner of the repo, or whatever condition you'd like).