2020-05-08 09:08:30 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
main() {
|
|
|
|
cd "$(dirname "$0")/../.."
|
2020-05-11 23:08:22 +02:00
|
|
|
source ./ci/lib.sh
|
2022-01-22 00:28:56 +01:00
|
|
|
source ./ci/steps/steps-lib.sh
|
2020-05-08 09:08:30 +02:00
|
|
|
|
2022-01-22 00:28:56 +01: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-03 21:54:36 +01: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 00:28:56 +01:00
|
|
|
exit 1
|
2021-07-16 23:44:00 +02:00
|
|
|
fi
|
|
|
|
|
2022-02-03 21:54:36 +01: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 00:28:56 +01:00
|
|
|
|
2022-03-16 00:59:40 +01: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 00:28:56 +01:00
|
|
|
# This allows us to publish to npm in CI workflows
|
2020-05-08 09:08:30 +02:00
|
|
|
if [[ ${CI-} ]]; then
|
2021-06-28 18:36:55 +02:00
|
|
|
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
|
2020-05-08 09:08:30 +02:00
|
|
|
fi
|
|
|
|
|
2022-02-03 21:54:36 +01: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-03-29 19:33:56 +02:00
|
|
|
# a developer to install this version with `npm install code-server@<pr-number>`
|
2022-02-03 21:54:36 +01:00
|
|
|
# "staging" - this means we tag with `beta`, allowing
|
2022-03-29 19:33:56 +02:00
|
|
|
# a developer to install this version with `npm install code-server@beta`
|
2022-02-03 21:54:36 +01:00
|
|
|
# "production" - this means we tag with `latest` (default), allowing
|
2022-03-29 19:33:56 +02:00
|
|
|
# a developer to install this version with `npm install code-server@latest`
|
2022-02-03 21:54:36 +01:00
|
|
|
if ! is_env_var_set "NPM_ENVIRONMENT"; then
|
|
|
|
echo "NPM_ENVIRONMENT is not set. Determining in script based on GITHUB environment variables."
|
|
|
|
|
|
|
|
if [[ "$GITHUB_EVENT_NAME" == 'push' && "$GITHUB_REF" == 'refs/heads/main' ]]; then
|
|
|
|
NPM_ENVIRONMENT="staging"
|
|
|
|
else
|
|
|
|
NPM_ENVIRONMENT="development"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Using npm environment: $NPM_ENVIRONMENT"
|
|
|
|
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 08:59:03 +02:00
|
|
|
# https://github.com/actions/upload-artifact/issues/38
|
2020-05-18 19:48:45 +02:00
|
|
|
tar -xzf release-npm-package/package.tar.gz
|
2021-08-11 19:01:15 +02:00
|
|
|
|
|
|
|
# Ignore symlink when publishing npm package
|
2022-02-01 17:45:19 +01:00
|
|
|
# See: https://github.com/coder/code-server/pull/3935
|
2021-08-11 19:01:15 +02:00
|
|
|
echo "node_modules.asar" > release/.npmignore
|
2022-01-22 00:28:56 +01:00
|
|
|
|
2022-03-16 00:59:40 +01:00
|
|
|
# We use this to set the name of the package in the
|
|
|
|
# package.json
|
|
|
|
PACKAGE_NAME="code-server"
|
|
|
|
|
2022-01-22 00:28:56 +01: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-03 21:54:36 +01:00
|
|
|
if [[ "$NPM_ENVIRONMENT" == "production" ]]; then
|
2022-01-22 00:28:56 +01:00
|
|
|
NPM_VERSION="$VERSION"
|
2022-02-03 21:54:36 +01:00
|
|
|
# This means the npm version will be published as "stable"
|
2022-03-29 19:33:56 +02:00
|
|
|
# and installed when a user runs `npm install code-server`
|
2022-02-03 21:54:36 +01:00
|
|
|
NPM_TAG="latest"
|
2022-01-22 00:28:56 +01:00
|
|
|
else
|
2022-02-03 21:54:36 +01:00
|
|
|
COMMIT_SHA="$GITHUB_SHA"
|
2022-01-22 00:28:56 +01:00
|
|
|
echo "Not a production environment"
|
2022-02-03 21:54:36 +01:00
|
|
|
echo "Found environment: $NPM_ENVIRONMENT"
|
2022-01-22 00:28:56 +01:00
|
|
|
echo "Manually bumping npm version..."
|
|
|
|
|
2022-02-03 21:54:36 +01:00
|
|
|
if [[ "$NPM_ENVIRONMENT" == "staging" ]]; then
|
|
|
|
NPM_VERSION="$VERSION-beta-$COMMIT_SHA"
|
|
|
|
# This means the npm version will be tagged with "beta"
|
2022-03-29 19:33:56 +02:00
|
|
|
# and installed when a user runs `npm install code-server@beta`
|
2022-02-03 21:54:36 +01: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 00:59:40 +01:00
|
|
|
PACKAGE_NAME="@coder/code-server-pr"
|
2022-02-03 21:54:36 +01:00
|
|
|
# This means the npm version will be tagged with "<pr number>"
|
2022-03-29 19:33:56 +02:00
|
|
|
# and installed when a user runs `npm install code-server@<pr number>`
|
2022-02-03 21:54:36 +01:00
|
|
|
NPM_TAG="$PR_NUMBER"
|
2022-01-22 00:28:56 +01:00
|
|
|
fi
|
|
|
|
|
2022-02-03 21:54:36 +01:00
|
|
|
echo "using tag: $NPM_TAG"
|
2022-03-16 00:59:40 +01:00
|
|
|
echo "using package name: $PACKAGE_NAME"
|
2022-02-03 21:54:36 +01:00
|
|
|
|
2022-01-22 00:28:56 +01:00
|
|
|
# We modify the version in the package.json
|
|
|
|
# to be the current version + the PR number + commit SHA
|
2022-02-03 21:54:36 +01:00
|
|
|
# or we use current version + beta + commit SHA
|
2022-01-22 00:28:56 +01:00
|
|
|
# Example: "version": "4.0.1-4769-ad7b23cfe6ffd72914e34781ef7721b129a23040"
|
2022-02-03 21:54:36 +01:00
|
|
|
# Example: "version": "4.0.1-beta-ad7b23cfe6ffd72914e34781ef7721b129a23040"
|
2022-01-22 00:28:56 +01:00
|
|
|
pushd release
|
2022-03-16 00:59:40 +01:00
|
|
|
# NOTE@jsjoeio
|
2022-01-22 00:28:56 +01:00
|
|
|
# I originally tried to use `yarn version` but ran into issues and abandoned it.
|
|
|
|
npm version "$NPM_VERSION"
|
2022-03-16 00:59:40 +01: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 01:11:11 +02: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 00:28:56 +01:00
|
|
|
popd
|
|
|
|
fi
|
|
|
|
|
|
|
|
# We need to make sure we haven't already published the version.
|
|
|
|
# This is because npm view won't exit with non-zero so we have
|
|
|
|
# to check the output.
|
|
|
|
local hasVersion
|
|
|
|
hasVersion=$(npm view "code-server@$NPM_VERSION" version)
|
|
|
|
if [[ $hasVersion == "$NPM_VERSION" ]]; then
|
|
|
|
echo "$NPM_VERSION is already published"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2022-03-16 00:59:40 +01: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 09:08:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|