Skip to content

Commit

Permalink
Ensure npm credentials are set by publish_snapshot.sh (#422)
Browse files Browse the repository at this point in the history
## Summary:
We've seen Github action failures in the "Publish npm snapshot" job. This is because the npm auth info has not been provided (this is done automatically by the changesets Github Action, but we aren't using it here because we're publishing a snapshot. 

This PR mimics what the changesets Github Action [does](https://github.com/changesets/action/blob/8c3f5f5637a95a2327e78d5dabcf357978aedcbb/src/index.ts#L58..L85) by creating a `.npmrc` file with the credentials in it. 

Issue: "none"

## Test plan:

Create a new PR after landing this one.
Ensure that the "Publish npm snapshot" job succeeds.

Author: jeremywiebe

Reviewers: jeremywiebe, handeyeco

Required Reviewers:

Approved By: handeyeco

Checks: ✅ finish_coverage, ✅ Check for .changeset file (ubuntu-latest, 16.x), ✅ Publish npm snapshot (ubuntu-latest, 16.x), ✅ Lint, Flow, and Test (ubuntu-latest, 16.x), ✅ Extract i18n strings (ubuntu-latest, 16.x), ✅ Cypress Coverage (ubuntu-latest, 16.x), ✅ Jest Coverage (ubuntu-latest, 16.x), ✅ gerald, ✅ Check builds for changes in size (ubuntu-latest, 16.x)

Pull Request URL: #422
  • Loading branch information
jeremywiebe authored Mar 23, 2023
1 parent 05c0bca commit a18ef7e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 57 deletions.
2 changes: 2 additions & 0 deletions .changeset/yellow-garlics-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
145 changes: 88 additions & 57 deletions utils/publish-snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,63 +20,94 @@ ROOT="$MYPATH/.."

pushd "$ROOT"

if [ -z ${CI+CI_UNSET} ]; then
echo "CI environment variable is unset. Exiting!"
exit 1
fi

if [ -z ${GITHUB_EVENT_NAME+GITHUB_EVENT_NAME_UNSET} ]; then
echo "GITHUB_EVENT_NAME environment variable is unset. Exiting!"
exit 1
fi

if [ -z ${GITHUB_REF+GITHUB_REF_UNSET} ]; then
echo "GITHUB_REF environment variable is unset. Exiting!"
exit 1
fi

if [[
"$GITHUB_EVENT_NAME" != "workflow_dispatch" \
&& "$GITHUB_EVENT_NAME" != "pull_request"
]]; then
exit
fi

# Check if we need to do any work
# NOTE: changeset's --output flag has a bug where it always prefixes whatever
# you pass to it with `cwd` (the code does `path.join(cwd, outputParam)`). So
# we just allow it to write the file in our local dir (although I would prefer
# to use `mktemp`).
yarn changeset status --verbose --output changeset-status.json
# We use jq to check if the json outpu has changesets. If not, we exit the
# process (but not with a non-zero exit status because we don't want to cause
# the github action to exit with a failure status).
jq -e '.releases | length | if . > 0 then . else "No changesets found" | halt_error(1) end' \
< "changeset-status.json" || \
exit 0


echo "Running for $GITHUB_EVENT_NAME @ $GITHUB_REF"

# Example GITHUB_REF
# refs/pull/:prNumber/merge
if [[ "$GITHUB_REF" =~ refs/pull/([[:digit:]]+)/merge ]]; then
echo "Found PR #${BASH_REMATCH[1]}"
PR_NUMBER="PR${BASH_REMATCH[1]}"
else
echo "Pull Request number not found in ref. Exiting!"
exit 1
fi

# publish:ci steps
node "$ROOT/utils/pre-publish-check-ci.js"

if ! git diff --stat --exit-code HEAD; then
echo "Git repo is dirty. This is unexpected when running in CI."
echo "Please review the logs leading up to this error to figure out why " \
"the repo was touched."
exit 1
fi
verify_env() {
if [ -z ${CI+CI_UNSET} ]; then
echo "Required 'CI' environment variable is unset. Exiting!"
exit 1
fi

if [ -z ${GITHUB_EVENT_NAME+GITHUB_EVENT_NAME_UNSET} ]; then
echo "Required 'GITHUB_EVENT_NAME' environment variable is unset. Exiting!"
exit 1
fi

if [ -z ${GITHUB_REF+GITHUB_REF_UNSET} ]; then
echo "Required 'GITHUB_REF' environment variable is unset. Exiting!"
exit 1
fi

if [ -z ${NPM_TOKEN+NPM_TOKEN_UNSET} ]; then
echo "Required 'NPM_TOKEN' environment variable is unset. Exiting!"
exit 1
fi

if [[
"$GITHUB_EVENT_NAME" != "workflow_dispatch" \
&& "$GITHUB_EVENT_NAME" != "pull_request"
]]; then
exit
fi

# Example GITHUB_REF
# refs/pull/:prNumber/merge
if [[ "$GITHUB_REF" =~ refs/pull/([[:digit:]]+)/merge ]]; then
echo "Found PR #${BASH_REMATCH[1]}"
PR_NUMBER="PR${BASH_REMATCH[1]}"
else
echo "Pull Request number not found in ref. Exiting!"
exit 1
fi
}

check_for_changes() {
# Check if we need to do any work
# NOTE: changeset's --output flag has a bug where it always prefixes whatever
# you pass to it with `cwd` (the code does `path.join(cwd, outputParam)`). So
# we just allow it to write the file in our local dir (although I would prefer
# to use `mktemp`).
yarn changeset status --verbose --output changeset-status.json
# We use jq to check if the json outpu has changesets. If not, we exit the
# process (but not with a non-zero exit status because we don't want to cause
# the github action to exit with a failure status).
jq -e '.releases | length | if . > 0 then . else "No changesets found" | halt_error(1) end' \
< "changeset-status.json" || \
exit 0
}

pre_publish_check() {
node "$ROOT/utils/pre-publish-check-ci.js"

if ! git diff --stat --exit-code HEAD; then
echo "Git repo is dirty. This is unexpected when running in CI."
echo "Please review the logs leading up to this error to figure out why " \
"the repo was touched."
exit 1
fi
}

create_npmrc() {
# Inspiration: https://github.com/changesets/action/blob/8c3f5f5637a95a2327e78d5dabcf357978aedcbb/src/index.ts#L59..L85
echo "Checking for valid .npmrc"
if [[ -f "$HOME/.npmrc" ]]; then
if grep --silent "registry.npmjs.org" "$HOME/.npmrc"; then
return
fi
fi

# Append or create the file!
printf "\n//registry.npmjs.org/:_authToken=%s\n" "$NPM_TOKEN" >> "$HOME/.npmrc"
}

######
## Now we start the actual workflow of the script
##
## A similar set of steps to the 'publish:ci' package.json script
##

verify_env
check_for_changes
pre_publish_check
create_npmrc

yarn build
yarn extract-strings
Expand Down

0 comments on commit a18ef7e

Please sign in to comment.