From d52f2daf996e5fb00be90550e1257648b9b8d519 Mon Sep 17 00:00:00 2001 From: Sanjay Vasandani Date: Mon, 1 Jul 2024 11:06:56 -0700 Subject: [PATCH] Publish common library as Maven artifact on release. --- .github/workflows/publish-maven-artifacts.yml | 99 +++++++++++++++++++ build/maven/BUILD.bazel | 0 build/maven/defs.bzl | 20 ++++ .../org/wfanet/measurement/common/BUILD.bazel | 13 +++ 4 files changed, 132 insertions(+) create mode 100644 .github/workflows/publish-maven-artifacts.yml create mode 100644 build/maven/BUILD.bazel create mode 100644 build/maven/defs.bzl diff --git a/.github/workflows/publish-maven-artifacts.yml b/.github/workflows/publish-maven-artifacts.yml new file mode 100644 index 000000000..9dcfce7f1 --- /dev/null +++ b/.github/workflows/publish-maven-artifacts.yml @@ -0,0 +1,99 @@ +# Copyright 2024 The Cross-Media Measurement Authors +# +# 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 +# +# http://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. + +on: + release: + workflow_dispatch: + inputs: + base-version: + description: Base artifact version, which will be suffixed with "-SNAPSHOT" + required: true + pull_request: + types: [opened, synchronize, edited] + +jobs: + publish-artifacts: + name: Publish Maven artifacts + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + + - id: get-artifact-version + env: + BASE_VERSION: 0.86.0-SNAPSHOT + run: | + declare artifact_version + if [[ "$GITHUB_EVENT_NAME" == 'release' ]]; then + artifact_version="${GITHUB_REF_NAME#v}" + else + artifact_version="${BASE_VERSION}-SNAPSHOT" + fi + + echo "artifact-version=${artifact_version}" >> "$GITHUB_OUTPUT" + + - name: Set up Bazel + run: | + mkdir -p ~/.local/bin + bazelisk_path="$(which bazelisk)" + ln -s "${bazelisk_path}" ~/.local/bin/bazel + + - name: Set up Buildozer + run: | + cd ~/.local/bin + curl -L -o buildozer https://github.com/bazelbuild/buildtools/releases/download/v7.1.2/buildozer-linux-amd64 + echo '8d5c459ab21b411b8be059a8bdf59f0d3eabf9dff943d5eccb80e36e525cc09d buildozer' > buildozer.sha256 + sha256sum --check buildozer.sha256 + chmod +x buildozer + + - name: Write ~/.bazelrc + run: | + echo 'common --config=ci' >> ~/.bazelrc + + - name: Get Bazel cache params + id: get-cache-params + uses: world-federation-of-advertisers/actions/bazel-get-cache-params@v2 + with: + cache-version: 1 + + - name: Restore Bazel cache + uses: actions/cache/restore@v3 + with: + path: ${{ steps.get-cache-params.outputs.cache-path }} + key: ${{ steps.get-cache-params.outputs.cache-key }} + restore-keys: |- + ${{ steps.get-cache-params.outputs.restore-key }} + + # Patch MODULE.bazel and MODULE.bazel.lock to specify version. + # TODO(bazelbuild/bazel#22919): Use alternative mechanism when available. + - name: Patch module version + env: + ARTIFACT_VERSION: ${{ steps.get-artifact-version.outputs.artifact-version }} + run: | + # Make sure lockfile is still valid before changing anything. + bazel mod deps --lockfile_mode=error + + # Update MODULE.bazel. + buildozer "set version $ARTIFACT_VERSION" //MODULE.bazel:%module + + # Update lockfile to pick up changes. + bazel mod deps --lockfile_mode=update + + - name: Publish artifacts + env: + MAVEN_REPO: https://maven.pkg.github.com/${{ github.repository }} + # TODO(bazelbuild/rules_jvm_external#1186): Use GITHUB_TOKEN instead. + MAVEN_USER: ${{ vars.MAVEN_USER }} + MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }} + run: | + bazel query "kind('^maven_publish', //src/main/...)" | xargs bazel run diff --git a/build/maven/BUILD.bazel b/build/maven/BUILD.bazel new file mode 100644 index 000000000..e69de29bb diff --git a/build/maven/defs.bzl b/build/maven/defs.bzl new file mode 100644 index 000000000..197c6fcd7 --- /dev/null +++ b/build/maven/defs.bzl @@ -0,0 +1,20 @@ +# Copyright 2024 The Cross-Media Measurement Authors +# +# 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 +# +# http://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. + +"""Defs for exporting to Maven.""" + +# buildifier: disable=unnamed-macro +def artifact_version(): + """Returns the artifact version for libraries defined in this module.""" + return native.module_version() or "0.0.0-SNAPSHOT" diff --git a/src/main/kotlin/org/wfanet/measurement/common/BUILD.bazel b/src/main/kotlin/org/wfanet/measurement/common/BUILD.bazel index f5e4bf2b9..4d6e061ea 100644 --- a/src/main/kotlin/org/wfanet/measurement/common/BUILD.bazel +++ b/src/main/kotlin/org/wfanet/measurement/common/BUILD.bazel @@ -1,10 +1,15 @@ +load("@rules_jvm_external//:defs.bzl", "maven_export") load("@wfa_rules_kotlin_jvm//kotlin:defs.bzl", "kt_jvm_library") +load("//build/maven:defs.bzl", "artifact_version") package(default_visibility = ["//visibility:public"]) +MAVEN_COORDINATES = "org.wfanet.measurement:common:" + artifact_version() + kt_jvm_library( name = "common", srcs = glob(["*.kt"]), + tags = ["maven_coordinates=" + MAVEN_COORDINATES], deps = [ "//imports/java/com/github/benmanes/caffeine", "//imports/java/com/google/common:guava", @@ -19,3 +24,11 @@ kt_jvm_library( "//imports/kotlin/kotlinx/coroutines:core", ], ) + +maven_export( + name = "common_maven", + lib_name = "common", + maven_coordinates = MAVEN_COORDINATES, + tags = ["no-javadocs"], + visibility = ["//visibility:private"], +)