Lesson 48 of 60 – GitHub Actions Introduction
80%

GitHub Actions Introduction

GitHub Actions is a GitHub feature used to automate software development workflows. It can automatically run tasks when events happen in a repository, such as pushing code or opening a pull request.

Note: GitHub Actions can automate tasks such as testing, building applications, checking code, and deploying projects.

1. What is GitHub Actions?

GitHub Actions is an automation platform integrated with GitHub repositories. It allows developers to create workflows that run automatically in response to repository events.

GitHub Event
     ↓
Workflow
     ↓
Jobs
     ↓
Steps
     ↓
Result

2. Why Use GitHub Actions?

GitHub Actions can reduce repetitive manual work by automating development tasks.

  • Run tests
  • Build applications
  • Check code
  • Deploy applications
  • Publish packages
  • Automate workflows
  • Perform scheduled tasks

3. Automation Example

A common workflow is to run tests whenever code is pushed.

Developer
    ↓
git push
    ↓
GitHub
    ↓
GitHub Actions
    ↓
Run Tests
    ↓
Pass / Fail

4. GitHub Actions Workflow

A workflow is an automated process defined in a YAML file inside the repository.

.github/
└── workflows/
    └── test.yml

A workflow contains instructions describing when and how automation should run.

5. Workflow File

GitHub Actions workflow files are normally stored in:

.github/workflows/

For example:

.github/workflows/ci.yml

The workflow file uses YAML syntax.

6. Basic Workflow Structure

name: CI

on:
  push:

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - run: echo "Running tests"

This workflow runs when a push event occurs and then executes the defined job.

7. Workflow Name

The name property gives a workflow a readable name.

name: My CI Workflow

The name helps developers identify the workflow in the GitHub Actions interface.

8. Events

An event determines when a workflow should run.

Common examples include:

  • push
  • pull_request
  • workflow_dispatch
  • schedule
  • Other supported GitHub events
on:
  push:

9. Push Event

The push event can trigger a workflow when commits are pushed to the repository.

on:
  push:

This is commonly used for automated testing and build processes.

10. Pull Request Event

The pull_request event can trigger automation when a pull request is created or updated.

on:
  pull_request:

This is useful for automatically checking proposed changes.

11. Manual Workflow

A workflow can also be configured for manual execution using workflow_dispatch.

on:
  workflow_dispatch:

This allows an authorized user to start the workflow manually from GitHub.

12. Scheduled Workflows

GitHub Actions can also run workflows on a schedule using a cron expression.

on:
  schedule:
    - cron: '0 0 * * *'

Scheduled workflows are useful for recurring automation tasks.

13. Jobs

A workflow contains one or more jobs. A job is a group of steps that runs as a unit.

jobs:
  test:
    ...
  build:
    ...

A workflow can contain multiple jobs.

14. Job ID

Each job has an identifier.

jobs:
  test:

Here, test is the job ID.

Another example:

jobs:
  build:
    ...

15. Runner

A runner is the environment in which a GitHub Actions job executes.

runs-on: ubuntu-latest

The runner provides the environment needed to execute the job's steps.

16. Common Runner Example

A commonly used runner is:

runs-on: ubuntu-latest

The appropriate runner depends on the requirements of the project and workflow.

17. Steps

A job contains one or more steps. Steps perform the actual tasks of the job.

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - run: echo "Hello"
      - run: echo "Testing"

18. Run Command

The run keyword executes a shell command.

- run: echo "Hello GitHub Actions"

Commands can be used to install dependencies, run tests, build projects, or perform other tasks.

19. Actions

An action is a reusable unit of automation that can perform a particular task.

- uses: actions/checkout@v4

The uses keyword allows a workflow to use an existing action.

20. Checkout Action

The checkout action is commonly used to make the repository's source code available to the workflow.

- uses: actions/checkout@v4

After checkout, later steps can work with the repository files.

21. Installing Dependencies

A workflow can install project dependencies before running tests or building an application.

steps:
  - uses: actions/checkout@v4

  - run: npm install

  - run: npm test

The exact commands depend on the programming language and project.

22. Running Tests

GitHub Actions can automatically run tests after code is pushed or a pull request is updated.

steps:
  - uses: actions/checkout@v4
  - run: npm install
  - run: npm test

If the test command fails, the workflow can report a failed result.

23. Building a Project

Workflows can also build applications automatically.

steps:
  - uses: actions/checkout@v4
  - run: npm install
  - run: npm run build

Build commands depend on the project's technology and configuration.

24. Job Dependencies

Jobs can be configured so that one job depends on another job.

jobs:

  test:
    ...

  build:
    needs: test
    ...

Here, the build job depends on the test job.

25. GitHub Actions for CI

Continuous Integration (CI) involves frequently integrating code changes and automatically checking them.

Push Code
   ↓
Workflow
   ↓
Install Dependencies
   ↓
Run Tests
   ↓
Build
   ↓
Result

26. GitHub Actions for Deployment

GitHub Actions can also be used to automate deployment processes.

Code
 ↓
Test
 ↓
Build
 ↓
Deploy

Deployment steps depend on the hosting platform and project configuration.

27. Example CI Workflow

name: Node.js CI

on:
  push:
  pull_request:

jobs:
  test:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - run: npm install

      - run: npm test

This example checks the project whenever the configured events occur.

28. Complete GitHub Actions Workflow

Repository
    ↓
GitHub Event
    ↓
Workflow Starts
    ↓
Runner
    ↓
Job
    ↓
Checkout Code
    ↓
Install Dependencies
    ↓
Run Tests
    ↓
Build
    ↓
Result

29. GitHub Actions Best Practices

  • Keep workflows focused.
  • Use clear workflow and job names.
  • Run automated tests.
  • Use appropriate actions.
  • Keep action versions maintained.
  • Protect sensitive values.
  • Use secrets for sensitive credentials.
  • Review workflow permissions.
  • Keep commands easy to understand.
  • Check workflow results regularly.

30. Summary of GitHub Actions

GitHub Actions provides automation directly inside GitHub repositories. Workflows are defined using YAML files and can run automatically in response to repository events.

Event
 ↓
Workflow
 ↓
Job
 ↓
Runner
 ↓
Steps
 ↓
Action / Command
 ↓
Result

GitHub Actions can be used for continuous integration, testing, building, deployment, scheduled tasks, and many other development automation processes.

📌 Key Points

  • GitHub Actions is used to automate development workflows.
  • Workflows are normally stored in .github/workflows/.
  • Workflow files use YAML syntax.
  • Events determine when workflows run.
  • Common events include push and pull request events.
  • Jobs contain groups of steps.
  • Runners provide the environment where jobs execute.
  • Steps can run commands or use reusable actions.
  • The checkout action can make repository files available to a workflow.
  • GitHub Actions can automatically run tests and builds.
  • Workflows can also automate deployment.
  • GitHub Actions is commonly used for continuous integration.

🧠 Quick Quiz

Question: What is the main purpose of GitHub Actions?