Setting up GitHub Actions

Setting up GitHub Actions

2022, Dec 28    

GitHub Actions is a powerful tool that allows developers to automate their workflow and build, test, and deploy their code directly from GitHub. With GitHub Actions, developers can create custom, reusable “actions” that can be combined to create a workflow that can run on a variety of different triggers, such as a new push to a repository, the creation of a new issue, or the expiration of a scheduled timer.

In this tutorial, we will walk through the steps to set up a simple workflow that will automatically build and deploy a static website to GitHub Pages every time a commit is pushed to the repository.

Prerequisites

Before we begin, you will need to have the following:

  • A GitHub account
  • A repository on GitHub that you want to use for your static website
  • A static website that you want to deploy to GitHub Pages (you can use an existing one or create a new one)

Setting up the workflow

Navigate to your repository on GitHub and click on the “Actions” tab. Click on the “Set up a workflow yourself” button. This will open a new file in your repository called main.yml. This file will contain the code for your workflow. At the top of the file, add the following lines to define the name and trigger for your workflow:


name: Deploy to GitHub Pages
on: [ push ]

This will give your workflow the name “Deploy to GitHub Pages” and will trigger it to run every time a commit is pushed to the repository.

Next, we need to define the steps that will be run as part of the workflow. For our simple workflow, we will only need two steps: one to build our static website and another to deploy it to GitHub Pages. To build the website, we will use the actions/setup-node action, which installs Node.js and the necessary dependencies for our workflow. Add the following lines to define this step:


jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-node@v1
        with:
          node-version: 14.x
      - run: npm install
      - run: npm run build

This will install Node.js version 14, install any dependencies specified in your package.json file, and then run the build script defined in your package.json file.

To deploy the website to GitHub Pages, we will use the peaceiris/actions-gh-pages action. Add the following lines to define this step:


deploy:
  needs: build
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v2
    - uses: peaceiris/actions-gh-pages@v3
      with:
        github_token: $
        publish_dir: ./dist

This will check out the code from your repository and then use the actions-gh-pages action to publish the contents of the ./dist directory (which should contain the built version of your static website) to GitHub Pages.

Save the main.yml file to commit the changes to your repository.

Testing the workflow

Now that we have our workflow set up, it’s time to test it out. To do this, we just need to make a change to our static website and push it to the repository.

Make a change to your static website and commit the changes to your repository. Push the changes to your repository. Navigate back to the “Actions” tab in your repository and you should see the workflow run in the “Workflows” section. Click on the workflow to see the details of each step, including any logs or errors that may have occurred. If everything went smoothly, your static website should now be deployed to GitHub Pages and available at the URL provided in the “Settings” tab of your repository.

Customizing the workflow

The workflow we have created is a very basic example, but there are many other actions and options available that can help you customize your workflow to fit your specific needs.

For example, you can add additional steps to run tests, lint your code, or deploy to other platforms. You can also set up multiple jobs and specify dependencies between them, or use conditional statements to control the flow of the workflow.

To learn more about all the options available with GitHub Actions, you can check out the GitHub Actions documentation.

I hope this tutorial has given you a good starting point for using GitHub Actions to automate your workflow.