2020-05-08 07:08:30 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
main() {
|
|
|
|
cd "$(dirname "$0")/../.."
|
2020-05-11 21:08:22 +00:00
|
|
|
source ./ci/lib.sh
|
2022-01-21 23:28:56 +00:00
|
|
|
source ./ci/steps/steps-lib.sh
|
2020-05-08 07:08:30 +00:00
|
|
|
|
2022-01-21 23:28:56 +00: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
|
|
|
|
|
|
|
|
# NOTE@jsjoeio - only needed if we use the download_artifact
|
|
|
|
# because we talk to the GitHub API.
|
|
|
|
# Needed to use GitHub API
|
|
|
|
if ! is_env_var_set "GITHUB_TOKEN"; then
|
|
|
|
echo "GITHUB_TOKEN is not set. Cannot download npm release artifact without GitHub 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 20:54:36 +00: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-21 23:28:56 +00:00
|
|
|
exit 1
|
2021-07-16 21:44:00 +00:00
|
|
|
fi
|
|
|
|
|
2022-02-03 20:54:36 +00: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-21 23:28:56 +00:00
|
|
|
|
|
|
|
# This allows us to publish to npm in CI workflows
|
2020-05-08 07:08:30 +00:00
|
|
|
if [[ ${CI-} ]]; then
|
2021-06-28 16:36:55 +00:00
|
|
|
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
|
2020-05-08 07:08:30 +00:00
|
|
|
fi
|
|
|
|
|
2022-02-03 20:54:36 +00: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
|
|
|
|
# a developer to install this version with `yarn add code-server@<pr-number>`
|
|
|
|
# "staging" - this means we tag with `beta`, allowing
|
|
|
|
# a developer to install this version with `yarn add code-server@beta`
|
|
|
|
# "production" - this means we tag with `latest` (default), allowing
|
|
|
|
# a developer to install this version with `yarn add code-server@latest`
|
|
|
|
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 06:59:03 +00:00
|
|
|
# https://github.com/actions/upload-artifact/issues/38
|
2020-05-18 17:48:45 +00:00
|
|
|
tar -xzf release-npm-package/package.tar.gz
|
2021-08-11 17:01:15 +00:00
|
|
|
|
|
|
|
# Ignore symlink when publishing npm package
|
2022-02-01 16:45:19 +00:00
|
|
|
# See: https://github.com/coder/code-server/pull/3935
|
2021-08-11 17:01:15 +00:00
|
|
|
echo "node_modules.asar" > release/.npmignore
|
2022-01-21 23:28:56 +00: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 20:54:36 +00:00
|
|
|
if [[ "$NPM_ENVIRONMENT" == "production" ]]; then
|
2022-01-21 23:28:56 +00:00
|
|
|
NPM_VERSION="$VERSION"
|
2022-02-03 20:54:36 +00:00
|
|
|
# This means the npm version will be published as "stable"
|
|
|
|
# and installed when a user runs `yarn install code-server`
|
|
|
|
NPM_TAG="latest"
|
2022-01-21 23:28:56 +00:00
|
|
|
else
|
2022-02-03 20:54:36 +00:00
|
|
|
COMMIT_SHA="$GITHUB_SHA"
|
2022-01-21 23:28:56 +00:00
|
|
|
echo "Not a production environment"
|
2022-02-03 20:54:36 +00:00
|
|
|
echo "Found environment: $NPM_ENVIRONMENT"
|
2022-01-21 23:28:56 +00:00
|
|
|
echo "Manually bumping npm version..."
|
|
|
|
|
2022-02-03 20:54:36 +00:00
|
|
|
if [[ "$NPM_ENVIRONMENT" == "staging" ]]; then
|
|
|
|
NPM_VERSION="$VERSION-beta-$COMMIT_SHA"
|
|
|
|
# This means the npm version will be tagged with "beta"
|
|
|
|
# and installed when a user runs `yarn install code-server@beta`
|
|
|
|
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"
|
|
|
|
# This means the npm version will be tagged with "<pr number>"
|
|
|
|
# and installed when a user runs `yarn install code-server@<pr number>`
|
|
|
|
NPM_TAG="$PR_NUMBER"
|
2022-01-21 23:28:56 +00:00
|
|
|
fi
|
|
|
|
|
2022-02-03 20:54:36 +00:00
|
|
|
echo "using tag: $NPM_TAG"
|
|
|
|
|
2022-01-21 23:28:56 +00:00
|
|
|
# We modify the version in the package.json
|
|
|
|
# to be the current version + the PR number + commit SHA
|
2022-02-03 20:54:36 +00:00
|
|
|
# or we use current version + beta + commit SHA
|
2022-01-21 23:28:56 +00:00
|
|
|
# Example: "version": "4.0.1-4769-ad7b23cfe6ffd72914e34781ef7721b129a23040"
|
2022-02-03 20:54:36 +00:00
|
|
|
# Example: "version": "4.0.1-beta-ad7b23cfe6ffd72914e34781ef7721b129a23040"
|
2022-01-21 23:28:56 +00:00
|
|
|
pushd release
|
|
|
|
# NOTE:@jsjoeio
|
|
|
|
# I originally tried to use `yarn version` but ran into issues and abandoned it.
|
|
|
|
npm version "$NPM_VERSION"
|
|
|
|
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
|
|
|
|
|
|
|
|
yarn publish --non-interactive release --tag "$NPM_TAG"
|
2020-05-08 07:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|