Using Pre-Built Actions for Automation

Tutorial 4 of 5

1. Introduction

In this tutorial, we will be learning how to utilize pre-built actions in GitHub Actions to automate common tasks. GitHub Actions is a powerful tool that allows you to automate your software workflows right from GitHub. Pre-built actions are ready-made actions provided by GitHub and the community that can be used to perform common tasks in your workflows.

By the end of this tutorial, you will be able to:
- Understand what pre-built actions are
- Learn how to use pre-built actions in your workflows
- Automate common tasks using pre-built actions

Prerequisites:
- Basic knowledge of GitHub
- A GitHub account

2. Step-by-Step Guide

2.1 Understanding Pre-built Actions

Pre-built actions are reusable pieces of code that perform a specific task. They can be used to automate tasks like building, testing, and deploying your projects.

To use a pre-built action, you need to reference it in your workflow file (.github/workflows/main.yml). You can find pre-built actions in the GitHub Marketplace.

2.2 Using Pre-built Actions

To use a pre-built action, you will need to include it in your workflow file. Here's a basic structure of a workflow file:

name: My Workflow

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Run a one-line script
      run: echo Hello, world!

In the above example, actions/checkout@v2 is a pre-built action that checks out your repository so your workflow can access it.

3. Code Examples

Let's look at some examples of using pre-built actions:

Example 1: Using the setup-node action to setup a specific Node.js version

steps:
- name: Use Node.js 12.x
  uses: actions/setup-node@v2
  with:
    node-version: 12.x

In this example, actions/setup-node@v2 is a pre-built action that sets up a Node.js environment with a specific version (12.x).

Example 2: Using the upload-artifact action to upload build artifacts

steps:
- uses: actions/upload-artifact@v2
  with:
    name: my-artifact
    path: ./dist/

In this example, actions/upload-artifact@v2 is a pre-built action that uploads build artifacts from your workflow. The artifacts are then available for download.

4. Summary

In this tutorial, we learned what pre-built actions are and how to use them in our workflows. We also looked at a couple of examples of using pre-built actions.

To learn more about pre-built actions, you can check out the GitHub Actions documentation.

5. Practice Exercises

Exercise 1: Create a workflow that uses the actions/checkout action to checkout your repository.

Exercise 2: Create a workflow that uses the setup-node action to set up a Node.js environment with version 14.x and actions/checkout to checkout your repository.

Exercise 3: Create a workflow that uses the actions/upload-artifact action to upload a file named test.txt from your repository.

Solutions:

  1. Here's a solution for the first exercise:
name: Checkout Repo

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
  1. Here's a solution for the second exercise:
name: Setup Node.js and Checkout Repo

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Use Node.js 14.x
      uses: actions/setup-node@v2
      with:
        node-version: 14.x
  1. Here's a solution for the third exercise:
name: Upload Artifact

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - uses: actions/upload-artifact@v2
      with:
        name: test
        path: ./test.txt

Remember, practice makes perfect. Keep creating and experimenting with different workflows and actions!