Skip to content

Commit 91fc1cc

Browse files
committed
clean it all up
1 parent 8e9e67a commit 91fc1cc

File tree

6 files changed

+46
-37
lines changed

6 files changed

+46
-37
lines changed

.github/workflows/ci.yaml

+2-2
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
198200
ENVIRONMENT: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && 'staging' || 'development' }}
199-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
200-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
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

+22-20
Original file line numberDiff line numberDiff line change
@@ -61,41 +61,38 @@ arch() {
6161
# https://developer.github.com/v3/actions/workflow-runs/#list-workflow-runs
6262
get_artifacts_url() {
6363
local environment="$1"
64+
local branch="$2"
6465
local artifacts_url
66+
local event
6567

6668
case $environment in
6769
production)
6870
# This assumes you're releasing code-server using a branch named `v$VERSION` i.e. `v4.0.1`
6971
# If you don't, this will fail.
70-
local branch="v$VERSION"
71-
local event="pull_request"
72-
local workflow_runs_url="repos/:owner/:repo/actions/workflows/ci.yaml/runs?event=$event&branch=$branch"
73-
artifacts_url=$(gh api "$workflow_runs_url" | jq -r ".workflow_runs[] | select(.head_branch == \"$branch\") | .artifacts_url" | head -n 1)
72+
branch="v$VERSION"
73+
event="pull_request"
7474
;;
7575
staging)
76-
local branch="main"
77-
local event="push"
78-
local workflow_runs_url="repos/:owner/:repo/actions/workflows/ci.yaml/runs?event=$event&branch=$branch"
79-
artifacts_url=$(gh api "$workflow_runs_url" | jq -r ".workflow_runs[] | select(.head_branch == \"$branch\") | .artifacts_url" | head -n 1)
76+
branch="main"
77+
event="push"
8078
;;
8179
development)
82-
# TODO@jsjoeio how do we get the branch name here? probably need to pass in from caller
83-
local branch="????"
84-
local event="pull_request"
85-
local workflow_runs_url="repos/:owner/:repo/actions/workflows/ci.yaml/runs?event=$event&branch=$branch"
86-
artifacts_url=$(gh api "$workflow_runs_url" | jq -r ".workflow_runs[] | select(.head_branch == \"$version_branch\") | .artifacts_url" | head -n 1)
80+
# Use $branch value passed in in this case
81+
event="pull_request"
8782
;;
8883
*)
89-
echo -n "unknown"
84+
# Assume production for default
85+
branch="v$VERSION"
86+
event="pull_request"
9087
;;
9188
esac
9289

93-
local workflow_runs_url="repos/:owner/:repo/actions/workflows/ci.yaml/runs?event=pull_request&branch=$version_branch"
94-
artifacts_url=$(gh api "$workflow_runs_url" | jq -r ".workflow_runs[] | select(.head_branch == \"$version_branch\") | .artifacts_url" | head -n 1)
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)
9592

9693
if [[ -z "$artifacts_url" ]]; then
9794
echo >&2 "ERROR: artifacts_url came back empty"
98-
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"
9996
echo >&2 "URL used for gh API call: $workflow_runs_url"
10097
exit 1
10198
fi
@@ -107,27 +104,32 @@ get_artifacts_url() {
107104
# Takes 2 arguments:
108105
# $1 = artifact_name {string} - the name of the artifact to download (i.e. "npm-package")
109106
# $2 = environment {string} "staging" | "development" | "production" - which environment this is called in
107+
# $3 = branch {string} - the branch name
110108
# See: https://developer.github.com/v3/actions/artifacts/#list-workflow-run-artifacts
111109
get_artifact_url() {
112110
local artifact_name="$1"
113111
local environment="$2"
114-
gh api "$(get_artifacts_url "$environment")" | jq -r ".artifacts[] | select(.name == \"$artifact_name\") | .archive_download_url" | head -n 1
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
115114
}
116115

117116
# A helper function to download the an artifact into a directory
118117
# Takes 3 arguments:
119118
# $1 = artifact_name {string} - the name of the artifact to download (i.e. "npm-package")
120119
# $2 = destination {string} - where to download the artifact
121120
# $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
122122
download_artifact() {
123123
local artifact_name="$1"
124124
local dst="$2"
125-
local environment="$3"
125+
# We assume production values unless specified
126+
local environment="${3:-production}"
127+
local branch="${4:v$VERSION}"
126128

127129
local tmp_file
128130
tmp_file="$(mktemp)"
129131

130-
gh api "$(get_artifact_url "$artifact_name" "$environment")" > "$tmp_file"
132+
gh api "$(get_artifact_url "$artifact_name" "$environment" "$branch")" > "$tmp_file"
131133
unzip -q -o "$tmp_file" -d "$dst"
132134
rm "$tmp_file"
133135
}

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

+19-12
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

@@ -53,6 +53,12 @@ main() {
5353
exit 1
5454
fi
5555

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+
5662
# We use this when setting NPM_VERSION
5763
if ! is_env_var_set "GITHUB_SHA"; then
5864
echo "GITHUB_SHA is not set. Are you running this locally? We rely on values provided by GitHub."
@@ -64,11 +70,12 @@ main() {
6470
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
6571
fi
6672

67-
# TODO@jsjoeio we need to refactor to download this based on environment
68-
# for "development", use the branch artifacts
69-
# for "staging" (merges into main),
70-
# for "production" look for release branch (currently we do this)
71-
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"
7279
# https://github.com/actions/upload-artifact/issues/38
7380
tar -xzf release-npm-package/package.tar.gz
7481

@@ -80,27 +87,27 @@ main() {
8087
# We only need to run npm version for "development" and "staging".
8188
# This is because our release:prep script automatically bumps the version
8289
# in the package.json and we commit it as part of the release PR.
83-
if [[ "$ENVIRONMENT" == "production" ]]; then
90+
if [[ "$NPM_ENVIRONMENT" == "production" ]]; then
8491
NPM_VERSION="$VERSION"
8592
# This means the npm version will be published as "stable"
8693
# and installed when a user runs `yarn install code-server`
8794
NPM_TAG="latest"
8895
else
8996
COMMIT_SHA="$GITHUB_SHA"
9097
echo "Not a production environment"
91-
echo "Found environment: $ENVIRONMENT"
98+
echo "Found environment: $NPM_ENVIRONMENT"
9299
echo "Manually bumping npm version..."
93100

94-
if [[ "$ENVIRONMENT" == "beta" ]]; then
101+
if [[ "$NPM_ENVIRONMENT" == "beta" ]]; then
95102
NPM_VERSION="$VERSION-beta-$COMMIT_SHA"
96103
# This means the npm version will be tagged with "beta"
97104
# and installed when a user runs `yarn install code-server@beta`
98105
NPM_TAG="beta"
99106
fi
100107

101-
if [[ "$ENVIRONMENT" == "development" ]]; then
108+
if [[ "$NPM_ENVIRONMENT" == "development" ]]; then
102109
# Source: https://github.com/actions/checkout/issues/58#issuecomment-614041550
103-
PR_NUMBER=$(echo $GITHUB_REF | awk 'BEGIN { FS = "/" } ; { print $3 }')
110+
PR_NUMBER=$(echo "$GITHUB_REF" | awk 'BEGIN { FS = "/" } ; { print $3 }')
104111
NPM_VERSION="$VERSION-$PR_NUMBER-$COMMIT_SHA"
105112
# This means the npm version will be tagged with "<pr number>"
106113
# and installed when a user runs `yarn install code-server@<pr number>`

0 commit comments

Comments
 (0)