2020-05-08 15:08:30 +08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
main() {
|
|
|
|
cd "$(dirname "$0")/../.."
|
2020-05-12 05:08:22 +08:00
|
|
|
source ./ci/lib.sh
|
2022-01-22 07:28:56 +08:00
|
|
|
source ./ci/steps/steps-lib.sh
|
2020-05-08 15:08:30 +08:00
|
|
|
|
2022-01-22 07:28:56 +08:00
|
|
|
## Authentication tokens
|
|
|
|
# Needed to publish on NPM
|
|
|
|
if ! is_env_var_set "NPM_TOKEN"; then
|
|
|
|
echo "NPM_TOKEN is not set. Cannot publish to npm without credentials."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
## Publishing Information
|
|
|
|
# All the variables below are used to determine how we should publish
|
|
|
|
# the npm package. We also use this information for bumping the version.
|
|
|
|
# This is because npm won't publish your package unless it's a new version.
|
|
|
|
# i.e. for development, we bump the version to <current version>-<pr number>-<commit sha>
|
|
|
|
# example: "version": "4.0.1-4769-ad7b23cfe6ffd72914e34781ef7721b129a23040"
|
|
|
|
# We need the current package.json VERSION
|
|
|
|
if ! is_env_var_set "VERSION"; then
|
|
|
|
echo "VERSION is not set. Cannot publish to npm without VERSION."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-02-04 04:54:36 +08:00
|
|
|
# We use this to grab the PR_NUMBER
|
|
|
|
if ! is_env_var_set "GITHUB_REF"; then
|
|
|
|
echo "GITHUB_REF is not set. Are you running this locally? We rely on values provided by GitHub."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# We use this when setting NPM_VERSION
|
|
|
|
if ! is_env_var_set "GITHUB_SHA"; then
|
|
|
|
echo "GITHUB_SHA is not set. Are you running this locally? We rely on values provided by GitHub."
|
2022-01-22 07:28:56 +08:00
|
|
|
exit 1
|
2021-07-17 05:44:00 +08:00
|
|
|
fi
|
|
|
|
|
2022-02-04 04:54:36 +08:00
|
|
|
# We use this to determine the NPM_ENVIRONMENT
|
|
|
|
if ! is_env_var_set "GITHUB_EVENT_NAME"; then
|
|
|
|
echo "GITHUB_EVENT_NAME is not set. Are you running this locally? We rely on values provided by GitHub."
|
|
|
|
exit 1
|
|
|
|
fi
|
2022-01-22 07:28:56 +08:00
|
|
|
|
2022-03-16 07:59:40 +08:00
|
|
|
# Check that we're using at least v7 of npm CLI
|
|
|
|
if ! command -v jq &> /dev/null; then
|
|
|
|
echo "Couldn't find jq"
|
|
|
|
echo "We need this in order to modify the package.json for dev builds."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-01-22 07:28:56 +08:00
|
|
|
# This allows us to publish to npm in CI workflows
|
2020-05-08 15:08:30 +08:00
|
|
|
if [[ ${CI-} ]]; then
|
2021-06-29 00:36:55 +08:00
|
|
|
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
|
2020-05-08 15:08:30 +08:00
|
|
|
fi
|
|
|
|
|
2022-02-04 04:54:36 +08:00
|
|
|
## Environment
|
|
|
|
# This string is used to determine how we should tag the npm release.
|
|
|
|
# Environment can be one of three choices:
|
|
|
|
# "development" - this means we tag with the PR number, allowing
|
2022-04-14 01:15:21 +08:00
|
|
|
# a developer to install this version with `yarn add code-server@<pr-number>`
|
2022-02-04 04:54:36 +08:00
|
|
|
# "staging" - this means we tag with `beta`, allowing
|
2022-04-14 01:15:21 +08:00
|
|
|
# a developer to install this version with `yarn add code-server@beta`
|
2022-02-04 04:54:36 +08:00
|
|
|
# "production" - this means we tag with `latest` (default), allowing
|
2022-04-14 01:15:21 +08:00
|
|
|
# a developer to install this version with `yarn add code-server@latest`
|
2022-02-04 04:54:36 +08:00
|
|
|
if ! is_env_var_set "NPM_ENVIRONMENT"; then
|
2022-08-24 02:36:24 +08:00
|
|
|
echo "NPM_ENVIRONMENT is not set."
|
|
|
|
echo "Determining in script based on GITHUB environment variables."
|
2022-02-04 04:54:36 +08:00
|
|
|
|
|
|
|
if [[ "$GITHUB_EVENT_NAME" == 'push' && "$GITHUB_REF" == 'refs/heads/main' ]]; then
|
|
|
|
NPM_ENVIRONMENT="staging"
|
|
|
|
else
|
|
|
|
NPM_ENVIRONMENT="development"
|
|
|
|
fi
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
# NOTE@jsjoeio - this script assumes we have the artifact downloaded on disk
|
|
|
|
# That happens in CI as a step before we run this.
|
2020-05-12 14:59:03 +08:00
|
|
|
# https://github.com/actions/upload-artifact/issues/38
|
2020-05-19 01:48:45 +08:00
|
|
|
tar -xzf release-npm-package/package.tar.gz
|
2021-08-12 01:01:15 +08:00
|
|
|
|
2022-03-16 07:59:40 +08:00
|
|
|
# We use this to set the name of the package in the
|
|
|
|
# package.json
|
|
|
|
PACKAGE_NAME="code-server"
|
|
|
|
|
2022-01-22 07:28:56 +08:00
|
|
|
# NOTES:@jsjoeio
|
|
|
|
# We only need to run npm version for "development" and "staging".
|
|
|
|
# This is because our release:prep script automatically bumps the version
|
|
|
|
# in the package.json and we commit it as part of the release PR.
|
2022-02-04 04:54:36 +08:00
|
|
|
if [[ "$NPM_ENVIRONMENT" == "production" ]]; then
|
2022-01-22 07:28:56 +08:00
|
|
|
NPM_VERSION="$VERSION"
|
2022-02-04 04:54:36 +08:00
|
|
|
# This means the npm version will be published as "stable"
|
2022-04-14 01:15:21 +08:00
|
|
|
# and installed when a user runs `yarn install code-server`
|
2022-02-04 04:54:36 +08:00
|
|
|
NPM_TAG="latest"
|
2022-01-22 07:28:56 +08:00
|
|
|
else
|
2022-02-04 04:54:36 +08:00
|
|
|
COMMIT_SHA="$GITHUB_SHA"
|
2022-01-22 07:28:56 +08:00
|
|
|
|
2022-02-04 04:54:36 +08:00
|
|
|
if [[ "$NPM_ENVIRONMENT" == "staging" ]]; then
|
|
|
|
NPM_VERSION="$VERSION-beta-$COMMIT_SHA"
|
|
|
|
# This means the npm version will be tagged with "beta"
|
2022-04-14 01:15:21 +08:00
|
|
|
# and installed when a user runs `yarn install code-server@beta`
|
2022-02-04 04:54:36 +08:00
|
|
|
NPM_TAG="beta"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$NPM_ENVIRONMENT" == "development" ]]; then
|
|
|
|
# Source: https://github.com/actions/checkout/issues/58#issuecomment-614041550
|
|
|
|
PR_NUMBER=$(echo "$GITHUB_REF" | awk 'BEGIN { FS = "/" } ; { print $3 }')
|
|
|
|
NPM_VERSION="$VERSION-$PR_NUMBER-$COMMIT_SHA"
|
2022-03-16 07:59:40 +08:00
|
|
|
PACKAGE_NAME="@coder/code-server-pr"
|
2022-02-04 04:54:36 +08:00
|
|
|
# This means the npm version will be tagged with "<pr number>"
|
2022-04-14 01:15:21 +08:00
|
|
|
# and installed when a user runs `yarn install code-server@<pr number>`
|
2022-02-04 04:54:36 +08:00
|
|
|
NPM_TAG="$PR_NUMBER"
|
2022-01-22 07:28:56 +08:00
|
|
|
fi
|
|
|
|
|
2022-08-24 02:36:24 +08:00
|
|
|
echo "- tag: $NPM_TAG"
|
|
|
|
echo "- version: $NPM_VERSION"
|
|
|
|
echo "- package name: $PACKAGE_NAME"
|
|
|
|
echo "- npm environment: $NPM_ENVIRONMENT"
|
2022-02-04 04:54:36 +08:00
|
|
|
|
2022-01-22 07:28:56 +08:00
|
|
|
# We modify the version in the package.json
|
|
|
|
# to be the current version + the PR number + commit SHA
|
2022-02-04 04:54:36 +08:00
|
|
|
# or we use current version + beta + commit SHA
|
2022-01-22 07:28:56 +08:00
|
|
|
# Example: "version": "4.0.1-4769-ad7b23cfe6ffd72914e34781ef7721b129a23040"
|
2022-02-04 04:54:36 +08:00
|
|
|
# Example: "version": "4.0.1-beta-ad7b23cfe6ffd72914e34781ef7721b129a23040"
|
2022-01-22 07:28:56 +08:00
|
|
|
pushd release
|
2022-03-16 07:59:40 +08:00
|
|
|
# NOTE@jsjoeio
|
2022-01-22 07:28:56 +08:00
|
|
|
# I originally tried to use `yarn version` but ran into issues and abandoned it.
|
|
|
|
npm version "$NPM_VERSION"
|
2022-03-16 07:59:40 +08:00
|
|
|
# NOTE@jsjoeio
|
|
|
|
# Use the development package name
|
|
|
|
# This is so we don't clutter the code-server versions on npm
|
|
|
|
# with development versions.
|
2022-03-31 07:11:11 +08:00
|
|
|
# jq can't edit in place so we must store in memory and echo
|
|
|
|
local contents
|
|
|
|
contents="$(jq ".name |= \"$PACKAGE_NAME\"" package.json)"
|
|
|
|
echo "${contents}" > package.json
|
2022-01-22 07:28:56 +08:00
|
|
|
popd
|
|
|
|
fi
|
|
|
|
|
2022-08-24 02:36:24 +08:00
|
|
|
# NOTE@jsjoeio
|
2022-01-22 07:28:56 +08:00
|
|
|
# We need to make sure we haven't already published the version.
|
2022-08-24 02:36:24 +08:00
|
|
|
# If we get error, continue with script because we want to publish
|
|
|
|
# If version is valid, we check if we're publishing the same one
|
2022-01-22 07:28:56 +08:00
|
|
|
local hasVersion
|
2022-08-24 02:36:24 +08:00
|
|
|
if hasVersion=$(npm view "$PACKAGE_NAME@$NPM_VERSION" version 2> /dev/null) && [[ $hasVersion == "$NPM_VERSION" ]]; then
|
|
|
|
echo "$NPM_VERSION is already published under $PACKAGE_NAME"
|
2022-01-22 07:28:56 +08:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2022-03-16 07:59:40 +08:00
|
|
|
# NOTE@jsjoeio
|
|
|
|
# Since the dev builds are scoped to @coder
|
|
|
|
# We pass --access public to ensure npm knows it's not private.
|
|
|
|
yarn publish --non-interactive release --tag "$NPM_TAG" --access public
|
2020-05-08 15:08:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|