changesets/action

repository·main·Indexed 21 days ago

https://github.com/changesets/action

A collection of GitHub Actions for automating the Changesets workflow, including versioning, publishing, and managing pull requests. It provides a high-level combined action for standard workflows, as well as specialized sub-actions: select-mode, version, pack, publish, pr-status, and pr-comment. The action supports two commit modes (git-cli and github-api) and integrates with the Changesets CLI to manage monorepo or individual package releases.

Tokens
6K
Snippets
9
Records
26
Agent score
76%

What's inside @changesets/action

  1. Overview of Changesets GitHub Actions

    main

    The changesets/action repository provides a collection of GitHub Actions designed to automate the Changesets workflow. These actions allow you to manage versioning, publishing, and pull request automation for monorepos or individual packages.

    Available specialized actions include:

    • changesets/action/select-mode: Select the mode to run a Changesets workflow.
    • changesets/action/version: Version packages and create or update a pull request.
    • changesets/action/pack: Pack publishable packages into tarballs.
    • changesets/action/publish: Publish packages to npm.
    • changesets/action/pr-status: Generate changeset status in PRs.
    • changesets/action/pr-comment: Create or update comments on PRs.
  2. Use the combined changesets/action for versioning and publishing

    main

    The main changesets/action is a high-level action that combines the functionality of select-mode, version, and publish. It is ideal for a standard automated workflow where you want to version packages and publish them in a single step.

    Note on Security: If you are using trusted publishing, it is recommended to use the individual sub-actions (version and publish) instead of this combined action to maintain tighter, more granular publish permissions.

    Requirements

    • The repository must be checked out.
    • @changesets/cli must be installed in the environment.
    • Job Permissions:
      • contents: write: Required to commit version changes.
      • pull-requests: write: Required to create pull requests.
      • id-token: write: Required if using trusted publishing.
    • GitHub Settings: In Actions > General, ensure Allow GitHub Actions to create and approve pull requests is enabled.
  3. Use update-id to update existing comments

    main

    To prevent duplicate comments, you can use the update-id input. When an update-id is provided, the action searches the Pull Request comments for a marker formatted as <!-- changesets-action-pr-comment:update-id --> (or <!-- changesets-action-pr-comment --> if the ID is exactly changesets-action-pr-comment).

    If a matching comment is found, the action updates that specific comment with the new body. If no matching comment is found, it creates a new one. This is useful for continuous updates (like CI status or coverage reports) where you want a single, evolving comment rather than a new one for every run.

  4. Validate Changesets CLI version compatibility

    main

    This action requires Changesets CLI v3. It is not compatible with Changesets CLI v2. If your project uses @changesets/cli version 2, you should use the Changesets action v1 instead.

    The validation checks both the version declared in your package.json (dependencies or devDependencies) and the actual installed version of @changesets/cli in your workspace.

  5. How the mode selection logic works

    main

    The action follows a specific hierarchy to determine the mode:

    1. Check for pending changesets: It first reads the current changeset state. If there are changesets present that contain releases, it returns mode: "version". If changesets exist but contain no releases, it returns mode: "none".
    2. Check for a publish plan: If no pending changesets are found, it executes the Changesets CLI with the publish-plan command.
      • If the resulting publish plan is empty, it returns mode: "none".
      • If the publish plan contains packages, it returns mode: "publish" and uploads the plan as a GitHub Action artifact.

    This logic ensures that you don't attempt to publish when there are still unversioned changesets, and you don't run versioning steps when everything is already up to date.

  6. Pull Request status messages

    main

    The Changesets GitHub Action automatically comments on Pull Requests to indicate whether the changes will trigger a version bump. It uses two primary message types based on the presence of changesets in the PR:

    🦋 Changeset detected

    This message appears when changesets are found. It informs the reviewer that the changes in the PR will be included in the next version bump and provides a summary of the affected packages and their semver types (Major, Minor, or Patch).

    ⚠️ No Changeset found

    This message appears when no changesets are detected. It warns that merging this PR will not cause a version bump for any packages. If the changes should result in a version bump, the developer is prompted to add a changeset.

    Both messages include a summary of the release plan (hidden within a <details> HTML element) and a link for maintainers to quickly add a new changeset to the PR.

  7. Configure the pr-comment GitHub Action

    main

    The pr-comment action is designed to post or update a comment on a Pull Request. It can either create a brand new comment or update an existing one if an update-id is provided.

    Required Inputs

    • github-token: A GitHub token with permissions to write comments on the Pull Request.
    • body: The content of the comment to be posted or updated.

    Optional Inputs

    • update-id: A unique identifier used to find and update an existing comment. If this is provided, the action will look for a comment containing a specific HTML marker (e.g., <!-- changesets-action-pr-comment:YOUR_ID -->). If found, the existing comment is updated; otherwise, a new comment is created.

    Outputs

    • comment-id: The ID of the comment that was either created or updated.
  8. Use the pr-status action to generate comment messages

    main

    The pr-status action is designed to run within a GitHub Actions workflow triggered by pull_request or pull_request_target events. It generates a formatted comment message based on the pull request context and exposes it as an output.

    To use the generated message in subsequent steps of your workflow, access the comment-body output.

    Requirements:

    • The workflow must be triggered by a pull_request or pull_request_target event. Running this action on other event types will cause it to fail.
    # Example workflow usage
    steps:
      - name: Get PR Status Comment
        id: pr_status_step
        uses: changesets/action@v1
    
      - name: Post Comment
        uses: actions/github-script@v6
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: "${{ steps.pr_status_step.outputs.comment-body }}"
            })
  9. Configure the Changesets version GitHub Action

    main

    The version action automates the process of versioning packages based on changesets and creating a Pull Request. It requires a GitHub token and a commit message, and allows for optional configuration of the script to run, PR title, PR draft status, base branch, and commit mode.

    Required Inputs

    • github-token: Your GitHub token for authentication.
    • commit-message: The message to use for the versioning commit.

    Optional Inputs

    • script: The script to run for versioning (e.g., npm version or yarn version).
    • pr-title: The title for the generated Pull Request.
    • pr-draft: Controls if the PR is a draft. Valid values are always, create, or omitted (defaults to not a draft).
    • pr-base-branch: The base branch for the Pull Request.
    • commit-mode: How commits are made. Valid values are git-cli (default) or github-api.

    Outputs

    • pr-number: The number of the created Pull Request.
  10. Pack changesets into an artifact

    main

    The pack action bundles changesets and potentially a publish plan into a directory, which is then uploaded as a GitHub Action artifact. This is useful for preparing a deployment package that can be used in subsequent workflow steps.

    Inputs

    • publish-plan-artifact-id (optional): The ID of a previously uploaded artifact containing a changeset-publish-plan. If provided, the action will download this plan and use it to drive the packing process via the --from-publish-plan flag in the Changesets CLI.

    Outputs

    • pack-dir-artifact-id: The ID of the newly created artifact containing the packed files.

    Behavior

    1. Validates the installed changesets CLI version.
    2. If publish-plan-artifact-id is provided, it downloads the changeset-publish-plan artifact and locates publish-plan.json.
    3. Executes the changesets pack command with the --out-dir flag.
    4. Uploads the resulting directory as a new artifact with a 30-day retention period.
  11. Configure Rolldown for the Changesets Action project

    main

    The rolldown.config.js file defines the bundling configuration for the project using defineConfig. It specifies multiple entry points for different functional modules (like index, pack, version, etc.), sets the output directory to dist, uses the esm module format, and enables minification and directory cleaning.

    import { defineConfig } from "rolldown";
    
    export default defineConfig({
      input: {
        index: "src/index.ts",
        pack: "src/pack/index.ts",
        "pr-status": "src/pr-status/index.ts",
        "pr-comment": "src/pr-comment/index.ts",
        "select-mode": "src/select-mode/index.ts",
        version: "src/version/index.ts",
        publish: "src/publish/index.ts",
      },
      output: {
        dir: "dist",
        format: "esm",
        cleanDir: true,
        minify: true,
        comments: false,
      },
      platform: "node",
    });
  12. Configure CommitMode for GitHub operations

    main

    When instantiating the GitHub class, you can specify the commitMode to determine how Git operations are performed.

    OptionTypeDescription
    commitMode"git-cli" | "github-api"Determines if operations use the local Git binary or the GitHub REST API. Defaults to "git-cli".
    new GitHub({
      githubToken: '...',
      cwd: '...',
      commitMode: 'github-api'
    });