I have a git repo where a workflow file called build.yml(which builds the project, and triggers iac pipeline that deploys my application in AWS resources) is set up under github Actions. This workflow is triggered when a commit/code push happened to the default branch. But code commits to this repo happens rarely. I want to trigger this workflow regularly say once in every month to trigger build and redeploy AWS resources(with updated patches from org level) via Actions.
Presently I am triggering the Actions workflow by Manually pushing an empty commit to the default branch.
Looking for a way to automatically push an empty commit to my default branch from github Actions at regular intervals using schedule cron from Actions workflow. Any pointers for the same?
I am able use the scheduler in Actions workflow using schedule cron as below, But need to push an empty commit to the default branch at that frequency
push:
branches: [main]
schedule:
- cron: '30 3 10 * *'
I'm about 15 months late to this post but I have been beating my head against the wall over the same issue, so I wanted to post a solution here in case it helps anyone else that finds this. Take this with a grain of salt since I'm still very green with this stuff, but the yaml code below worked for me
My goal with this was to automatically push an empty commit every day at 3AM (I'm hosting a website on railway.app, and this is seemingly one of the only ways to automate redeployment):
name: cron-schedule-updater
on:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '0 3 * * *'
jobs:
empty_commit:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- run: git config user.email "[email protected]"
- run: git config user.name "YOUR NAME"
- name: Create Empty Commit
run: git commit --allow-empty -m "Empty commit"
- name: Push Empty Commit
run: git push origin main # Change 'main' to your branch name
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Some notes, in order of importance:
1.) From the github repo dashboard, go to "Settings" -> "Actions", then scroll down to "Workflow permissions", and select "Read and write permissions." It won't work without selecting this
2.) The secret token is automatically generated with that last line, so nothing extra that you need to do there. Mostly sure that line is necessary but not 100%
3.) In the repo settings, also make sure that you don't have any branch protection rules preventing this, and note that you may have some extra headaches if it's a private repo
Anyways, I think that's everything. Hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With