Exercise - Create a custom JavaScript GitHub action

In this lab, you will learn how to create custom GitHub Actions using JavaScript. While GitHub provides many pre-built actions, creating your own custom actions allows you to build exactly the automation you need and share it with others. JavaScript actions are portable and can run on any runner that supports Node.js.

You will learn how to:

  • Understand the structure of GitHub Actions
  • Create a JavaScript action from scratch
  • Define action inputs and outputs
  • Use the actions toolkit libraries
  • Test your custom action
  • Publish and share your action
  • Use your custom action in workflows

This lab takes approximately 45-60 minutes to complete.

Before you start

To complete the lab, you need:

  • A GitHub user account. If you don't have one, you can create a new account. If you need instructions on how to create a GitHub account, refer to the article Creating an account on GitHub.
  • Basic understanding of GitHub Actions workflows.
  • JavaScript/Node.js knowledge (basic to intermediate level).
  • A web browser with access to the internet.

Complete the exercise on GitHub

In this exercise, you'll create a custom JavaScript action through a hands-on GitHub Skills exercise.

Note: This exercise is hosted on GitHub Skills and provides an interactive learning experience. You'll build a working action that performs a useful task and can be reused across different projects.

The exercise consists of the following activities:

  1. Start a web browser and navigate to the exercise repository: https://github.com/skills-dev/write-javascript-actions

  2. On the exercise page, select the Use this template button to copy the exercise to your GitHub account.

    Note: Simply copy the exercise to your account, then give GitHub about 20 seconds to prepare the first lesson, then refresh the page.

  3. Follow the instructions on the repository's README to complete all the challenges, which include:

    • Understanding action types (JavaScript, Docker, Composite)
    • Creating action metadata in action.yml file
    • Defining inputs and outputs for your action
    • Writing JavaScript code using the actions toolkit
    • Using @actions/core for logging and error handling
    • Using @actions/github for API interactions
    • Testing your action in a workflow
    • Publishing your action to the GitHub Marketplace
    • Versioning and releasing your action
  4. Work through each step in the exercise, following the prompts and instructions provided.

    Note: Custom actions enable you to encapsulate complex logic and make it reusable across multiple projects and repositories.

  5. When you finish all the challenges, you'll have created a functional custom action that you can use and share.

What you've learned

After completing this exercise, you should be able to:

  • Understand the different types of GitHub Actions
  • Create custom JavaScript actions from scratch
  • Define action metadata with inputs and outputs
  • Use the GitHub Actions toolkit libraries
  • Handle action inputs and set outputs
  • Implement error handling and logging
  • Test custom actions in workflows
  • Version and release actions
  • Publish actions to the GitHub Marketplace
  • Share reusable automation with the community

Congratulations! You've completed the "Create a custom JavaScript GitHub action" exercise and learned how to extend GitHub Actions with your own automation!