googleapis / release-please-action

automated releases based on conventional commits
Apache License 2.0
1.7k stars 212 forks source link

Customize release PR contents #873

Closed rsenden closed 11 months ago

rsenden commented 11 months ago

TL;DR

Allow for easily customizing the contents of the release PR.

Detailed design

We're developing a single GitHub Action repository that contains multiple (composite) actions that need to be versioned together. For example, when invoking org/github-action@v1.0.0, this composite action should invoke the corresponding org/github-action/sub-action@v1.0.0. Unfortunately, GitHub doesn't seem to support such use cases out of the box, so when releasing a new version of our action repository, all composite actions need to be updated to use the proper org/github-action/<sub-action>@<version>.

Ideally, probably not directly related to release-please-action, individual branches should automatically use org/github-action/<sub-action>@main or org/github-action/<sub-action>@feat-1, if the calling workflow is referring to an action branch instead of release version.

Unless you have any better suggestions, the best way to accomplish this seems to be to have a workflow that updates the composite action.yml files on branches and in release PRs generated by release-please-action. As a proof of concept, I've come up with the following:

update-action-refs.sh script used by the workflow below:

#!/bin/bash
for f in $(find . -name 'action.yml'); do 
  if grep -qrlZ 'uses: org/github-action/' $f; then
    echo "Updating $f: version $1" && \
    sed -r -i -e "s|(uses: org\/github-action\/[^@]+)@.*|\1@$1|g" "$f"
  fi
done

GitHub Actions workflow

on:
  push:

permissions:
  contents: write
  pull-requests: write

name: Publish release
jobs:
  update-action-references:
    runs-on: ubuntu-latest
    steps:
      - name: Check-out source code
        uses: actions/checkout@v4
      - name: Update action version references to match branch
        run: |
          ./update-action-refs.sh "${GITHUB_REF#refs/heads/}"
          git config user.name github-actions
          git config user.email github-actions@company.com
          git add . && git commit -m "chore: Update action references" && git push || \
            echo "No update needed" 

  build-and-release:
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    needs: update-action-references
    steps:
      - name: Check-out source code
        uses: actions/checkout@v4

      - name: Generate and process release PR
        id: release_please
        uses: google-github-actions/release-please-action@v3
        with:
          release-type: simple
          package-name: ${{ github.event.repository.name }}

      - name: Publish v{major}.{minor} tag
        if: steps.release_please.outputs.release_created
        uses: richardsimko/update-tag@v1
        with:
          tag_name: v${{steps.release_please.outputs.major}}.${{steps.release_please.outputs.minor}}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 

      - name: Publish v{major} tag
        if: steps.release_please.outputs.release_created
        uses: richardsimko/update-tag@v1
        with:
          tag_name: v${{steps.release_please.outputs.major}}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Update release PR
        if: steps.release_please.outputs.pr
        shell: bash
        run: |
          prBranch=${{ fromJSON(steps.release_please.outputs.pr).headBranchName }}
          git fetch origin "${prBranch}"
          git checkout "${prBranch}"
          version=v$(cat version.txt)
          ./update-action-refs.sh "${version}"
          git config user.name github-actions
          git config user.email github-actions@company.com
          git add . && git commit -m "chore: Update action references" && git push || echo "No update needed" 

Based on some limited testing, this workflow seems to work fine, but improvement suggestions are welcome.

Some release-please-action improvement suggestions:

Additional information

See https://github.com/orgs/community/discussions/75505 for more information on this topic.

chingor13 commented 11 months ago

The core release-please library supports monorepos where you configure multiple "components" that are released and tagged independently. You will need to configure a "manifest configuration" to use this.

Additionally, we have the notion of plugins which can modify the release PRs after they are assembled and the most common use case is a "workspace plugin" which bumps internal dependency versions within a monorepo. So if A depends on B and B has a new version, we will also bump A's version and reference the new B version. We don't have a workspace plugin for knowing how GitHub action components in a monorepo are structured yet (would take a feature PR if this is a common setup in the community).