2023-03-04

Actions in GitHub Actions

What is Action in GitHub Actions

An action in GitHub Actions is an individual task or operation that can be executed as part of a workflow. Actions are the building blocks of workflows and can be combined in various ways to create sophisticated and efficient pipelines for your projects. By chaining together multiple actions, you can automate complex processes such as building, testing, and deploying your code.

Types of Actions

We'll delve into the various types of actions available for use in your GitHub Actions workflows.

GitHub Marketplace Actions

GitHub Marketplace is a central hub for community-contributed actions that can be quickly integrated into your workflows.

https://github.com/marketplace?type=

These actions cover a wide range of use cases, such as code linting, testing, deployment, and monitoring. To include an action from the GitHub Marketplace in your workflow, you'll need to reference it in your workflow file using the following syntax:

yaml
steps:
  - name: My Step
    uses: owner/repository@version

For example, to use the actions/checkout action, you would include the following in your workflow file:

yaml
steps:
  - name: Checkout code
    uses: actions/checkout@v2

Third-Party Actions

Third-party actions are actions developed by external organizations or individuals not hosted in the GitHub Marketplace. These actions are typically hosted in their own repositories and can be included in your workflows by referencing the repository URL in your workflow file:

yaml
steps:
  - name: My Step
    uses: user/repo@version

Make sure to review the documentation and code of third-party actions before using them in your projects, as they may not have the same level of support or security guarantees as actions from the GitHub Marketplace.

Container Image from Docker Hub

You can utilize container images from Docker Hub as actions within your workflows. This is particularly useful when you need to run an action in a specific environment or with a particular set of tools. To use a container image from Docker Hub, specify the image name and version in your workflow file:

yaml
steps:
  - name: My Step
    uses: docker://image-name:image-version

For example, to run a step using the official Python 3.9 image, you would include the following in your workflow file:

yaml
steps:
  - name: Run Python script
    uses: docker://python:3.9
    with:
      args: "python my-script.py"

Creating Your Own Action

If you require a custom action tailored to your specific project needs, you can create your own action. Actions can be written in any programming language and can be hosted in your own repository or published in the GitHub Marketplace. Creating your own action grants you complete control over its functionality and implementation.

To create your own action, follow these steps:

  1. Create a new repository for your action.
  2. Add a Dockerfile or action.yml file to define your action's metadata and entry point.
  3. Write your action's code in your chosen programming language.
  4. Push your code to the repository.
  5. Reference your action in your workflow file using the repository URL.

For example, if you created an action in a repository called my-org/my-action, you would include it in your workflow file like this:

yaml
steps:
  - name: My Custom Action
    uses: my-org/my-action@v1

Popular Actions

We'll discuss some of the most popular and widely-used actions in GitHub Actions.

  • actions/checkout
    The actions/checkout action is used to check out your repository's code, making it available for use in your workflow. This action is an essential starting point for most workflows, as it provides access to the codebase for subsequent steps.
yaml
steps:
  - name: Checkout code
    uses: actions/checkout@v2
  • actions/setup-node
    The actions/setup-node action sets up a Node.js environment in your workflow, allowing you to run Node.js scripts and use Node.js-based tools. This action is particularly useful for JavaScript and TypeScript projects.
yaml
steps:
  - name: Set up Node.js
    uses: actions/setup-node@v2
    with:
      node-version: 14
  • actions/cache
    The actions/cache action caches dependencies and build outputs to improve workflow execution time. This action can be customized to cache specific directories or files depending on your project's requirements.
yaml
steps:
  - name: Cache dependencies
    uses: actions/cache@v2
    with:
      path: node_modules
      key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
      restore-keys: |
        ${{ runner.os }}-node-
  • actions/upload-artifact
    The actions/upload-artifact action uploads build artifacts for use in other jobs or for storage. This action is useful for sharing build outputs, such as compiled binaries or test results, between jobs or for later use.
yaml
steps:
  - name: Upload build artifacts
    uses: actions/upload-artifact@v2
    with:
      name: my-artifact
      path: build/output
  • actions/download-artifact
    The actions/download-artifact action downloads build artifacts from previous jobs in your workflow. This action is typically used in conjunction with the actions/upload-artifact action to share build outputs between jobs.
yaml
steps:
  - name: Download build artifacts
    uses: actions/download-artifact@v2
    with:
      name: my-artifact
  • codecov/codecov-action
    The codecov/codecov-action action uploads code coverage reports to Codecov for analysis and tracking. This action is useful for monitoring the code coverage of your tests and ensuring that your codebase maintains a high level of test coverage.
yaml
steps:
  - name: Upload coverage to Codecov
    uses: codecov/codecov-action@v1
    with:
      token: ${{ secrets.CODECOV_TOKEN }}
      file: ./coverage/lcov.info

References

https://docs.github.com/en/actions

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!