Lesson 12 GitHub Actions

In this lesson, we will introduce you to GitHub Actions, a powerful automation tool that allows you to create custom workflows for your projects. You will learn how to create basic workflows, use actions from the marketplace, and build custom actions.

Objectives

  • Understand the purpose and benefits of GitHub Actions
  • Learn how to create a basic workflow
  • Discover how to use actions from the marketplace
  • Explore building custom actions

Introduction to GitHub Actions

GitHub Actions is an automation tool that allows you to create custom workflows for your projects. With GitHub Actions, you can automate tasks such as building, testing, and deploying your code, as well as managing issues and pull requests. Workflows are defined using YAML files and can be triggered by various events, such as pushing code or opening a pull request.

Creating a basic workflow

To create a basic workflow, you need to create a YAML file in the .github/workflows directory of your repository. The file should define the events that trigger the workflow, the jobs to be executed, and the steps within each job. Each step can either run a shell command or use an action from the marketplace.

Using actions from the marketplace

The GitHub Actions Marketplace is a collection of pre-built actions that you can use in your workflows. These actions can help you automate common tasks, such as setting up a specific environment, deploying to a hosting service, or running tests. To use an action from the marketplace, simply include it in the uses field of a step in your workflow file.

Building custom actions

In addition to using pre-built actions from the marketplace, you can also create your own custom actions. Custom actions can be written in any programming language and can be packaged as Docker containers or JavaScript actions. To create a custom action, you need to create a new repository, add a Dockerfile or action.yml file, and write the code for your action. Heres an example of a simple action that executes a greeting with every PR.

name: Greetings

on:
  pull_request:
    types: [opened]
  issues:
    types: [opened]

jobs:
  greeting:
    runs-on: ubuntu-latest
    steps:
    - name: Greet new contributors
      uses: actions/github-script@v5
      with:
        script: |
          const issue = context.issue;
          const author = context.payload.sender.login;
          const repo = context.repo;
          const message = `Hello @${author}! Thank you for contributing to our project! We appreciate your effort and look forward to reviewing your changes. If you have any questions, feel free to ask.`;
          if (context.eventName === "pull_request") {
            github.rest.pulls.createReview({
              ...repo,
              pull_number: issue.number,
              body: message,
              event: 'COMMENT',
            });
          } else {
            github.rest.issues.createComment({
              ...repo,
              issue_number: issue.number,
              body: message,
            });
          }

Exercises

Create a basic workflow

Create a new repository on GitHub and add a basic workflow file in the .github/workflows directory. The workflow should be triggered on every push to the repository and should run a simple shell command, such as printing "Hello, World!" to the console.

Use an action from the marketplace

Modify the workflow you created in the previous exercise to include an action from the GitHub Actions Marketplace. Choose an action that is relevant to your project, such as setting up a specific programming language environment or running tests.

Build a custom action

Create a new repository on GitHub and build a custom action. The action can be a simple one, such as printing a custom message to the console or checking for specific keywords in the commit messages. Once you have created the custom action, modify the workflow in your main repository to use the custom action.