Getting Started with GitHub Actions

Tutorial 1 of 5

Getting Started with GitHub Actions

1. Introduction

Goal of the Tutorial

This tutorial aims to provide a hands-on introduction to GitHub Actions, a powerful tool for automating software workflows.

Learning Objectives

By the end of this tutorial, you will be able to:
- Understand what GitHub Actions is and its benefits.
- Create your first workflow using GitHub Actions.
- Understand basic concepts like events, jobs, steps, and actions.

Prerequisites

  • Basic understanding of Git and GitHub.
  • A GitHub account.

2. Step-by-Step Guide

GitHub Actions allows you to automate your software development workflows in the same place you store code and collaborate on pull requests and issues. You can write individual tasks, called actions, and combine them to create a custom workflow. Workflows are custom automated processes that you can set up in your repository to build, test, package, release, or deploy any code project on GitHub.

Concepts

  • Workflow: An automated procedure that you add to your repository. Workflows are made up of one or more jobs and can be scheduled or triggered by an event.
  • Event: A specific activity that triggers a workflow run.
  • Job: A set of steps that execute on the same runner.
  • Step: An individual task that can run commands in a job. A step can be either an action or a shell command.
  • Action: The smallest portable building block of a workflow.

Create Your First Workflow

  1. On GitHub, navigate to the main page of the repository.
  2. Under your repository name, click on the Actions tab.
  3. Find the template that matches your application language and click Set up this workflow.

3. Code Examples

Simple Workflow Example

This is a simple example of a workflow that runs whenever a push or pull request event occurs to the master branch.

name: CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Run a one-line script
      run: echo Hello, world!
    - name: Run a multi-line script
      run: |
        echo Add other actions to build,
        echo test, and deploy your project.
  • name: CI - This is the name of your workflow, it can be any string.
  • on: - This is the event that triggers the workflow. In this case, it's either a push or pull request to the master branch.
  • jobs: - This is where you specify the jobs to be run.
  • build: - This is the identifier you're assigning to the job.
  • runs-on: ubuntu-latest - This specifies the type of runner that the job will run on.
  • steps: - Steps are a sequence of tasks that will be executed as part of the job.
  • uses: actions/checkout@v2 - This is an action that checks-out your repository under $GITHUB_WORKSPACE, so your workflow can access it.
  • run: - This is where you'll define the commands to be run.

4. Summary

In this tutorial, you've learned the basics of GitHub Actions, including how to create your first workflow. You've also been introduced to concepts like events, jobs, steps, and actions.

Next steps could include learning more about the different events that can trigger workflows, exploring the marketplace of existing actions, or creating your own actions.

Additional Resources:
- GitHub Actions Documentation
- GitHub Learning Lab

5. Practice Exercises

  1. Exercise 1: Create a workflow that prints "Hello, World!" whenever a push event occurs to the master branch.
  2. Exercise 2: Add a job to the previous workflow that runs only on pull requests and prints "New Pull Request".

Solutions and Explanations

  1. Refer to the code example in this tutorial. The single line script echo Hello, world! prints "Hello, World!" to the console.

  2. You can add a new job to the workflow like this:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Run a one-line script
      run: echo Hello, world!
  pull_request:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Run a one-line script
      run: echo New Pull Request

In this case, the pull_request job includes an if condition to ensure it only runs on pull request events.

Keep practicing by creating workflows for different events and using different actions available in the marketplace.