diff --git a/.github/scripts/publish_package.sh b/.github/scripts/publish_package.sh index d4a7bb4e0b..aaaaad598f 100755 --- a/.github/scripts/publish_package.sh +++ b/.github/scripts/publish_package.sh @@ -14,10 +14,107 @@ # See the License for the specific language governing permissions and # limitations under the License. -set -e -set -u +set -euo pipefail +umask 077 -echo "//wombat-dressing-room.appspot.com/:_authToken=${NPM_AUTH_TOKEN}" >> .npmrc +readonly AR_PROJECT="oss-exit-gate-prod" +readonly AR_LOCATION="us" +readonly AR_REPO="firebase-admin-node--npm" +readonly PACKAGE_NAME="firebase-admin" +readonly REGISTRY_HOST="us-npm.pkg.dev/${AR_PROJECT}/${AR_REPO}/" +readonly REGISTRY_URL="https://${REGISTRY_HOST}" +readonly MANIFEST_BUCKET="gs://oss-exit-gate-prod-projects-bucket/firebase-admin-node/npm/manifests" -readonly UNPREFIXED_VERSION=`echo ${VERSION} | cut -c 2-` -npm publish ./dist/firebase-admin-${UNPREFIXED_VERSION}.tgz --registry https://wombat-dressing-room.appspot.com +if [ -z "${VERSION:-}" ]; then + echo "Error: VERSION environment variable is not set." >&2 + exit 1 +fi + +readonly UNPREFIXED_VERSION="${VERSION#v}" +if [ -z "${UNPREFIXED_VERSION}" ]; then + echo "Error: empty version derived from VERSION='${VERSION}'." >&2 + exit 1 +fi + +readonly TARBALL="./dist/firebase-admin-${UNPREFIXED_VERSION}.tgz" +if [ ! -f "${TARBALL}" ]; then + echo "Error: expected pre-packed tarball not found at ${TARBALL}" >&2 + exit 1 +fi + +# Defense-in-depth: ensure no WIF credentials or staging metadata were packed into the tarball. +tar_entries="$(tar -tzf "${TARBALL}")" +if grep -Eq '(^|/)(gha-creds-[^/]+\.json|\.npmrc|manifest\.json)$' <<< "${tar_entries}"; then + echo "Error: forbidden credential or staging file detected inside ${TARBALL}" >&2 + exit 1 +fi + +readonly TEMP_DIR="${RUNNER_TEMP:-/tmp}" +readonly RUN_ID="${GITHUB_RUN_ID:-manual}" +readonly RUN_ATTEMPT="${GITHUB_RUN_ATTEMPT:-1}" +readonly NPMRC_PATH="${TEMP_DIR}/exit-gate-${RUN_ID}-${RUN_ATTEMPT}.npmrc" +readonly MANIFEST_PATH="${TEMP_DIR}/manifest-${RUN_ID}-${RUN_ATTEMPT}.json" +trap 'rm -f "${NPMRC_PATH}" "${MANIFEST_PATH}"' EXIT + +# Version-scoped cleanup: delete only a colliding version from a prior failed attempt +# without touching other versions or swallowing API/auth errors. +existing_pkg="$(gcloud artifacts packages list \ + --project="${AR_PROJECT}" \ + --location="${AR_LOCATION}" \ + --repository="${AR_REPO}" \ + --format=json | jq -r --arg pkg "${PACKAGE_NAME}" \ + '.[] | select(.name == $pkg or (.name | endswith("/packages/" + $pkg))) | .name')" + +if [ -n "${existing_pkg}" ]; then + existing_ver="$(gcloud artifacts versions list \ + --project="${AR_PROJECT}" \ + --location="${AR_LOCATION}" \ + --repository="${AR_REPO}" \ + --package="${PACKAGE_NAME}" \ + --format=json | jq -r --arg ver "${UNPREFIXED_VERSION}" \ + '.[] | select(.name == $ver or (.name | endswith("/versions/" + $ver))) | .name')" + if [ -n "${existing_ver}" ]; then + gcloud artifacts versions delete "${UNPREFIXED_VERSION}" \ + --project="${AR_PROJECT}" \ + --location="${AR_LOCATION}" \ + --repository="${AR_REPO}" \ + --package="${PACKAGE_NAME}" \ + --delete-tags \ + --quiet + fi +fi + +ACCESS_TOKEN="$(gcloud auth print-access-token)" +if [ -z "${ACCESS_TOKEN}" ]; then + echo "Error: failed to obtain OAuth access token from gcloud." >&2 + exit 1 +fi +echo "::add-mask::${ACCESS_TOKEN}" + +cat < "${NPMRC_PATH}" +registry=${REGISTRY_URL} +//${REGISTRY_HOST}:_authToken=${ACCESS_TOKEN} +//${REGISTRY_HOST}:always-auth=true +NPMRC + +NPM_CONFIG_USERCONFIG="${NPMRC_PATH}" npm publish "${TARBALL}" \ + --registry="${REGISTRY_URL}" \ + --provenance=false + +jq -n \ + --arg pkg "${PACKAGE_NAME}" \ + --arg ver "${UNPREFIXED_VERSION}" \ + '{ + publish_all: false, + publishing_groups: [ + { + packages: [ + { name: $pkg, version: $ver } + ] + } + ] + }' > "${MANIFEST_PATH}" + +gcloud storage cp --if-generation-match=0 \ + "${MANIFEST_PATH}" \ + "${MANIFEST_BUCKET}/manifest-${RUN_ID}-${RUN_ATTEMPT}.json" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 773c3eb57f..51f174dafd 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -98,6 +98,9 @@ jobs: publish_release: needs: stage_release + concurrency: + group: ${{ github.workflow }}-publish-${{ github.ref }} + cancel-in-progress: false # Check whether the release should be published. We publish only when the trigger is # 1. a push (merge) @@ -111,6 +114,7 @@ jobs: environment: Release permissions: contents: write + id-token: write steps: - name: Checkout source for publish @@ -147,8 +151,15 @@ jobs: --title "Firebase Admin Node.js SDK $RELEASE_VER" \ --notes "$RELEASE_BODY" - - name: Publish to NPM + - name: Authenticate to Google Cloud (OSS Exit Gate) + uses: google-github-actions/auth@7c6bc770dae815cd3e89ee6cdf493a5fab2cc093 # v3.0.0 + with: + workload_identity_provider: 'projects/305452601764/locations/global/workloadIdentityPools/builders/providers/github' + + - name: Set up Cloud SDK + uses: google-github-actions/setup-gcloud@aa5489c8933f4cc7a4f7d45035b3b1440c9c10db # v3.0.1 + + - name: Publish to NPM via OSS Exit Gate run: ./.github/scripts/publish_package.sh env: - NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} VERSION: ${{ steps.preflight.outputs.version }}