Linux script to download latest VS Code Server, good for Docker (tested in Alpine).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
# Auto-Get the latest commit sha via command line. | |
get_latest_release() { | |
tag=$(curl --silent "https://api.github.com/repos/${1}/releases/latest" | # Get latest release from GitHub API | |
grep '"tag_name":' | # Get tag line | |
sed -E 's/.*"([^"]+)".*/\1/' ) # Pluck JSON value | |
tag_data=$(curl --silent "https://api.github.com/repos/${1}/git/ref/tags/${tag}") | |
sha=$(echo "${tag_data}" | # Get latest release from GitHub API | |
grep '"sha":' | # Get tag line | |
sed -E 's/.*"([^"]+)".*/\1/' ) # Pluck JSON value | |
sha_type=$(echo "${tag_data}" | # Get latest release from GitHub API | |
grep '"type":' | # Get tag line | |
sed -E 's/.*"([^"]+)".*/\1/' ) # Pluck JSON value | |
if [ "${sha_type}" != "commit" ]; then | |
combo_sha=$(curl -s "https://api.github.com/repos/${1}/git/tags/${sha}" | # Get latest release from GitHub API | |
grep '"sha":' | # Get tag line | |
sed -E 's/.*"([^"]+)".*/\1/' ) # Pluck JSON value | |
# Remove the tag sha, leaving only the commit sha; | |
# this won't work if there are ever more than 2 sha, | |
# and use xargs to remove whitespace/newline. | |
sha=$(echo "${combo_sha}" | sed -E "s/${sha}//" | xargs) | |
fi | |
printf "${sha}" | |
} | |
ARCH="x64" | |
U_NAME=$(uname -m) | |
if [ "${U_NAME}" = "aarch64" ]; then | |
ARCH="arm64" | |
fi | |
archive="vscode-server-linux-${ARCH}.tar.gz" | |
owner='microsoft' | |
repo='vscode' | |
commit_sha=$(get_latest_release "${owner}/${repo}") | |
if [ -n "${commit_sha}" ]; then | |
echo "will attempt to download VS Code Server version = '${commit_sha}'" | |
# Download VS Code Server tarball to tmp directory. | |
curl -L "https://update.code.visualstudio.com/commit:${commit_sha}/server-linux-${ARCH}/stable" -o "/tmp/${archive}" | |
# Make the parent directory where the server should live. | |
# NOTE: Ensure VS Code will have read/write access; namely the user running VScode or container user. | |
mkdir -vp ~/.vscode-server/bin/"${commit_sha}" | |
# Extract the tarball to the right location. | |
tar --no-same-owner -xzv --strip-components=1 -C ~/.vscode-server/bin/"${commit_sha}" -f "/tmp/${archive}" | |
else | |
echo "could not pre install vscode server" | |
fi |
Thanks for the script!
If you add the following to the top of the script, and replace x64
with "$ARCH"
, this works for aarch64 devices and possible others too.
ARCH=$(uname -m)
if [[ "$ARCH" == "aarch64" ]]; then
ARCH="arm64"
elif [[ "$ARCH" == "x86_64" ]]; then
ARCH="x64"
fi
Thanks for the script!
If you add the following to the top of the script, and replace
x64
with"$ARCH"
, this works for aarch64 devices and possible others too.ARCH=$(uname -m) if [[ "$ARCH" == "aarch64" ]]; then ARCH="arm64" elif [[ "$ARCH" == "x86_64" ]]; then ARCH="x64" fi
Actually, this doesn't work if the script is executed by /bin/sh instead of /bin/bash, as specified by the shebang.
/bin/sh doesn't support [[
syntax.
The following works
if [ "$ARCH" = "aarch64" ]; then
ARCH="arm64"
elif [ "$ARCH" = "x86_64" ]; then
ARCH="x64"
fi
In that case, this line should also be changed from
if [[ "${sha_type}" != "commit" ]]; then
to
if [ "${sha_type}" != "commit" ]; then
That's a good idea. I'll try and add this tonight.
I added extra symlinking:
# Extract the tarball to the right location.
tar --no-same-owner -xzv --strip-components=1 -C ~/.vscode-server/bin/"${commit_sha}" -f "/tmp/${archive}"
+ (cd ~/.vscode-server/bin && ln -s "${commit_sha}" default_version)
So I can add code-server to $PATH
:
ADD download-vs-code-server.sh /root
RUN cd /root && chmod a+x download-vs-code-server.sh && ./download-vs-code-server.sh
ENV PATH=/root/.vscode-server/bin/default_version/bin:$PATH
RUN code-server --install-extension ms-python.python
Thanks a trillion, works great!
To make vscode-server download on raspberry pi, i added these lines:
if [ "${U_NAME}" = "armv7l" ]; then
ARCH="armhf"
fi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This worked!