Skip to content

Commit ea161c2

Browse files
committed
refactor: modify download_artifact logic
This updates our `download_artifact` logic in `lib.sh` to allow for more flexibility. Now we pass it an environment arg (staging, development, production) and an optional branch. This allows us to download artifacts from different runs rather than only on release branches.
1 parent d2e6ab2 commit ea161c2

File tree

6 files changed

+105
-37
lines changed

6 files changed

+105
-37
lines changed

.github/workflows/ci.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,13 @@ jobs:
191191
- name: Run ./ci/steps/publish-npm.sh
192192
run: yarn publish:npm
193193
env:
194+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
195+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
194196
# The way this logic works is it checks if the GitHub event is a push to the `main` branch
195197
# if so, we're in the staging environment (i.e. running in CI on merge into `main`)
196198
# otherwise it's running in CI on a PR event, meaning development environment
197199
# Source: https://kevsoft.net/2020/06/10/running-github-action-steps-and-jobs-only-on-push-to-master.html
198-
ENVIRONMENT: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && "staging" || "development" }}
199-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
200-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
200+
ENVIRONMENT: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && 'staging' || 'development' }}
201201

202202
# TODO: cache building yarn --production
203203
# possibly 2m30s of savings(?)

.github/workflows/npm-brew.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +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 }}
24+
NPM_ENVIRONMENT: "production"
2525

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

ci/build/release-github-assets.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ main() {
1010
cd "$(dirname "$0")/../.."
1111
source ./ci/lib.sh
1212

13-
download_artifact release-packages ./release-packages
13+
download_artifact release-packages ./release-packages "production" "v$VERSION"
1414
local assets=(./release-packages/code-server*"$VERSION"*{.tar.gz,.deb,.rpm})
1515

1616
EDITOR=true gh release upload "v$VERSION" "${assets[@]}" --clobber

ci/lib.sh

+53-9
Original file line numberDiff line numberDiff line change
@@ -49,43 +49,87 @@ arch() {
4949
esac
5050
}
5151

52+
# A helper function to get the artifacts url
53+
# Takes 1 argument:
54+
# $1 = environment {string} "staging" | "development" | "production" - which environment this is called in
55+
# Notes:
5256
# Grabs the most recent ci.yaml github workflow run that was triggered from the
5357
# pull request of the release branch for this version (regardless of whether
5458
# that run succeeded or failed). The release branch name must be in semver
5559
# format with a v prepended.
5660
# This will contain the artifacts we want.
5761
# https://developer.github.com/v3/actions/workflow-runs/#list-workflow-runs
5862
get_artifacts_url() {
63+
local environment="$1"
64+
local branch="$2"
5965
local artifacts_url
60-
local version_branch="v$VERSION"
61-
local workflow_runs_url="repos/:owner/:repo/actions/workflows/ci.yaml/runs?event=pull_request&branch=$version_branch"
62-
artifacts_url=$(gh api "$workflow_runs_url" | jq -r ".workflow_runs[] | select(.head_branch == \"$version_branch\") | .artifacts_url" | head -n 1)
66+
local event
67+
68+
case $environment in
69+
production)
70+
# This assumes you're releasing code-server using a branch named `v$VERSION` i.e. `v4.0.1`
71+
# If you don't, this will fail.
72+
branch="v$VERSION"
73+
event="pull_request"
74+
;;
75+
staging)
76+
branch="main"
77+
event="push"
78+
;;
79+
development)
80+
# Use $branch value passed in in this case
81+
event="pull_request"
82+
;;
83+
*)
84+
# Assume production for default
85+
branch="v$VERSION"
86+
event="pull_request"
87+
;;
88+
esac
89+
90+
local workflow_runs_url="repos/:owner/:repo/actions/workflows/ci.yaml/runs?event=$event&branch=$branch"
91+
artifacts_url=$(gh api "$workflow_runs_url" | jq -r ".workflow_runs[] | select(.head_branch == \"$branch\") | .artifacts_url" | head -n 1)
92+
6393
if [[ -z "$artifacts_url" ]]; then
6494
echo >&2 "ERROR: artifacts_url came back empty"
65-
echo >&2 "We looked for a successful run triggered by a pull_request with for code-server version: $VERSION and a branch named $version_branch"
95+
echo >&2 "We looked for a successful run triggered by a pull_request with for code-server version: $VERSION and a branch named $branch"
6696
echo >&2 "URL used for gh API call: $workflow_runs_url"
6797
exit 1
6898
fi
6999

70100
echo "$artifacts_url"
71101
}
72102

73-
# Grabs the artifact's download url.
74-
# https://developer.github.com/v3/actions/artifacts/#list-workflow-run-artifacts
103+
# A helper function to grab the artifact's download url
104+
# Takes 2 arguments:
105+
# $1 = artifact_name {string} - the name of the artifact to download (i.e. "npm-package")
106+
# $2 = environment {string} "staging" | "development" | "production" - which environment this is called in
107+
# $3 = branch {string} - the branch name
108+
# See: https://developer.github.com/v3/actions/artifacts/#list-workflow-run-artifacts
75109
get_artifact_url() {
76110
local artifact_name="$1"
77-
gh api "$(get_artifacts_url)" | jq -r ".artifacts[] | select(.name == \"$artifact_name\") | .archive_download_url" | head -n 1
111+
local environment="$2"
112+
local branch="$3"
113+
gh api "$(get_artifacts_url "$environment" "$branch")" | jq -r ".artifacts[] | select(.name == \"$artifact_name\") | .archive_download_url" | head -n 1
78114
}
79115

80-
# Uses the above two functions to download a artifact into a directory.
116+
# A helper function to download the an artifact into a directory
117+
# Takes 3 arguments:
118+
# $1 = artifact_name {string} - the name of the artifact to download (i.e. "npm-package")
119+
# $2 = destination {string} - where to download the artifact
120+
# $3 = environment {string} "staging" | "development" | "production" - which environment this is called in
121+
# $4 = branch {string} - the name of the branch to use when looking for artifacts
81122
download_artifact() {
82123
local artifact_name="$1"
83124
local dst="$2"
125+
# We assume production values unless specified
126+
local environment="${3:-production}"
127+
local branch="${4:v$VERSION}"
84128

85129
local tmp_file
86130
tmp_file="$(mktemp)"
87131

88-
gh api "$(get_artifact_url "$artifact_name")" > "$tmp_file"
132+
gh api "$(get_artifact_url "$artifact_name" "$environment" "$branch")" > "$tmp_file"
89133
unzip -q -o "$tmp_file" -d "$dst"
90134
rm "$tmp_file"
91135
}

ci/steps/docker-buildx-push.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ main() {
2424
fi
2525

2626
# Download the release-packages artifact
27-
download_artifact release-packages ./release-packages
27+
download_artifact release-packages ./release-packages "production" "v$VERSION"
2828

2929
# Login to Docker
3030
if [[ ${CI-} ]]; then

ci/steps/publish-npm.sh

+46-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

4-
# TODOs refactor this to determine ENVIRONMENT, NPM_TAG and NPM_VERSION
5-
64
main() {
75
cd "$(dirname "$0")/../.."
86
source ./ci/lib.sh
@@ -32,8 +30,8 @@ main() {
3230
# a developer to install this version with `yarn add code-server@beta`
3331
# "production" - this means we tag with `latest` (default), allowing
3432
# a developer to install this version with `yarn add code-server@latest`
35-
if ! is_env_var_set "ENVIRONMENT"; then
36-
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."
3735
exit 1
3836
fi
3937

@@ -49,27 +47,35 @@ main() {
4947
exit 1
5048
fi
5149

52-
# We need TAG to know what to publish under on npm
53-
# Options are "latest", "beta", or "<pr number >"
54-
# See Environment comments above to know when each is used.
55-
# TODO@jsjoeio - we need to determine this ourselves
56-
if ! is_env_var_set "NPM_TAG"; then
57-
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."
5853
exit 1
5954
fi
6055

61-
echo "using tag: $NPM_TAG"
56+
# We use this to grab the branch name
57+
if ! is_env_var_set "GITHUB_REF_NAME"; then
58+
echo "GITHUB_REF_NAME 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
6267

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

68-
# TODO@jsjoeio we need to refactor to download this based on environment
69-
# for "development", use the branch artifacts
70-
# for "staging" (merges into main),
71-
# for "production" look for release branch (currently we do this)
72-
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_REF_NAME:main}"
78+
download_artifact npm-package ./release-npm-package "$NPM_ENVIRONMENT" "$BRANCH"
7379
# https://github.com/actions/upload-artifact/issues/38
7480
tar -xzf release-npm-package/package.tar.gz
7581

@@ -81,22 +87,40 @@ main() {
8187
# We only need to run npm version for "development" and "staging".
8288
# This is because our release:prep script automatically bumps the version
8389
# in the package.json and we commit it as part of the release PR.
84-
if [[ "$ENVIRONMENT" == "production" ]]; then
90+
if [[ "$NPM_ENVIRONMENT" == "production" ]]; then
8591
NPM_VERSION="$VERSION"
92+
# This means the npm version will be published as "stable"
93+
# and installed when a user runs `yarn install code-server`
94+
NPM_TAG="latest"
8695
else
96+
COMMIT_SHA="$GITHUB_SHA"
8797
echo "Not a production environment"
88-
echo "Found environment: $ENVIRONMENT"
98+
echo "Found environment: $NPM_ENVIRONMENT"
8999
echo "Manually bumping npm version..."
90100

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

117+
echo "using tag: $NPM_TAG"
118+
96119
# We modify the version in the package.json
97120
# to be the current version + the PR number + commit SHA
121+
# or we use current version + beta + commit SHA
98122
# Example: "version": "4.0.1-4769-ad7b23cfe6ffd72914e34781ef7721b129a23040"
99-
NPM_VERSION="$VERSION-$PR_NUMBER_AND_COMMIT_SHA"
123+
# Example: "version": "4.0.1-beta-ad7b23cfe6ffd72914e34781ef7721b129a23040"
100124
pushd release
101125
# NOTE:@jsjoeio
102126
# I originally tried to use `yarn version` but ran into issues and abandoned it.

0 commit comments

Comments
 (0)