Skip to content

Commit f204a8f

Browse files
Support preview releases in publish script (#9170)
* feat: support preview releases in publish script The publish script has been updated to support a new 'preview' version. When releasing a preview version, the following changes occur: - A branch name must be provided, and the release will be built from that branch. - The npm package is published with a '--tag=preview' flag. - The following steps are skipped: - Pushing changes to the master branch. - Creating a GitHub release. - Generating firepit artifacts. - Generating a Docker image. * feat: include branch name in preview release version and tag Updates the preview release functionality to include the sanitized branch name in the npm prerelease version string and the npm distribution tag. This allows for multiple preview versions from different branches to coexist without conflicting. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent af64adf commit f204a8f

File tree

3 files changed

+106
-31
lines changed

3 files changed

+106
-31
lines changed

scripts/publish.sh

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
22
set -e
33

44
printusage() {
5-
echo "publish.sh <version>"
5+
echo "publish.sh <version> [branch]"
66
echo "REPOSITORY_ORG and REPOSITORY_NAME should be set in the environment."
77
echo "e.g. REPOSITORY_ORG=user, REPOSITORY_NAME=repo"
88
echo ""
99
echo "Arguments:"
10-
echo " version: 'patch', 'minor', 'major', or 'artifactsOnly'"
10+
echo " version: 'patch', 'minor', 'major', 'artifactsOnly', or 'preview'"
11+
echo " branch: required if version is 'preview'"
1112
}
1213

1314
VERSION=$1
15+
BRANCH=$2
1416
if [[ $VERSION == "" ]]; then
1517
printusage
1618
exit 1
1719
elif [[ $VERSION == "artifactsOnly" ]]; then
1820
echo "Skipping npm package publish since VERSION is artifactsOnly."
1921
exit 0
22+
elif [[ $VERSION == "preview" ]]; then
23+
if [[ $BRANCH == "" ]]; then
24+
printusage
25+
exit 1
26+
fi
2027
elif [[ ! ($VERSION == "patch" || $VERSION == "minor" || $VERSION == "major") ]]; then
2128
printusage
2229
exit 1
@@ -61,6 +68,11 @@ echo "Moved to temporary directory."
6168
echo "Cloning repository..."
6269
git clone "[email protected]:${REPOSITORY_ORG}/${REPOSITORY_NAME}.git"
6370
cd "${REPOSITORY_NAME}"
71+
if [[ $VERSION == "preview" ]]; then
72+
echo "Checking out branch $BRANCH..."
73+
git checkout "$BRANCH"
74+
echo "Checked out branch $BRANCH."
75+
fi
6476
echo "Cloned repository."
6577

6678
echo "Making sure there is a changelog..."
@@ -78,10 +90,18 @@ echo "Running tests..."
7890
npm test
7991
echo "Ran tests."
8092

81-
echo "Making a $VERSION version..."
82-
npm version $VERSION
83-
NEW_VERSION=$(jq -r ".version" package.json)
84-
echo "Made a $VERSION version."
93+
if [[ $VERSION == "preview" ]]; then
94+
echo "Making a preview version..."
95+
sanitized_branch=$(echo "$BRANCH" | sed 's/[^a-zA-Z0-9]/-/g')
96+
npm version prerelease --preid=preview-${sanitized_branch}
97+
NEW_VERSION=$(jq -r ".version" package.json)
98+
echo "Made a preview version."
99+
else
100+
echo "Making a $VERSION version..."
101+
npm version $VERSION
102+
NEW_VERSION=$(jq -r ".version" package.json)
103+
echo "Made a $VERSION version."
104+
fi
85105

86106
echo "Making the release notes..."
87107
RELEASE_NOTES_FILE=$(mktemp)
@@ -92,23 +112,31 @@ cat CHANGELOG.md >> "${RELEASE_NOTES_FILE}"
92112
echo "Made the release notes."
93113

94114
echo "Publishing to npm..."
95-
npx [email protected] --before-script ./scripts/clean-shrinkwrap.sh
115+
if [[ $VERSION == "preview" ]]; then
116+
# Note: we publish with a dynamic tag so that this does not become the "latest" version
117+
sanitized_branch=$(echo "$BRANCH" | sed 's/[^a-zA-Z0-9]/-/g')
118+
npx [email protected] --before-script ./scripts/clean-shrinkwrap.sh --tag=preview-${sanitized_branch}
119+
else
120+
npx [email protected] --before-script ./scripts/clean-shrinkwrap.sh
121+
fi
96122
echo "Published to npm."
97123

98-
echo "Updating package-lock.json for Docker image..."
99-
npm --prefix ./scripts/publish/firebase-docker-image install
100-
echo "Updated package-lock.json for Docker image."
124+
if [[ $VERSION != "preview" ]]; then
125+
echo "Updating package-lock.json for Docker image..."
126+
npm --prefix ./scripts/publish/firebase-docker-image install
127+
echo "Updated package-lock.json for Docker image."
101128

102-
echo "Cleaning up release notes..."
103-
rm CHANGELOG.md
104-
touch CHANGELOG.md
105-
git commit -m "[firebase-release] Removed change log and reset repo after ${NEW_VERSION} release" CHANGELOG.md scripts/publish/firebase-docker-image/package-lock.json
106-
echo "Cleaned up release notes."
129+
echo "Cleaning up release notes..."
130+
rm CHANGELOG.md
131+
touch CHANGELOG.md
132+
git commit -m "[firebase-release] Removed change log and reset repo after ${NEW_VERSION} release" CHANGELOG.md scripts/publish/firebase-docker-image/package-lock.json
133+
echo "Cleaned up release notes."
107134

108-
echo "Pushing to GitHub..."
109-
git push origin master --tags
110-
echo "Pushed to GitHub."
135+
echo "Pushing to GitHub..."
136+
git push origin master --tags
137+
echo "Pushed to GitHub."
111138

112-
echo "Publishing release notes..."
113-
hub release create --file "${RELEASE_NOTES_FILE}" "v${NEW_VERSION}"
114-
echo "Published release notes."
139+
echo "Publishing release notes..."
140+
hub release create --file "${RELEASE_NOTES_FILE}" "v${NEW_VERSION}"
141+
echo "Published release notes."
142+
fi

scripts/publish/cloudbuild.yaml

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,15 @@ steps:
9898
# Publish the package.
9999
- name: "gcr.io/$PROJECT_ID/package-builder"
100100
dir: "${_REPOSITORY_NAME}"
101-
args: ["bash", "./scripts/publish.sh", "${_VERSION}"]
101+
entrypoint: bash
102+
args:
103+
- -c
104+
- |
105+
if [ "${_VERSION}" == "preview" ]; then
106+
./scripts/publish.sh "${_VERSION}" "${_BRANCH}"
107+
else
108+
./scripts/publish.sh "${_VERSION}"
109+
fi
102110
env:
103111
- "REPOSITORY_ORG=${_REPOSITORY_ORG}"
104112
- "REPOSITORY_NAME=${_REPOSITORY_NAME}"
@@ -110,25 +118,51 @@ steps:
110118
# Set up the hub credentials for firepit-builder.
111119
- name: "gcr.io/$PROJECT_ID/firepit-builder"
112120
entrypoint: "bash"
113-
args: ["-c", "mkdir -vp ~/.config && cp -v hub ~/.config/hub"]
121+
args:
122+
- "-c"
123+
- |
124+
if [ "${_VERSION}" != "preview" ]; then
125+
mkdir -vp ~/.config && cp -v hub ~/.config/hub
126+
else
127+
echo "Skipping hub credentials for firepit-builder for preview."
128+
fi
114129
115130
# Publish the firepit builds.
116131
- name: "gcr.io/$PROJECT_ID/firepit-builder"
117-
entrypoint: "node"
118-
args: ["/usr/src/app/pipeline.js", "--package=firebase-tools@latest", "--publish"]
132+
entrypoint: "bash"
133+
args:
134+
- "-c"
135+
- |
136+
if [ "${_VERSION}" != "preview" ]; then
137+
node /usr/src/app/pipeline.js --package=firebase-tools@latest --publish
138+
else
139+
echo "Skipping firepit build for preview version."
140+
fi
119141
120142
# Grab the latest version, store in workspace
121143
- id: "Read New Version Number from npm"
122144
name: "node"
123145
entrypoint: "sh"
124-
args: ["-c", "npm view firebase-tools version > /workspace/version_number.txt"]
146+
args:
147+
- "-c"
148+
- |
149+
if [ "${_VERSION}" != "preview" ]; then
150+
npm view firebase-tools version > /workspace/version_number.txt
151+
else
152+
echo "Skipping version lookup for preview version."
153+
fi
125154
126155
# Publish the Firebase docker image
127156
- name: "gcr.io/cloud-builders/docker"
128-
entrypoint: "sh"
157+
entrypoint: "bash"
129158
args:
130159
- "-c"
131-
- "docker build -t us-docker.pkg.dev/${_ARTIFACT_REGISTRY_PROJECT}/us/firebase:$(cat /workspace/version_number.txt) -t us-docker.pkg.dev/${_ARTIFACT_REGISTRY_PROJECT}/us/firebase:latest -f ./firebase-docker-image/Dockerfile ./firebase-docker-image"
160+
- |
161+
if [ "${_VERSION}" != "preview" ]; then
162+
docker build -t us-docker.pkg.dev/${_ARTIFACT_REGISTRY_PROJECT}/us/firebase:$(cat /workspace/version_number.txt) -t us-docker.pkg.dev/${_ARTIFACT_REGISTRY_PROJECT}/us/firebase:latest -f ./firebase-docker-image/Dockerfile ./firebase-docker-image
163+
else
164+
echo "Skipping docker build for preview version."
165+
fi
132166
133167
images:
134168
- "us-docker.pkg.dev/${_ARTIFACT_REGISTRY_PROJECT}/us/firebase"
@@ -142,6 +176,7 @@ options:
142176

143177
substitutions:
144178
_VERSION: ""
179+
_BRANCH: ""
145180
_KEY_RING: "cloud-build-ring"
146181
_KEY_NAME: "publish"
147182
_REPOSITORY_ORG: "firebase"

scripts/publish/run.sh

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,33 @@
22
set -e
33

44
printusage() {
5-
echo "run.sh <version>"
5+
echo "run.sh <version> [branch]"
66
echo ""
77
echo "Arguments:"
8-
echo " version: 'patch', 'minor', 'major', or 'artifactsOnly'"
8+
echo " version: 'patch', 'minor', 'major', 'artifactsOnly', or 'preview'"
9+
echo " branch: required if version is 'preview'"
910
}
1011

1112
VERSION=$1
13+
BRANCH=$2
1214
if [[ $VERSION == "" ]]; then
1315
printusage
1416
exit 1
17+
elif [[ $VERSION == "preview" ]]; then
18+
if [[ $BRANCH == "" ]]; then
19+
printusage
20+
exit 1
21+
fi
1522
elif [[ ! ($VERSION == "patch" || $VERSION == "minor" || $VERSION == "major" || $VERSION == "artifactsOnly") ]]; then
1623
printusage
1724
exit 1
1825
fi
1926

27+
SUBSTITUTIONS="_VERSION=$VERSION"
28+
if [[ $VERSION == "preview" ]]; then
29+
SUBSTITUTIONS="$SUBSTITUTIONS,_BRANCH=$BRANCH"
30+
fi
31+
2032
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
2133

2234
cd "$THIS_DIR"
@@ -25,5 +37,5 @@ gcloud --project fir-tools-builds \
2537
builds \
2638
submit \
2739
--machine-type=e2-highcpu-8 \
28-
--substitutions=_VERSION=$VERSION \
40+
--substitutions=$SUBSTITUTIONS \
2941
.

0 commit comments

Comments
 (0)