Automate tests with GitHub Actions
Automate tests with GitHub Actions
A team should not depend on every developer remembering to run the same checks manually before each merge. GitHub Actions can run repeatable workflows in response to repository events.
What you’ll learn
- Describe events, jobs, runners and steps.
- Create a simple workflow file.
- Use CI as a pull-request status check.
Event to automated result
Event
Push or pull request occurs.
triggerJob
A runner executes defined steps.
build/testResult
Logs and status are attached to the repository event.
pass / fail
A workflow is event-driven automation stored in the repository
GitHub Actions workflows are YAML files under .github/workflows. A workflow has one or more triggers, jobs and steps. Jobs run on runners; steps run commands or reusable actions.
A common CI workflow runs on pushes and pull requests, checks out the code, installs dependencies and runs tests. Keep permissions minimal and treat third-party actions as dependencies that deserve review.
Create a minimal CI workflow
Run this in a disposable practice repository unless the lesson explicitly says otherwise.
mkdir -p .github/workflows
cat > .github/workflows/ci.yml <<'YAML'
name: CI
on:
push:
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Run project tests here"
YAMLAfter you commit and push this file, matching repository events can trigger the workflow. Replace the echo step with the real build and test commands for your project.
Practice: GitHub Actions
Your task: Add a second step that prints git --version. Open the Actions tab after pushing and find the job logs for both steps.
Common pitfall to avoid
Secrets must not be printed to logs. Use repository/environment secrets and the built-in GITHUB_TOKEN with intentionally scoped permissions when workflows need authentication.
Check your understanding
GitHub Actions: what to remember
Automate tests with GitHub Actions is complete when you can explain the state being changed before you run the command, not merely repeat the syntax.
Read further: official supporting documentation.
Reaching the recap and completing the quizzes are separate milestones.