Skip to content

refactor(ci): fix npm workflows #4797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,42 @@ jobs:
name: npm-package
path: ./package.tar.gz

npm:
# the npm-package gets uploaded as an artifact in Build
# so we need that to complete before this runs
needs: build
# This environment "npm" requires someone from
# coder/code-server-reviewers to approve the PR before this job runs.
environment: npm
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

# NOTES@jsjoeio - trying out
- uses: actions/download-artifact@v2
id: download
with:
name: "npm-package"
path: release-npm-package

- name:
Set NPM_ENVIRONMENT
# The way this logic works is it checks if the GitHub event is a push to the `main` branch
# if so, we're in the staging environment (i.e. running in CI on merge into `main`)
# otherwise it's running in CI on a PR event, meaning development environment
# Source: https://kevsoft.net/2020/06/10/running-github-action-steps-and-jobs-only-on-push-to-master.html
run: |
if [[ $GITHUB_EVENT_NAME == 'push' && $GITHUB_REF == 'refs/heads/master' ]]; then
echo "NPM_ENVIRONMENT=staging" >> "$GITHUB_ENV"
else
echo "NPM_ENVIRONMENT=development" >> "$GITHUB_ENV"
fi
- name: Run ./ci/steps/publish-npm.sh
run: yarn publish:npm
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

# TODO: cache building yarn --production
# possibly 2m30s of savings(?)
# this requires refactoring our release scripts
Expand Down
29 changes: 0 additions & 29 deletions .github/workflows/npm-beta.yaml

This file was deleted.

3 changes: 1 addition & 2 deletions .github/workflows/npm-brew.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ jobs:
- name: Publish npm package and tag with "latest"
run: yarn publish:npm
env:
ENVIRONMENT: "production"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_TAG: "latest"
NPM_ENVIRONMENT: "production"

homebrew:
# The newest version of code-server needs to be available on npm when this runs
Expand Down
30 changes: 0 additions & 30 deletions .github/workflows/npm-dev.yaml

This file was deleted.

51 changes: 36 additions & 15 deletions ci/steps/publish-npm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ main() {
# 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 "ENVIRONMENT"; then
echo "ENVIRONMENT is not set. Cannot determine npm tag without ENVIRONMENT."
if ! is_env_var_set "NPM_ENVIRONMENT"; then
echo "NPM_ENVIRONMENT is not set. Cannot determine npm tag without NPM_ENVIRONMENT."
exit 1
fi

Expand All @@ -47,22 +47,25 @@ main() {
exit 1
fi

# We need TAG to know what to publish under on npm
# Options are "latest", "beta", or "<pr number >"
# See Environment comments above to know when each is used.
if ! is_env_var_set "NPM_TAG"; then
echo "NPM_TAG is not set. This is needed for tagging the npm release."
# 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

echo "using tag: $NPM_TAG"
# 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."
exit 1
fi

# This allows us to publish to npm in CI workflows
if [[ ${CI-} ]]; then
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
fi

download_artifact npm-package ./release-npm-package
# NOTE@jsjoeio - this script assumes we have the artifact downloaded on disk
# That happens in CI as a step before we run this.
# https://github.com/actions/upload-artifact/issues/38
tar -xzf release-npm-package/package.tar.gz

Expand All @@ -74,22 +77,40 @@ main() {
# 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.
if [[ "$ENVIRONMENT" == "production" ]]; then
if [[ "$NPM_ENVIRONMENT" == "production" ]]; then
NPM_VERSION="$VERSION"
# This means the npm version will be published as "stable"
# and installed when a user runs `yarn install code-server`
NPM_TAG="latest"
else
COMMIT_SHA="$GITHUB_SHA"
echo "Not a production environment"
echo "Found environment: $ENVIRONMENT"
echo "Found environment: $NPM_ENVIRONMENT"
echo "Manually bumping npm version..."

if ! is_env_var_set "PR_NUMBER_AND_COMMIT_SHA"; then
echo "PR_NUMBER_AND_COMMIT_SHA is not set. This is needed for setting the npm version in non-production environments."
exit 1
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"
fi

echo "using tag: $NPM_TAG"

# We modify the version in the package.json
# to be the current version + the PR number + commit SHA
# or we use current version + beta + commit SHA
# Example: "version": "4.0.1-4769-ad7b23cfe6ffd72914e34781ef7721b129a23040"
NPM_VERSION="$VERSION-$PR_NUMBER_AND_COMMIT_SHA"
# Example: "version": "4.0.1-beta-ad7b23cfe6ffd72914e34781ef7721b129a23040"
pushd release
# NOTE:@jsjoeio
# I originally tried to use `yarn version` but ran into issues and abandoned it.
Expand Down