GitHub Actions Toolkit

repository·main·Indexed 26 days ago

https://github.com/actions/toolkit

A collection of specialized JavaScript/TypeScript packages for developing custom GitHub Actions. Includes @actions/core for essential functions like inputs, outputs, and logging; @actions/artifact for managing artifacts; @actions/cache for saving and restoring caches; @actions/exec for cross-platform command execution; and @actions/attest for generating signed attestations and SLSA provenance.

Tokens
28.5K
Snippets
85
Records
192
Agent score
88%

What's inside actions-toolkit

  1. Use @actions/core for GitHub Actions core functions

    main
    The @actions/core package provides essential functions for building GitHub Actions. It allows you to set action results (success/failure), log messages to the console, register secrets to prevent them from being leaked in logs, and export environment variables for subsequent steps in a workflow.
  2. Manage Problem Matchers

    main

    Problem matchers allow the runner to scan build output and automatically surface errors or warnings. Use ::add-matcher with a path to a .json file to register one, or ::remove-matcher to remove one by its owner.

    echo "::add-matcher::eslint-compact-problem-matcher.json"
    echo "::remove-matcher owner=eslint-compact::"
  3. Reference GitHub Actions using different refs

    main

    When using an action in a workflow, you can reference it using a major version tag, a specific version tag, or a full SHA1 hash.

    • Major version tags (e.g., @v1): Recommended for most users. They track the latest stable release within that major version, allowing for automatic bug fixes and security updates without breaking existing workflows.
    • Specific version tags (e.g., @v1.0.0): Useful if you need to pin to a known working version.
    • Full SHA1 (e.g., @41775a...): Provides maximum immutability and reliability, but prevents automatic receipt of patches or fixes for breaking changes in the underlying runner environment.

    Warning: Do not reference @main. The main branch is unstable and contains code for the next major version which may include breaking changes.

    steps:
        - uses: actions/javascript-action@v1        # recommended. starter workflows use this
        - uses: actions/javascript-action@v1.0.0    # if an action offers specific releases 
        - uses: actions/javascript-action@41775a4da8ffae865553a738ab8ac1cd5a3c0044 # sha
  4. Group and Ungroup log lines

    main

    Create collapsible regions in the GitHub Actions log UI by wrapping log output with ::group:: and ::endgroup:: commands.

    This is wrapped by the @actions/core methods startGroup(name: string): void and endGroup(): void.

    echo "::group::my title"
    # ... log lines ...
    echo "::endgroup::"
  5. Execute tools not in the PATH

    main

    If a tool is not available in the system's PATH, you can execute it by providing its absolute or relative file path as the first argument to exec.exec().

    const exec = require('@actions/exec');
    
    await exec.exec('"/path/to/my-tool"', ['arg1']);
  6. Implement logic in a Docker Action entrypoint

    main

    The execution logic for a Docker action is contained within the entrypoint script, typically named entrypoint.sh. This script is executed when the container starts. Arguments passed from the runs.args section in the metadata are available to this script as positional parameters (e.g., $1, $2).

    #!/bin/sh -l
    
    echo "hello $1"
  7. Ensure compatibility between upload and download actions

    main

    When using GitHub Actions workflows, ensure that the versions of actions/upload-artifact and actions/download-artifact are compatible. They both rely on the GitHub Actions toolkit.

    Compatibility Matrix:

    upload-artifactdownload-artifacttoolkit
    v4v4v2
    < v3< v3< v1

    Example workflow usage:

      uses: actions/upload-artifact@v4
      # ...
      uses: actions/download-artifact@v4
      # ...
  8. Create a JavaScript Action

    main
    To create a JavaScript action, use the javascript-action template. This includes support for tests, linting, workflows, publishing, and versioning. You can use @actions/core to retrieve inputs from your action's configuration.
  9. Define metadata for a Docker Action

    main

    Configure your action's identity and inputs in the action metadata file (typically action.yml). You must define the name, description, and specify that it runs using docker.

    Inputs defined in the metadata are made available to workflow authors via the with: keyword. In a Docker action, these inputs are typically passed to the container via the args field in the runs section.

    name: 'My Container Action'
    description: 'Get started with Container actions'
    author: 'GitHub'
    inputs: 
      myInput:
        description: 'Input to use'
        default: 'world'
    runs:
      using: 'docker'
      image: 'Dockerfile'
      args:
        - ${{ inputs.myInput }}
  10. Configure proxy support for GitHub Actions Toolkit

    main

    To ensure GitHub Actions work correctly behind a proxy server on self-hosted runners, follow these requirements:

    1. Requirement: Use @actions/tool-cache version 1.3.1 or higher.
    2. Recommendation: Use @actions/http-client for HTTP requests, as it is designed to work with proxy configurations.

    If you are using other HTTP clients, you must configure them using the environment variables provided by the GitHub Actions runner.

  11. Type webhook payloads using @octokit/webhooks-definitions

    main

    To get full TypeScript type safety for webhook payloads, install @octokit/webhooks-definitions and cast the github.context.payload based on the github.context.eventName.

    // 1. Install the definitions: npm install @octokit/webhooks-definitions
    
    import * as core from '@actions/core'
    import * as github from '@actions/github'
    import {PushEvent} from '@octokit/webhooks-definitions/schema'
    
    if (github.context.eventName === 'push') {
      // Cast the payload to the specific event type
      const pushPayload = github.context.payload as PushEvent
      core.info(`The head commit is: ${pushPayload.head_commit}`)
    }
  12. Search for files using `@actions/glob`

    main

    Use @actions/glob to find files matching specific glob patterns. You can provide relative paths (rooted against the current working directory) or absolute paths. Use glob.create(patterns) to initialize a globber and globber.glob() to retrieve the list of files.

    const glob = require('@actions/glob');
    
    const patterns = ['**/tar.gz', '**/tar.bz']
    const globber = await glob.create(patterns.join('\n'))
    const files = await globber.glob()