Skip to content

Commit

Permalink
reconfig to be less jank
Browse files Browse the repository at this point in the history
  • Loading branch information
JackBlake-zkq committed Feb 11, 2024
1 parent eac263b commit e172cb8
Show file tree
Hide file tree
Showing 12 changed files with 754 additions and 52 deletions.
19 changes: 9 additions & 10 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ on:
push:
branches:
- main
paths:
- "server-wrapper/**"
- ".github/workflows/ci.yaml"
paths-ignore:
- ".github/workflows/run.yaml"
- "README.md"
- ".gitignore"

name: Build Server Wrapper

env:
IMAGE_NAME: ${{ github.event.act && 'localhost:8080' || 'ghcr.io' }}/${{ vars.IMAGE_REPO }}:latest

jobs:
build-and-push:
runs-on: ubuntu-latest
permissions: write-all
steps:
- uses: actions/checkout@v2
- if: ${{ !github.event.act }}
uses: docker/login-action@v1
- uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- run: docker build -t $IMAGE_NAME ./server-wrapper
- run: docker push $IMAGE_NAME
- name: run docker build and push
run: |
(cd easy-mcs && docker compose build)
docker push ghcr.io/jackblake-zkq/easy-mcs:latest
29 changes: 6 additions & 23 deletions .github/workflows/run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,13 @@ jobs:
name: run mc serv and open ngrok tunnel
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: setup env file
run: echo ${{ secrets.DOTENV}} > .env
- name: start mc server container
run: |
docker run -id --name mc --rm \
-p 25565:25565 \
-e START_COMMAND="${{ vars.START_COMMAND }}" \
-e S3_BUCKET_NAME=${{ vars.S3_BUCKET_NAME }} \
-e S3_REGION=${{ vars.S3_REGION }} \
-e BACKUP_FREQUENCY=${{ vars.BACKUP_FREQUENCY }} \
-e AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }} \
-e AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }} \
${{ github.event.act && 'localhost:8080' || 'ghcr.io' }}/${{ vars.IMAGE_REPO }}:latest
- name: install ngrok and screen
run: |
wget https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-${{ vars.ARCHITECTURE || 'amd64' }}.tgz
tar -xvf ngrok-v3-stable-linux-${{ vars.ARCHITECTURE || 'amd64' }}.tgz
sudo apt-get update
sudo apt-get install -y screen
- name: open ngrok tunnel in screen (SERVER IP HERE)
run: |
screen -d -m ./ngrok tcp 25565 --authtoken ${{ secrets.NGROK_AUTHTOKEN }} --log stdout
sleep 5
curl -s http://127.0.0.1:4040/api/tunnels | jq -r '.tunnels | .[0] | .public_url | .[6:]'
- name: wait until server finishes starting
run: timeout 5m grep -m 1 "Done" <(docker logs -f mc)
run: (cd easy-mcs && docker-compose up -d)
- name: wait until server starts, SERVER IP HERE
run: timeout 5m grep -m 1 "SERVER URL" <(docker logs -f mc)
- name: stop server after 3h if running on schedule
if: ${{ github.event_name == 'schedule' }}
run: (sleep 10800 && (echo "/stop" | docker attach mc)) &
Expand Down
File renamed without changes.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Free MC Server Hosting in GH Workflow
# Easy MCS

Run your Minecraft server IN A Github Workflow such that people can join it. This gives you 50h of free hosting per month, with unlimited customizability and 7GB of RAM. You could even get 14GB of RAM if you can get this working on a Mac or Windows GH Action Runner.
Docs Coming Soon

<!-- Run your Minecraft server IN A Github Workflow such that people can join it. This gives you 50h of free hosting per month, with unlimited customizability and 7GB of RAM. You could even get 14GB of RAM if you can get this working on a Mac or Windows GH Action Runner.
This repo also has a system for automatic backups to AWS S3, which will probably be free depending on the size of your server folder, but could incur a few cents a month if you have a huge one. If you do a bit of your own customization, you could use your own system.
Expand Down Expand Up @@ -36,5 +38,5 @@ Variables:
- `IMAGE_REPO` - path within ghcr.io to store your image at, something like `GH_USERNAME_LOWERCASE/IMAGE_NAME`. I'm doing `jackblake-zkq/mc-server`
- `S3_BUCKET_NAME` - name of S3 bucket you created
- `S3_REGION` - region of your S3 bucket
- `START_COMMAND` - command to start your minecreaft server e.g. `java -Xmx4G -Xms2G -jar server.jar nogui`
- `START_COMMAND` - command to start your minecreaft server e.g. `java -Xmx4G -Xms2G -jar server.jar nogui` -->

15 changes: 15 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3'
services:
mc-server:
image: ghcr.io/jackblake-zkq/easy-mcs:latest
build: .
volumes:
- ~/.aws/:/root/.aws:ro
- ./server:/server
env_file:
- .env
ports:
- "25565:25565"
stdin_open: true
tty: false
container_name: mcs
4 changes: 0 additions & 4 deletions event.json

This file was deleted.

37 changes: 25 additions & 12 deletions server-wrapper/index.mjs → index.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { spawn } from 'child_process';
import { writeS3, readS3 } from './s3.mjs';
import { mkdirSync } from 'fs';
import { connect } from "ngrok"


const { BACKUP_FREQUENCY, START_COMMAND } = process.env;
const { BACKUP_FREQUENCY, START_COMMAND, USE_BACKUP, NGROK_AUTHTOKEN } = process.env;

const command = START_COMMAND.split(" ")

let finishDiskSave = () => {};

let starting = true;

try {
process.chdir("./server")
} catch(err) {
mkdirSync("./server", {});
process.chdir("./server");
}
process.chdir("./server")

await readS3();
if(USE_BACKUP) await readS3();

const server_proc = spawn(command[0], command.slice(1));

Expand All @@ -29,7 +23,8 @@ server_proc.stdout.on('data', data => {
console.log(str);
if (str.includes("Done") && starting) {
starting = false;
setInterval(writeBackup, parseInt(BACKUP_FREQUENCY));
startNgrok();
if(BACKUP_FREQUENCY) setInterval(writeBackup, parseInt(BACKUP_FREQUENCY));
} else if (str.includes("Saved the game")) {
finishDiskSave();
}
Expand All @@ -44,7 +39,7 @@ server_proc.on('close', async code => {

if(code == 0) {
//must have saved to disk already, can write backup
await writeS3();
if(BACKUP_FREQUENCY) await writeS3();
}
process.exit(server_proc.exitCode);
});
Expand All @@ -68,4 +63,22 @@ async function writeBackup() {
await writeS3();

server_proc.stdin.write('/say Backup Complete\n');
}

async function startNgrok() {
connect({
proto: 'tcp', // http|tcp|tls, defaults to http
addr: 25565, // port or network address, defaults to 80
authtoken: NGROK_AUTHTOKEN, // your authtoken from ngrok.com
region: 'us', // one of ngrok regions (us, eu, au, ap, sa, jp, in), defaults to us
onStatusChange: status => {
if(status === 'closed') {
server_proc.stdin.write('/stop\n');
}
}
}).then(url => {
console.log("------------------------------------------------------")
console.log("SERVER URL: " + url.slice(6))
console.log("------------------------------------------------------")
})
}
Loading

0 comments on commit e172cb8

Please sign in to comment.