Skip to content

Commit

Permalink
Deploy main branch to next.esm.sh (#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije authored Nov 16, 2024
1 parent f5ee45d commit 5a5cdce
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 43 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Deploy

on:
push:
branches: [main]

jobs:
test:
name: Deploy main branch to next.esm.sh
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.23.x

- name: Run `deploy-ci.sh`
run: scripts/deploy-ci.sh
env:
GOOS: linux
GOARCH: ${{ secrets.DEPLOY_HOST_ARCH }}
SSH_HOST_NAME: ${{ secrets.DEPLOY_SSH_HOST_NAME }}
SSH_USER: ${{ secrets.DEPLOY_SSH_USER }}
SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_SSH_PRIVATE_KEY }}
4 changes: 2 additions & 2 deletions .github/workflows/docker-push.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Docker
name: Docker Push

on:
push:
Expand All @@ -7,7 +7,7 @@ on:

jobs:
push_image:
name: Push `stable`
name: Push image to ghcr.io
runs-on: ubuntu-latest
permissions:
packages: write
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: Test

on:
push:
Expand All @@ -8,7 +8,7 @@ on:

jobs:
test:
name: Testing
name: Test All
runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.22.x
go-version: 1.23.x

- name: Setup Deno
uses: denoland/setup-deno@main
Expand All @@ -52,5 +52,5 @@ jobs:
- name: Run `go test`
run: go test ./server ./server/storage -v

- name: Run `deno test`
run: deno run -A test/bootstrap.ts
- name: Run `test/bootstrap.ts`
run: test/bootstrap.ts
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Create a `config.json` file in the project root directory following the example
"port": 8080,
"workDir": ".esmd",
"npmRegistry": "https://registry.npmjs.org/", // change to your own registry if needed
"npmToken": "xxxxxx" ,// remove this line if you don't have one
"npmToken": "xxxxxx", // remove this line if you don't have one
}
```

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build Stage
FROM golang:1.22-alpine AS build-stage
FROM golang:1.23-alpine AS build-stage

ENV ESM_SH_REPO https://github.com/esm-dev/esm.sh
ENV ESM_SH_VERSION v136
Expand Down
17 changes: 8 additions & 9 deletions HOSTING.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Self-Hosting

[esm.sh](https://esm.sh) provides a global fast CDN publicly which is powered by
[Cloudflare](https://cloudflare.com). You can also host esm.sh service by yourself.
[esm.sh](https://esm.sh) provides a global fast CDN publicly which is powered by [Cloudflare](https://cloudflare.com).
You can also host esm.sh service by yourself. Please follow the instructions below.

## Clone the Source Code

Expand All @@ -19,16 +19,15 @@ To configure the server, create a `config.json` file then pass it to the server
{
"port": 8080,
"npmRegistry": "https://registry.npmjs.org/",
"npmToken": "xxxxxx"
"npmToken": "******"
}
```

You can find all the server options in [config.example.jsonc](./config.example.jsonc). (**Note**: the
`config.example.jsonc` is not a valid JSON file, it's a JSONC file.)
You can find all the server options in [config.example.jsonc](./config.example.jsonc).

## Run the Server Locally

You will need [Go](https://golang.org/dl) 1.18+ to compile the server.
You will need [Go](https://golang.org/dl) 1.22+ to compile and run the server.

```bash
go run main.go --config=config.json
Expand All @@ -38,7 +37,7 @@ Then you can import `React` from <http://localhost:8080/react>.

## Deploy the Server to a Single Machine

Ensure the [supervisor](http://supervisord.org/) has been installed on your host machine.
We provide a bash script to deploy the server to a single machine.

```bash
# first time deploy
Expand All @@ -49,7 +48,7 @@ Ensure the [supervisor](http://supervisord.org/) has been installed on your host

Recommended host machine requirements:

- Linux system with `git`, `git-lfs` and `supervisor` installed
- Linux system (Debian/Ubuntu)
- 4x CPU cores or more
- 8GB RAM or more
- 100GB disk space or more
Expand All @@ -70,7 +69,7 @@ Run the container:
```bash
docker run -p 8080:8080 \
-e NPM_REGISTRY=https://registry.npmjs.org/ \
-e NPM_TOKEN=xxxxxx \
-e NPM_TOKEN=****** \
ghcr.io/esm-dev/esm.sh:latest
```

Expand Down
6 changes: 3 additions & 3 deletions config.example.jsonc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
// The port to listen server on for HTTP requests, default is 8080.
"port": 8080,
// The port to listen server on for HTTP requests, default is 80.
"port": 80,

// The port to listen server on for HTTPs requests, default is 0 (disabled). Change to 443 if you want to enable it.
// You don't need to provide a certificate, the server uses ACME protocol to get a free certificate from Let's Encrypt.
// Note: if you are running the server in a docker container, you need to expose the port as well.
// Note: if you are running the server in a docker container, you need to expose the port as `443:443`.
"tlsPort": 0,

// The secret token to validate the `Authorization: Bearer <secret>` header of incoming requests, default is disabled.
Expand Down
15 changes: 0 additions & 15 deletions scripts/build.sh

This file was deleted.

66 changes: 66 additions & 0 deletions scripts/deploy-ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/bash

scriptDir=$(dirname $0)

echo "--- building..."
go build -o $scriptDir/esmd $scriptDir/../main.go
if [ "$?" != "0" ]; then
exit
fi

mkdir -p ~/.ssh
echo "${SSH_PRIVATE_KEY}" >> ~/.ssh/id_ed25519
echo "next.esm.sh" >> ~/.ssh/config
echo " HostName ${SSH_HOST_NAME}" >> ~/.ssh/config
echo " User ${SSH_USER}" >> ~/.ssh/config
echo " IdentityFile ~/.ssh/id_ed25519" >> ~/.ssh/config
echo " IdentitiesOnly yes" >> ~/.ssh/config

echo "--- uploading..."
tar -czf esmd.tar.gz esmd
scp esmd.tar.gz next.esm.sh:/tmp/esmd.tar.gz
if [ "$?" != "0" ]; then
exit
fi

echo "--- installing..."
ssh next.esm.sh << EOF
cd /tmp
tar -xzf esmd.tar.gz
if [ "\$?" != "0" ]; then
exit
fi
rm -rf esmd.tar.gz
supervisorctl version
if [ "\$?" != "0" ]; then
apt update
apt install -y supervisor git git-lfs
fi
svcf=/etc/supervisor/conf.d/esmd.conf
reload=no
if [ ! -f \$svcf ]; then
echo "[program:esmd]" >> \$svcf
echo "command=/usr/local/bin/esmd" >> \$svcf
echo "directory=/tmp" >> \$svcf
echo "user=${SSH_USER}" >> \$svcf
echo "autostart=true" >> \$svcf
echo "autorestart=true" >> \$svcf
reload=yes
if
supervisorctl stop esmd
rm -f /usr/local/bin/esmd
mv -f esmd /usr/local/bin/esmd
chmod +x /usr/local/bin/esmd
if [ "\$?" != "0" ]; then
exit
fi
if [ "\$reload" == "yes" ]; then
supervisorctl reload
else
supervisorctl start esmd
fi
EOF
32 changes: 26 additions & 6 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ workDir=""

if [ "$init" == "yes" ]; then
echo "Server configuration:"
read -p "? http server port (default is ${port}): " v
read -p "? http server port (default is 80): " v
if [ "$v" != "" ]; then
port="$v"
fi
Expand Down Expand Up @@ -52,9 +52,19 @@ if [ "$v" != "" ]; then
sshPort="$v"
fi

cd $(dirname $0)
sh build.sh
read -p "? build GOOS (default is 'linux'): " goos
read -p "? build GOARCH (default is 'amd64'): " goarch
if [ "$goos" == "" ]; then
goos="linux"
fi
if [ "$goarch" == "" ]; then
goarch="amd64"
fi

echo "--- building(${goos}_$goarch)..."
export GOOS=$goos
export GOARCH=$goarch
go build -o $(dirname $0)/esmd $(dirname $0)/../main.go
if [ "$?" != "0" ]; then
exit
fi
Expand All @@ -69,7 +79,12 @@ if [ "$?" != "0" ]; then
fi

echo "--- installing..."
ssh -p $sshPort $user@$host << EOF
ssh -p $sshPort ${user}@${host} << EOF
if [ "$init" == "yes" ]; then
apt update
apt install -y supervisor git git-lfs
fi
SVV=\$(supervisorctl version)
if [ "\$?" != "0" ]; then
echo "error: supervisor not installed!"
Expand All @@ -84,6 +99,7 @@ ssh -p $sshPort $user@$host << EOF
cd /tmp
tar -xzf esmd.tar.gz
rm -rf esmd.tar.gz
supervisorctl stop esmd
rm -f /usr/local/bin/esmd
Expand All @@ -97,8 +113,12 @@ ssh -p $sshPort $user@$host << EOF
if [ -f \$SVCONF ]; then
rm -f \$SVCONF
fi
mkdir -p /etc/esmd
echo "{\"port\":${port},\"tlsPort\":${tlsPort},\"workDir\":\"${workDir}\"}" >> /etc/esmd/config.json
wd=$workDir
if [ "$wd" == "" ]; then
wd=~/.esmd
fi
mkdir $wd
echo "{\"port\": ${port}, \"tlsPort\": ${tlsPort}, \"workDir\": \"${wd}\"}" >> /etc/esmd/config.json
writeSVConfLine "[program:esmd]"
writeSVConfLine "command=/usr/local/bin/esmd --config=/etc/esmd/config.json"
writeSVConfLine "directory=/tmp"
Expand Down
2 changes: 1 addition & 1 deletion server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func DefaultConfig() *Config {

func normalizeConfig(c *Config) {
if c.Port == 0 {
c.Port = 8080
c.Port = 80
}
if c.AuthSecret == "" {
c.AuthSecret = os.Getenv("AUTH_SECRET")
Expand Down
9 changes: 9 additions & 0 deletions test/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ async function startServer(onStart: () => Promise<void>, verbose: boolean) {
if (!success) {
Deno.exit(code);
}
if (!(await existsFile("config.json"))) {
await Deno.writeTextFile(
"config.json",
JSON.stringify({
"port": 8080,
"workDir": ".esmd",
}),
);
}
const p = new Deno.Command("./esmd", {
args: ["--debug"],
stdout: verbose ? "inherit" : "null",
Expand Down

0 comments on commit 5a5cdce

Please sign in to comment.