Skip to content

Commit eed8d58

Browse files
committed
feat: refactor npm workflows to use download-artifact
This refactors the npm workflows to use the download-artifact GitHub Action. We had problems in the past with our download_artifact custom bash function. This also fixes an issue where we weren't downloading the correct artifacts when publishing beta and dev tags to npm.
1 parent 439f27c commit eed8d58

File tree

5 files changed

+85
-76
lines changed

5 files changed

+85
-76
lines changed

.github/workflows/ci.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,42 @@ jobs:
177177
name: npm-package
178178
path: ./package.tar.gz
179179

180+
npm:
181+
# the npm-package gets uploaded as an artifact in Build
182+
# so we need that to complete before this runs
183+
needs: build
184+
# This environment "npm" requires someone from
185+
# coder/code-server-reviewers to approve the PR before this job runs.
186+
environment: npm
187+
runs-on: ubuntu-latest
188+
steps:
189+
- uses: actions/checkout@v2
190+
191+
# NOTES@jsjoeio - trying out
192+
- uses: actions/download-artifact@v2
193+
id: download
194+
with:
195+
name: "npm-package"
196+
path: release-npm-package
197+
198+
- name:
199+
Set NPM_ENVIRONMENT
200+
# The way this logic works is it checks if the GitHub event is a push to the `main` branch
201+
# if so, we're in the staging environment (i.e. running in CI on merge into `main`)
202+
# otherwise it's running in CI on a PR event, meaning development environment
203+
# Source: https://kevsoft.net/2020/06/10/running-github-action-steps-and-jobs-only-on-push-to-master.html
204+
run: |
205+
if [[ $GITHUB_EVENT_NAME == 'push' && $GITHUB_REF == 'refs/heads/master' ]]; then
206+
echo "NPM_ENVIRONMENT=staging" >> "$GITHUB_ENV"
207+
else
208+
echo "NPM_ENVIRONMENT=development" >> "$GITHUB_ENV"
209+
fi
210+
- name: Run ./ci/steps/publish-npm.sh
211+
run: yarn publish:npm
212+
env:
213+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
214+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
215+
180216
# TODO: cache building yarn --production
181217
# possibly 2m30s of savings(?)
182218
# this requires refactoring our release scripts

.github/workflows/npm-beta.yaml

-29
This file was deleted.

.github/workflows/npm-brew.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ jobs:
1919
- name: Publish npm package and tag with "latest"
2020
run: yarn publish:npm
2121
env:
22-
ENVIRONMENT: "production"
2322
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2423
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
25-
NPM_TAG: "latest"
24+
NPM_ENVIRONMENT: "production"
2625

2726
homebrew:
2827
# The newest version of code-server needs to be available on npm when this runs

.github/workflows/npm-dev.yaml

-30
This file was deleted.

ci/steps/publish-npm.sh

+48-15
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ main() {
3030
# a developer to install this version with `yarn add code-server@beta`
3131
# "production" - this means we tag with `latest` (default), allowing
3232
# a developer to install this version with `yarn add code-server@latest`
33-
if ! is_env_var_set "ENVIRONMENT"; then
34-
echo "ENVIRONMENT is not set. Cannot determine npm tag without ENVIRONMENT."
33+
if ! is_env_var_set "NPM_ENVIRONMENT"; then
34+
echo "NPM_ENVIRONMENT is not set. Cannot determine npm tag without NPM_ENVIRONMENT."
3535
exit 1
3636
fi
3737

@@ -47,22 +47,37 @@ main() {
4747
exit 1
4848
fi
4949

50-
# We need TAG to know what to publish under on npm
51-
# Options are "latest", "beta", or "<pr number >"
52-
# See Environment comments above to know when each is used.
53-
if ! is_env_var_set "NPM_TAG"; then
54-
echo "NPM_TAG is not set. This is needed for tagging the npm release."
50+
# We use this to grab the PR_NUMBER
51+
if ! is_env_var_set "GITHUB_REF"; then
52+
echo "GITHUB_REF is not set. Are you running this locally? We rely on values provided by GitHub."
5553
exit 1
5654
fi
5755

58-
echo "using tag: $NPM_TAG"
56+
# We use this to grab the branch name
57+
if ! is_env_var_set "GITHUB_HEAD_REF"; then
58+
echo "GITHUB_HEAD_REF is not set. Are you running this locally? We rely on values provided by GitHub."
59+
exit 1
60+
fi
61+
62+
# We use this when setting NPM_VERSION
63+
if ! is_env_var_set "GITHUB_SHA"; then
64+
echo "GITHUB_SHA is not set. Are you running this locally? We rely on values provided by GitHub."
65+
exit 1
66+
fi
5967

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

65-
download_artifact npm-package ./release-npm-package
73+
# Note: if this runs on a push to main or a release workflow
74+
# There is no BRANCH so branch will be empty which is why
75+
# we set a default.
76+
# Source:https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
77+
BRANCH="${GITHUB_HEAD_REF-main}"
78+
79+
# NOTE@jsjoeio - this script assumes we have the artifact downloaded on disk
80+
# That happens in CI as a step before we run this.
6681
# https://github.com/actions/upload-artifact/issues/38
6782
tar -xzf release-npm-package/package.tar.gz
6883

@@ -74,22 +89,40 @@ main() {
7489
# We only need to run npm version for "development" and "staging".
7590
# This is because our release:prep script automatically bumps the version
7691
# in the package.json and we commit it as part of the release PR.
77-
if [[ "$ENVIRONMENT" == "production" ]]; then
92+
if [[ "$NPM_ENVIRONMENT" == "production" ]]; then
7893
NPM_VERSION="$VERSION"
94+
# This means the npm version will be published as "stable"
95+
# and installed when a user runs `yarn install code-server`
96+
NPM_TAG="latest"
7997
else
98+
COMMIT_SHA="$GITHUB_SHA"
8099
echo "Not a production environment"
81-
echo "Found environment: $ENVIRONMENT"
100+
echo "Found environment: $NPM_ENVIRONMENT"
82101
echo "Manually bumping npm version..."
83102

84-
if ! is_env_var_set "PR_NUMBER_AND_COMMIT_SHA"; then
85-
echo "PR_NUMBER_AND_COMMIT_SHA is not set. This is needed for setting the npm version in non-production environments."
86-
exit 1
103+
if [[ "$NPM_ENVIRONMENT" == "staging" ]]; then
104+
NPM_VERSION="$VERSION-beta-$COMMIT_SHA"
105+
# This means the npm version will be tagged with "beta"
106+
# and installed when a user runs `yarn install code-server@beta`
107+
NPM_TAG="beta"
108+
fi
109+
110+
if [[ "$NPM_ENVIRONMENT" == "development" ]]; then
111+
# Source: https://github.com/actions/checkout/issues/58#issuecomment-614041550
112+
PR_NUMBER=$(echo "$GITHUB_REF" | awk 'BEGIN { FS = "/" } ; { print $3 }')
113+
NPM_VERSION="$VERSION-$PR_NUMBER-$COMMIT_SHA"
114+
# This means the npm version will be tagged with "<pr number>"
115+
# and installed when a user runs `yarn install code-server@<pr number>`
116+
NPM_TAG="$PR_NUMBER"
87117
fi
88118

119+
echo "using tag: $NPM_TAG"
120+
89121
# We modify the version in the package.json
90122
# to be the current version + the PR number + commit SHA
123+
# or we use current version + beta + commit SHA
91124
# Example: "version": "4.0.1-4769-ad7b23cfe6ffd72914e34781ef7721b129a23040"
92-
NPM_VERSION="$VERSION-$PR_NUMBER_AND_COMMIT_SHA"
125+
# Example: "version": "4.0.1-beta-ad7b23cfe6ffd72914e34781ef7721b129a23040"
93126
pushd release
94127
# NOTE:@jsjoeio
95128
# I originally tried to use `yarn version` but ran into issues and abandoned it.

0 commit comments

Comments
 (0)