Github action works on push but not scheduled - github-actions

I am running the example of javascript github actions and it works just fine when I have
on: [push]
but not when I have
on:
schedule:
- cron: '*/5 * * * *'
I expect the github action to run every 5 minutes but it doesn't seem to run at all.
Here is the rest of my code for reference
.github/worflows/main.yml
on:
schedule:
- cron: '*/5 * * * *'
jobs:
hello_world_job:
runs-on: ubuntu-latest
name: A job to say hello
steps:
- name: Hello world action step
id: hello
uses: StephenVNelson/website/#3-experiment-with-actions
with:
who-to-greet: 'Mona the Octocat'
# Use the output from the `hello` step
- name: Get the output time
run: echo "The time was ${{ steps.hello.outputs.time }}"
./action.yml
name: 'Hello World'
description: 'Greet someone and record the time'
inputs:
who-to-greet: # id of input
description: 'Who to greet'
required: true
default: 'World'
outputs:
time: # id of output
description: 'The time we greeted you'
runs:
using: 'node12'
main: './github-actions/main.js'
./github-actions/main.js
const core = require('#actions/core');
const github = require('#actions/github');
try {
// `who-to-greet` input defined in action metadata file
const nameToGreet = core.getInput('who-to-greet');
console.log(`Hello ${nameToGreet}!`);
const time = (new Date()).toTimeString();
core.setOutput("time", time);
// Get the JSON webhook payload for the event that triggered the workflow
const payload = JSON.stringify(github.context.payload, undefined, 2)
console.log(`The event payload: ${payload}`);
} catch (error) {
core.setFailed(error.message);
}

You won't be able to schedule it for every 5 minutes as the "shortest interval you can run scheduled workflows is once every 15 minutes":
https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
Change it to '*/15 * * * *' and you'll be fine.

As mentioned in the GitHub documentation about Scheduled events
The schedule event can be delayed during periods of high loads of GitHub Actions workflow runs. High load times include the start of every hour. To decrease the chance of delay, schedule your workflow to run at a different time of the hour.
Read further : No assurance on scheduled jobs?

Related

GitHub action workflow not triggering

I have created a new YAML file and that flow not triggering. Whereas I have other workflows that are working as expected.
I want to assign a label on the project card moved from to-do to in progress.
Not Working flow:
name: Update Label on Project Card Move
on:
project_card:
types: [moved]
jobs:
update_label:
if: ${{ github.event.project_card.column_id == '192285912' }}
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- uses: actions/github-script#v6
with:
script: |
// this gets the number at the end of the content URL, which should be the issue/PR number
const issue_num = context.payload.project_card.content_url.split('/').pop()
github.rest.issues.addLabels({
issue_number: issue_num,
owner: context.repo.owner,
repo: context.repo.repo,
name: ["in progress"]
})

Github action that fails if no commit for 24 hours

We are using github to backup configuration files automatically from our servers.
To check that the cron job is working correctly is there a way to have an action failing if there is a no commit for the last 24 hours ?
You can use action with below structure
Github scheduled action, Uses cron syntax
Step actions/checkout step to checkout code
Custom step to get last commit date time e.q.
git log -1 --format=%cd //Outputs: time of last commit
git log -1 --format=%cr //Output: X days ago
Check this and fail the workflow, if condition met.
Eventually that the action to check we are using
name: CheckBackup
on:
workflow_dispatch:
schedule:
- cron: '0 1 * * *' # every day at 1:00am
jobs:
build:
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v3
- name: Check dates difference
run: |
echo "Github last update : " + `git log -1 --format=%cd$`
echo "Now : " + `date`
let now=`date +%s`
let backupTime=`git log -1 --format=%cd$ --date=raw | grep -o "^\w*\b"`
deltaHours=$((now-backupTime))
deltaHours=$((deltaHours/3600))
if [ $deltaHours -ge 8 ]; then
echo 'failed : ' + $deltaHours + ' hours'
exit 1
else
echo 'ok : ' + $deltaHours
exit 0
fi

Schedule Trigger Github Action workflow with Input parameters

I want to run my Github workflow two ways:
Manually by user
Cron job
Now, everything was running fine until I added input parameters. After that, the cron job is running but not picking default value.
Here is my yaml:
name: WebDriverIO Automation
on:
workflow_dispatch:
inputs:
typeOfTesting:
type: choice
description: Select Type of Test
default: 'stage-test-local-All'
required: true
options:
- stage-test-local-All
- stage-test
- stage-test-local-Sanity
- prod-test
branches:
- workingBranch
- JSNew
schedule:
- cron: "*/5 * * * *"
inputs are available to workflows triggered by the workflow_dispatch event only (and to any workflows that are called by dispatched workflows).
You can use the || expression operator to set the default values for input parameters. For example:
on:
schedule:
- cron: '15 0,18 * * 0-5'
workflow_dispatch:
inputs:
logLevel:
required: true
type: string
jobs:
test:
uses: ./.github/workflows/run-job-with-params.yml
secrets: inherit
with:
springProfile: 'production'
logLevel: ${{ inputs.logLevel || 'DEBUG' }}
In the above example the logLevel parameter is set to DEBUG when the workflow is triggered by cron. When the job is triggered by a dispatch event it gets set to the input value.
Below code worked
name: WebDriverIO Automation
on:
workflow_dispatch:
inputs:
typeOfTesting:
type: choice
description: Select Type of Test
default: 'stage-test-local-All'
required: true
options:
- stage-test-local-All
- stage-test
- stage-test-local-Sanity
- prod-test
branches:
- workingBranch
schedule:
- cron: "*/5 * * * *"
...
..
..
- name: Test
run: |
if [ ${{ github.event.inputs.typeOfTesting }} != "" ]; then
npm run ${{ github.event.inputs.typeOfTesting }}
else
npm run stage-test-local-All
fi

Having issues with variables and outputs

I started learning Github Actions a few days ago and I am playing around with it. I am having issues with defining outputs and using them in variables or as input values in another workflow step. I am not sure what I am doing wrong.
name: Demo
on:
workflow_dispatch:
jobs:
build:
runs-on: [ ubuntu-latest ]
outputs:
paths: ${{ steps.find_path.outputs.paths }}
steps:
- name: Code Checkout
uses: actions/checkout#v2
- name: Find Paths
id: find_path
run: |
all_paths=$(cat document.txt)
# some code
echo "::set-output name=paths::$(echo ${all_paths})"
- name: List
id: list_path
run: |
var_paths=${{ steps.find_path.outputs.paths }}
for part in ${var_paths[#]}; do
# parent_dir=$(cat...)
# ....
# some code
echo $part
done
I have a text file with some paths in it
/home/ubuntu
/home/ubuntu/docs
/home/ariel/code
I want to use the output from find_path step in another list_path step. There is some other code I didn't include as it's irrelevant to my question. So I defined the paths output in find_path step and it is basically a space-separated string /home/ubuntu /home/ubuntu/docs /home/ariel/code and that's format I want to have.
But I don't know how to use outputs from one step as input value in another step.
Basically,
var_paths=/home/ubuntu /home/ubuntu/docs /home/ariel/code
But when I loop var_paths I get weird errors like no such file or directory.
You're missing the quotes when assigning the output from the step to the variable:
name: Demo
on:
workflow_dispatch:
jobs:
build:
runs-on: [ ubuntu-latest ]
steps:
- name: Code Checkout
uses: actions/checkout#v3
- name: Find Paths
id: find_path
run: |
all_paths=$(cat document.txt)
# some code
echo "::set-output name=paths::$(echo ${all_paths})"
- name: List
id: list_path
run: |
# add quotes here
var_paths="${{ steps.find_path.outputs.paths }}"
for part in ${var_paths[#]}; do
# parent_dir=$(cat...)
# ....
# some code
echo $part
done
Misc comments:
You don't need to declare the outputs on the job level unless the output from a step should also be the output from the job. In your case, you're only using the output between steps.
The checkout action is available in v3

How to pass an API token as output variable between jobs in GitHub Actions

I'm new to GitHub Actions (and yaml syntax) so I may be misunderstanding something about the ability to pass data between jobs: I've been trying to use a workflow with two jobs:
authenticateWithAuth0API asks for a token to be generated
triggerNetlifyFunction uses the token to authenticate with a Lambda function
For the first job, I can see that I do get back some kind of response that is saved as an output. The logs show the response is an access token with value ***. I assume the value appears as asterisks in the logs because the runner understands this value to be sensitive (a secret - though not a GitHub Secret).
I was under the impression that I could declare this as an output (which seems to go through ok) and then use it in the next job with the "needs" context. I'm using it as the value to the "Authorization" header for a call triggered in the next job.
However, in the logs I can see that the value of the header is empty showing up as Authorization:"". Am I missing something in terms of the ability to pass sensitive variables between jobs?
jobs:
authenticateWithAuth0API:
runs-on: ubuntu-latest
outputs:
token: ${{ steps.getToken.outputs.API_RESPONSE }}
steps:
- uses: actions/checkout#v2
- id: getToken
uses: fjogeleit/http-request-action#v1.8.0
with:
url: <<removed: some token generation endpoint>>
data: '{"client_id":"${{ secrets.... }}","client_secret":"${{ secrets....}}","audience":"${{ secrets.... }}","grant_type":"client_credentials"}'
- id: saveResponse
run: echo "::set-output name=API_RESPONSE"
triggerNetlifyFunction:
runs-on: ubuntu-latest
needs: authenticateWithAuth0API
steps:
- id: callFunction
uses: fjogeleit/http-request-action#v1.8.0
with:
url: <<removed: netlify function url>>
customHeaders: '{"Authorization":"${{ needs.authenticateWithAuth0API.outputs.token }}"}'
- id: ShowFunctionResponse
run: echo ${{ steps.callFunction.outputs.response }}
DEBUG LOGS FROM JOB1:
##[debug]..Evaluating String:
##[debug]..=> 'token'
##[debug]=> '***'
##[debug]Result: '***'
DEBUG LOGS FROM JOB2:
##[debug]Evaluating: format('{{"Authorization":"{0}"}}', needs.authenticateWithAuth0API.outputs.token)
##[debug]Evaluating format:
##[debug]..Evaluating String:
##[debug]..=> '{{"Authorization":"{0}"}}'
##[debug]..Evaluating Index:
##[debug]....Evaluating Index:
##[debug]......Evaluating Index:
##[debug]........Evaluating needs:
##[debug]........=> Object
##[debug]........Evaluating String:
##[debug]........=> 'authenticateWithAuth0API'
##[debug]......=> Object
##[debug]......Evaluating String:
##[debug]......=> 'outputs'
##[debug]....=> Object
##[debug]....Evaluating String:
##[debug]....=> 'token'
##[debug]..=> null
...
##[debug]....Evaluating String:
##[debug]....=> 'token'
##[debug]..=> null
##[debug]=> '{"Authorization":""}'
##[debug]Result: '{"Authorization":""}'
##[debug]Loading env
Added debug logs... if i'm reading this correctly it seems that my output variable token is not a string, it's an object {access_token: *** }? But even so, why would it come up as "" (empty string in the authorization header)? Should it not have added an object at least?
I believe this is an expected behaviour as Github Actions runner probably can't check the output value, and ensure the secret isn't exposed through the workflow afterwards.
A workaround to your issue could be to save the output (API_RESPONSE) as a secret on the first job, and then access it as any other secret on the second job.
This GH secrets action could help you do it (it would even update the secret if it is already set).
Note: There may be other actions available providing a similar result.