Skip to content

Latest commit

 

History

History
126 lines (103 loc) · 4.08 KB

README.md

File metadata and controls

126 lines (103 loc) · 4.08 KB

6 Using other events

Navigation
  1. Getting started
  2. Creating a workflow
  3. Building code in a workflow
  4. Running multiple jobs in parallel
  5. Running jobs in sequence
  6. Deploying to GitHub Pages
  7. Using other events to run workflows (this task)
  8. Outputs from steps and jobs
  9. Keeping dependencies up to date with Dependabot
  10. Matrices
  11. Workflow dispatch inputs and security verification
  12. Learn more about GitHub Actions

There are looads of events we can use to trigger our workflows. We mostly use the pull_request event, as it makes the most sense when validating pull requests, but some times we use the others, which make a huge list:

  • branch_protection_rule
  • check_run
  • check_suite
  • create
  • delete
  • deployment
  • deployment_status
  • discussion
  • discussion_comment
  • fork
  • gollum (yes, gollum)
  • issue_comment
  • issues
  • label
  • merge_group
  • milestone
  • page_build
  • project
  • project_card
  • project_column
  • public
  • pull_request
  • pull_request_comment (use issue_comment)
  • pull_request_review
  • pull_request_review_comment
  • pull_request_target
  • push
  • registry_package
  • release
  • repository_dispatch
  • schedule
  • status
  • watch
  • workflow_call
  • workflow_dispatch
  • workflow_run

Read more about them in the documentation.

6.1 workflow_dispatch

One of the events is workflow_dispatch which lets us trigger a workflow by the click of a button. This is useful for testing workflows, or for running workflows that are not triggered by an event, such as deploying to production. Let's create one that prints out hello world.

Because we're using a new event and not pull_request as before, we need to make a new workflow file for this.

  1. Create a new file called hello-world.yml in the .github/workflows directory.

  2. Add the following content to the file:

    name: Click to say hi
    
    on:
      - workflow_dispatch
    
    jobs:
      hello-world:
        runs-on: ubuntu-latest
        steps:
          - name: Hello world
            run: echo "Hello 🌏!"
  3. Commit and push your changes to the main branch

  4. Now go to the "Actions" tab in your repository (https://github.com/[your-username]/gh-actions-workshop/actions)

  5. Click on the Click to say hi workflow in the panel to the left and run it on the main branch

  6. See that there's a button called "Run workflow" in the top right corner

  7. Click it!!!

  8. Now see that a new run has been started and that it prints out Hello 🌏! in the logs

6.2 schedule

Another event that is useful is schedule. This lets us run a workflow on a schedule, for instance every day at 12:00. We use them to for instance run a workflow that checks for security vulnerabilities in our dependencies every day, to do backups, or to run workflows that are very long-running and therefore can't be run on every pull request. They're also useful for fetching data from other sources once a day.

Let's create a workflow that runs every five minutes (which is the shortest time interval) and prints out the current time. We'll use the CRON syntax to schedule the workflow. If you're not familiar with it, check out crontab guru to quickly create your own CRON expressions.

In a new workflow file (timestamp.yml), add the following content:

name: Timestamp

on:
  schedule:
    - cron: "*/5 * * * *" # Run every five minutes

jobs:
  timestamp:
    runs-on: ubuntu-latest
    steps:
      - name: Timestamp
        run: echo "The time is $(date)"

Important

Be sure to remove this workflow when you're done with it, as it will run every five minutes forever and that's not very sustainable 🌱

The next step is to learn about outputs from steps and jobs.