Lab: Scheduled Workflow
Introduction
In this lab, you will create a scheduled workflow that runs at regular intervals using a cron expression. The workflow will also support manual triggering and will run when specific changes are pushed. This will help you understand how to configure workflows with schedules and additional triggers.
Estimated Duration: 10-20 minutes
Instructions
Step 1: Create the Workflow File
-
In your repository, create a new directory
.github/workflows
if it doesn’t exist. -
Create a new workflow file named
intro-scheduled-workflow.yml
inside.github/workflows
. -
Add the following YAML content to the file to define the workflow name:
name: Intro - Scheduled Workflow
-
Save and commit the file to the
main
branch.
Step 2: Add Workflow Triggers
-
Open the
intro-scheduled-workflow.yml
file. -
Add the following
on
section below thename
field:on: workflow_dispatch: schedule: - cron: '*/5 * * * *' # Every 5 minutes push: paths: - '.github/workflows/intro-scheduled-workflow.yml'
-
Commit the updated file.
Step 3: Add Workflow Jobs and Steps
-
Open the
intro-scheduled-workflow.yml
file again. -
Add the following job definition below the triggers:
jobs: execute: runs-on: ubuntu-latest steps: - name: Display current date and time run: echo "The current date and time is $(date)"
-
Commit your changes.
Step 4: Run and Test the Workflow
- Wait for the scheduled run (every 5 minutes),
- Or manually trigger it from the Actions tab,
- Or commit a change to
.github/workflows/intro-scheduled-workflow.yml
to trigger it on push.
Step 5: View Workflow Results
- Go to the Actions tab,
- Click the latest workflow run,
- Open the execute job and expand the Display current date and time step to see the output.
Optional: Customize the Schedule
- Change the cron expression in the
schedule
section to run at different intervals, - Commit your changes and observe the updated schedule.
Summary
You created a scheduled GitHub Actions workflow from scratch, configured triggers for schedule, manual dispatch, and push events, added steps to the job, ran the workflow, and checked its output.