blob: aa58206caa62a18cdd85813f3cf7854a63abbf8e [file]
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Reusable Release Workflow
# This workflow is designed to be called from other workflows.
on:
workflow_call:
inputs:
version:
description: 'The version to release'
required: true
type: string
git_ref:
description: 'The git ref to use to create the final release.'
type: string
required: true
publish_to_sonatype:
description: 'Publish the release to Sonatype'
type: boolean
required: true
publish_to_bcr:
description: 'Publish the repo to BCR'
type: boolean
required: false
default: true
secrets:
SONATYPE_USERNAME_TOKEN:
description: 'Sonatype username token'
required: false
SONATYPE_PASSWORD_TOKEN:
description: 'Sonatype password token'
required: false
MAVEN_GPG_PRIVATE_KEY:
description: 'GPG private key for signing Maven artifacts'
required: false
MAVEN_GPG_PASSPHRASE:
description: 'Passphrase for the GPG private key'
required: false
jobs:
validation:
runs-on: ubuntu-latest
steps:
- name: Checkout current commit
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
# Fetch the entire history to have branch information. This will allow us to check that
# the commit we start the release on is part of the master branch.
# In our case, fetching the whole history only adds a few seconds to the checkout time for
# our larger repository, which is acceptable for a release process that is not run
# frequently.
fetch-depth: 0
ref: ${{ inputs.git_ref }}
- name: Check if on Master Branch
run: |
if ! git merge-base --is-ancestor "${{ inputs.git_ref }}" "refs/remotes/origin/master"; then
echo "Error: Releases can only be created from commits that are on the 'master' branch."
exit 1
fi
- name: Check Tag existence
run: |
TAG_NAME="${{ inputs.version }}"
# Get all remote refs matching the TAG_NAME
ALL_REFS=$(git ls-remote origin "$TAG_NAME")
# Check if any refs were found
if [ -n "${ALL_REFS}" ]; then
# Check if this is a tag reference
if echo "${ALL_REFS}" | grep -q "refs/tags/${TAG_NAME}"; then
echo "Error: Tag '$TAG_NAME' already exists. Aborting."
exit 1
else
echo "A ref with name '${TAG_NAME}' exists but is not a tag (e.g., a branch). Aborting."
exit 1
fi
fi
run_tests:
uses: ./.github/workflows/ci.yaml
with:
git_ref: ${{ inputs.git_ref }}
create_git_tag:
needs: [validation, run_tests]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout current commit
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: ${{ inputs.git_ref }}
- name: Create and Push Tag
run: |
TAG_NAME="${{ inputs.version }}"
git config --local user.email "j2cl-github-bot@github.com"
git config --local user.name "J2CL GitHub Bot"
git tag -a "$TAG_NAME" -m "Release $TAG_NAME"
git push origin "$TAG_NAME"
release:
needs: create_git_tag
uses: bazel-contrib/.github/.github/workflows/release_ruleset.yaml@v7.4.0
with:
prerelease: ${{ contains(inputs.version, '-RC') }}
release_files: ${{ github.event.repository.name }}-*.tar.gz
tag_name: ${{ inputs.version }}
# TODO(dramaix): contribute upstream to allow skipping test.
bazel_test_command: "bazel build"
publish_to_sonatype:
runs-on: ubuntu-latest
if: ${{ inputs.publish_to_sonatype }}
needs: release
steps:
- name: Check secrets are provided
run: |
check_secret() {
if [[ -z "${2:-}" ]]; then
echo "Error: secret ${1} is required but not provided."
exit 1
fi
}
check_secret "SONATYPE_USERNAME_TOKEN" "${{ secrets.SONATYPE_USERNAME_TOKEN }}"
check_secret "SONATYPE_PASSWORD_TOKEN" "${{ secrets.SONATYPE_PASSWORD_TOKEN }}"
check_secret "MAVEN_GPG_PRIVATE_KEY" "${{ secrets.MAVEN_GPG_PRIVATE_KEY }}"
check_secret "MAVEN_GPG_PASSPHRASE" "${{ secrets.MAVEN_GPG_PASSPHRASE }}"
- name: Checkout release tag
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: ${{ inputs.version }}
# Maven and Bazel require Java to be available.
- name: Setup Java
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5.2.0
with:
java-version: '21'
distribution: 'zulu'
java-package: jdk
# This will trigger the import of the private key in the GPG secret keyring.
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }}
# This will create the maven settings.xml file needed for deploying to Sonatype.
server-id: central
server-username: SONATYPE_USERNAME_TOKEN
server-password: SONATYPE_PASSWORD_TOKEN
gpg-passphrase: MAVEN_GPG_PASSPHRASE
- name: Setup Bazel
uses: bazelbuild/setup-bazelisk@v3
# NOTE: This step assumes the calling repository has this script at the specified path.
- name: Publish to Sonatype
run: |
auto_publish_flag=""
# We auto-publish the Sonatype release if we do not produce a BCR release because there
# are no subsequent validation steps (like those for BCR) to wait for before making the
# Sonatype release public.
if [[ "${{ inputs.publish_to_bcr }}" == "false" ]]; then
auto_publish_flag="--sonatype-auto-publish"
fi
./maven/publish_to_sonatype.sh --version ${{ inputs.version }} $auto_publish_flag
env:
SONATYPE_USERNAME_TOKEN: ${{ secrets.SONATYPE_USERNAME_TOKEN }}
SONATYPE_PASSWORD_TOKEN: ${{ secrets.SONATYPE_PASSWORD_TOKEN }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
publish_to_bcr:
if: ${{ inputs.publish_to_bcr }}
needs: release
uses: bazel-contrib/publish-to-bcr/.github/workflows/publish.yaml@v1.1.0
with:
tag_name: ${{ inputs.version }}
registry_fork: j2cl-github-bot/bazel-central-registry
attest: true
author_name: j2cl-github-bot
author_email: j2cl-github-bot@google.com
# The PR is authored by a bot account, which requires team member approval. Therefore, we
# don't need to create it as a draft.
draft: false
permissions:
contents: write
id-token: write
attestations: write
secrets:
publish_token: ${{ secrets.BCR_PUBLISH_TOKEN }}