Skip to content

Commit 504328a

Browse files
authored
Merge pull request #3 from exploded/claude/github-linode-deployment-tSEGu
Claude/GitHub linode deployment
2 parents 80dd3ba + d47f1a7 commit 504328a

2 files changed

Lines changed: 64 additions & 51 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
uses: actions/setup-go@v5
1818
with:
1919
go-version: '1.21'
20-
cache: true
20+
cache: false
2121

2222
- name: Download dependencies
2323
run: go mod download
@@ -38,7 +38,7 @@ jobs:
3838
uses: actions/setup-go@v5
3939
with:
4040
go-version: '1.21'
41-
cache: true
41+
cache: false
4242

4343
- name: Download dependencies
4444
run: go mod download
@@ -52,7 +52,7 @@ jobs:
5252
host: ${{ secrets.DEPLOY_HOST }}
5353
username: ${{ secrets.DEPLOY_USER }}
5454
key: ${{ secrets.DEPLOY_SSH_KEY }}
55-
port: ${{ secrets.DEPLOY_PORT || 22 }}
55+
port: ${{ secrets.DEPLOY_PORT || '22' }}
5656
source: "moon,index.html,about.html,calendar.html,static/"
5757
target: "/tmp/moon-deploy"
5858
overwrite: true
@@ -63,31 +63,5 @@ jobs:
6363
host: ${{ secrets.DEPLOY_HOST }}
6464
username: ${{ secrets.DEPLOY_USER }}
6565
key: ${{ secrets.DEPLOY_SSH_KEY }}
66-
port: ${{ secrets.DEPLOY_PORT || 22 }}
67-
script: |
68-
set -e
69-
70-
DEPLOY_SRC=/tmp/moon-deploy
71-
72-
# Install binary
73-
sudo cp "$DEPLOY_SRC/moon" /usr/local/bin/moon
74-
sudo chmod +x /usr/local/bin/moon
75-
76-
# Update web assets
77-
sudo cp "$DEPLOY_SRC/index.html" /var/www/moon/
78-
sudo cp "$DEPLOY_SRC/about.html" /var/www/moon/
79-
sudo cp "$DEPLOY_SRC/calendar.html" /var/www/moon/
80-
sudo cp -r "$DEPLOY_SRC/static/" /var/www/moon/
81-
sudo chown -R www-data:www-data /var/www/moon
82-
83-
# Restart service
84-
sudo systemctl restart moon
85-
86-
# Verify it came back up
87-
sleep 2
88-
sudo systemctl is-active moon
89-
90-
# Clean up
91-
rm -rf "$DEPLOY_SRC"
92-
93-
echo "Deployment complete"
66+
port: ${{ secrets.DEPLOY_PORT || '22' }}
67+
script: sudo /usr/local/bin/deploy-moon /tmp/moon-deploy

scripts/server-setup.sh

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,75 @@ chmod 600 "$KEY_DIR/authorized_keys"
5353
chown -R "$DEPLOY_USER:$DEPLOY_USER" "$KEY_DIR"
5454

5555
# ---------------------------------------------------------------
56-
# 3. Create sudoers entry (least privilege)
56+
# 3. Create the server-side deploy script (runs as root via sudo)
57+
#
58+
# Reads User/Group directly from the installed service file so
59+
# this script never needs to hardcode a username.
60+
# ---------------------------------------------------------------
61+
cat > /usr/local/bin/deploy-moon << 'DEPLOY_SCRIPT'
62+
#!/bin/bash
63+
# /usr/local/bin/deploy-moon
64+
# Runs as root (via sudo) during GitHub Actions deployments.
65+
66+
set -e
67+
68+
DEPLOY_SRC="${1:-/tmp/moon-deploy}"
69+
DEPLOY_DIR=/var/www/moon
70+
71+
# Read the service owner from the installed unit — no hardcoded username
72+
SERVICE_USER=$(systemctl show moon --property=User --value)
73+
SERVICE_GROUP=$(systemctl show moon --property=Group --value)
74+
75+
if [ -z "$SERVICE_USER" ]; then
76+
echo "[deploy] ERROR: Could not read User from moon.service"
77+
exit 1
78+
fi
79+
80+
echo "[deploy] Installing binary to $DEPLOY_DIR/moon (owner: $SERVICE_USER:$SERVICE_GROUP)..."
81+
cp "$DEPLOY_SRC/moon" "$DEPLOY_DIR/moon"
82+
chmod +x "$DEPLOY_DIR/moon"
83+
84+
echo "[deploy] Updating web assets..."
85+
cp "$DEPLOY_SRC/index.html" "$DEPLOY_DIR/"
86+
cp "$DEPLOY_SRC/about.html" "$DEPLOY_DIR/"
87+
cp "$DEPLOY_SRC/calendar.html" "$DEPLOY_DIR/"
88+
cp -r "$DEPLOY_SRC/static/" "$DEPLOY_DIR/"
89+
chown -R "$SERVICE_USER:$SERVICE_GROUP" "$DEPLOY_DIR"
90+
91+
echo "[deploy] Restarting service..."
92+
systemctl restart moon
93+
94+
echo "[deploy] Verifying service is active..."
95+
sleep 2
96+
if ! systemctl is-active --quiet moon; then
97+
echo "[deploy] ERROR: Service failed to start. Status:"
98+
systemctl status moon --no-pager --lines=30
99+
exit 1
100+
fi
101+
102+
echo "[deploy] Cleaning up..."
103+
rm -rf "$DEPLOY_SRC"
104+
105+
echo "[deploy] Done — moon is running."
106+
DEPLOY_SCRIPT
107+
108+
chmod +x /usr/local/bin/deploy-moon
109+
echo "[ok] Created /usr/local/bin/deploy-moon"
110+
111+
# ---------------------------------------------------------------
112+
# 4. Configure sudoers — only allow the one deploy script
57113
# ---------------------------------------------------------------
58114
SUDOERS_FILE="/etc/sudoers.d/moon-deploy"
59115

60116
cat > "$SUDOERS_FILE" << 'EOF'
61-
# Allow the deploy user to install the moon app without a password
62-
deploy ALL=(ALL) NOPASSWD: \
63-
/bin/cp /tmp/moon-deploy/moon /usr/local/bin/moon, \
64-
/bin/chmod +x /usr/local/bin/moon, \
65-
/bin/cp /tmp/moon-deploy/index.html /var/www/moon/, \
66-
/bin/cp /tmp/moon-deploy/about.html /var/www/moon/, \
67-
/bin/cp /tmp/moon-deploy/calendar.html /var/www/moon/, \
68-
/bin/cp -r /tmp/moon-deploy/static/ /var/www/moon/, \
69-
/bin/chown -R www-data\:www-data /var/www/moon, \
70-
/usr/bin/systemctl restart moon, \
71-
/usr/bin/systemctl is-active moon
117+
# Allow the deploy user to run the moon deployment script as root
118+
deploy ALL=(ALL) NOPASSWD: /usr/local/bin/deploy-moon
72119
EOF
73120

74121
chmod 440 "$SUDOERS_FILE"
75-
# Validate the file
76122
visudo -c -f "$SUDOERS_FILE"
77123
echo "[ok] sudoers entry created at $SUDOERS_FILE"
78124

79-
# ---------------------------------------------------------------
80-
# 4. Ensure /var/www/moon exists and is owned correctly
81-
# ---------------------------------------------------------------
82-
mkdir -p /var/www/moon
83-
chown -R www-data:www-data /var/www/moon
84-
echo "[ok] /var/www/moon ready"
85-
86125
# ---------------------------------------------------------------
87126
# 5. Print next steps
88127
# ---------------------------------------------------------------

0 commit comments

Comments
 (0)