Skip to content

Commit a7f68dc

Browse files
authored
feat: add a download script to facilitate easy use in CI MONGOSH-2687 (#2532)
1 parent f8a1d79 commit a7f68dc

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: "Test download_latest script"
2+
on:
3+
schedule:
4+
- cron: "0 3 * * *" # every day at 03:00 UTC
5+
push:
6+
branches:
7+
- main
8+
pull_request:
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
smoke-tests:
15+
name: "OS: ${{ matrix.runner }}, node@${{ matrix.node }}"
16+
strategy:
17+
matrix:
18+
runner: [ubuntu, macos]
19+
node: [24.x]
20+
fail-fast: false
21+
runs-on: ${{ matrix.runner }}-latest
22+
timeout-minutes: 30 # Installing dependencies on windows can take a while
23+
env:
24+
npm_config_loglevel: verbose
25+
npm_config_foreground_scripts: "true"
26+
PUPPETEER_SKIP_DOWNLOAD: "true"
27+
steps:
28+
- uses: actions/checkout@v5
29+
- uses: actions/setup-node@v6
30+
with:
31+
check-latest: true
32+
node-version: ${{ matrix.node }}
33+
34+
- name: Install mongosh through download_latest.sh
35+
run: ./download_latest.sh
36+
37+
- name: Run smoke tests
38+
run: npx -y mongodb-runner -- exec -- sh -c 'env MONGOSH_SMOKE_TEST_SERVER=$MONGODB_URI ./mongosh --smokeTests'

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ Once downloaded, you will have to extract the binary and add it to your PATH
2222
variable. For detailed instructions for each of our supported platforms, please visit
2323
[installation documentation](https://www.mongodb.com/docs/mongodb-shell/install#mdb-shell-install).
2424

25+
Alternatively:
26+
- Run `npx mongosh` to run mongosh without a full installation. This is
27+
easiest if you already have npm installed.
28+
- Run `download_latest.sh` to download a `mongosh` binary. You can use
29+
the following script:
30+
```sh
31+
curl -fsSL https://raw.githubusercontent.com/mongodb-js/mongosh/refs/heads/main/download_latest.sh | sh
32+
```
33+
2534
## CLI Usage
2635

2736
<!-- AUTOMATICALLY_INSERT_CLI_USAGE -->

download_latest.sh

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
5+
MONGOSH_RELEASES_URL=https://downloads.mongodb.com/compass/mongosh.json
6+
7+
say() {
8+
echo >&2 "$@"
9+
}
10+
11+
sayf() {
12+
# shellcheck disable=SC2059
13+
printf >&2 "$@"
14+
}
15+
16+
show_download_link() {
17+
say "Download mongosh manually from:"
18+
say
19+
sayf "\t%s\n" 'https://www.mongodb.com/try/download/shell'
20+
}
21+
22+
for tool in jq curl; do
23+
which "$tool" >/dev/null || {
24+
say "This script requires '$tool'."
25+
exit 1
26+
}
27+
done
28+
29+
os=$(uname -o | tr '[:upper:]' '[:lower:]')
30+
arch=$(uname -m)
31+
32+
case "$os" in
33+
*linux)
34+
ext=tgz
35+
os=linux
36+
;;
37+
darwin)
38+
ext=zip
39+
;;
40+
*)
41+
say "❌ This script does not support this OS ($os)."
42+
show_download_link
43+
44+
exit 1
45+
esac
46+
47+
# normalize $arch:
48+
case "$arch" in
49+
amd64|x64)
50+
arch=x86_64
51+
;;
52+
aarch64)
53+
arch=arm64
54+
;;
55+
*)
56+
# Use uname’s reported architecture in the jq query.
57+
esac
58+
59+
if [ "$os" = "linux" ]; then
60+
if ldd $(which curl) | grep -q libssl.so.3 ; then
61+
openssl_query='and .sharedOpenssl == "openssl3"'
62+
else
63+
openssl_query='and (has("sharedOpenssl") | not)'
64+
fi
65+
else
66+
openssl_query=''
67+
fi
68+
69+
jq_query=$(cat <<EOF
70+
.versions[0].downloads[] |
71+
select(
72+
.distro == "$os" and
73+
.arch == "$arch"
74+
$openssl_query
75+
) |
76+
.archive.url
77+
EOF
78+
)
79+
80+
url=$(curl -fsSL $MONGOSH_RELEASES_URL | jq -r "$jq_query")
81+
82+
if [ -z "$url" ]; then
83+
say "❓ No download found for $os on $arch."
84+
show_download_link
85+
exit 1
86+
fi
87+
88+
case "$ext" in
89+
zip)
90+
file=$(mktemp)
91+
92+
say "Downloading $url to $file"
93+
#trap 'rm -f "$file"' EXIT
94+
95+
curl -fsSL "$url" > "$file"
96+
say "Downloaded $ext file; extracting mongosh …"
97+
98+
unzip -j "$file" '*/bin/mongosh*'
99+
;;
100+
tgz)
101+
say "Downloading & extracting from $url"
102+
103+
curl -fsSL "$url" | tar -xzf - \
104+
--transform "s/.*\///" \
105+
--wildcards "**/bin/mongosh*" \
106+
| sed -E 's/^.*[/]//'
107+
108+
;;
109+
*)
110+
say "Bad file extension: $ext"
111+
show_download_link
112+
exit 1
113+
esac
114+
115+
./mongosh --build-info >/dev/null 2>&1 || {
116+
say "❌ Downloaded mongosh is not executable."
117+
./mongosh --build-info
118+
exit 1
119+
}
120+
121+
say "✅ Success! 'mongosh' and its crypto library are now saved in this directory."

packages/build/src/download-center/config.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,24 @@ describe('DownloadCenter config', function () {
205205
tutorial_link: 'test',
206206
});
207207
});
208+
209+
it('the list is sorted by semver even if versions are added out of order', function () {
210+
const getVersionConfig1x = sinon.stub().returns({ version: '1.2.2' });
211+
const getVersionConfig2x = sinon.stub().returns({ version: '2.0.0' });
212+
const existingDownloadCenterConfig =
213+
createDownloadCenterConfig(getVersionConfig2x);
214+
expect(existingDownloadCenterConfig.versions).to.have.lengthOf(1);
215+
216+
const updatedConfig = getUpdatedDownloadCenterConfig(
217+
existingDownloadCenterConfig,
218+
getVersionConfig1x
219+
);
220+
221+
expect(updatedConfig.versions).to.deep.equal([
222+
{ version: '2.0.0' },
223+
{ version: '1.2.2' },
224+
]);
225+
});
208226
});
209227

210228
context(

packages/build/src/download-center/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ export function getUpdatedDownloadCenterConfig(
202202
currentVersions[matchingMajorVersionIdx] = versionConfig;
203203
}
204204

205+
// NB: download_latest.sh depends on the specific ordering of versions here.
205206
currentVersions.sort((a, b) => semver.rcompare(a.version, b.version));
206207

207208
return {

0 commit comments

Comments
 (0)