forge
Explore
Log in

Don't you dare go Hollow

Resolve the deploy path from the server's $HOME

Browse files at this commit

03ba608313119511fccf84f3741bc98dd2d91df0Ahmet Kaan GümüşParent: 0ca81cd
  • ci
    • resolve the deploy path from the server's $HOME
Show full message
ci: resolve the deploy path from the server's $HOME

The deploy path was a secret holding an absolute path with the deploy
user's name baked in; the user change left it pointing at the previous
user's home. The path is now DEPLOY_SUBDIR (a visible constant) under
the SSH user's home, resolved remotely as $HOME — it follows whoever
SSH_USER is, and DEPLOY_PATH can be deleted from the repo secrets.
.github/workflows/main.ymlmodified+24 -17
@@ -25,6 +25,11 @@ env:
25 # CPU-bound on. See the profile in Cargo.toml.25 # CPU-bound on. See the profile in Cargo.toml.
26 PROFILE: dist26 PROFILE: dist
27 TOPCOAT: 0.6.227 TOPCOAT: 0.6.2
28+ # The deploy lives in this subdirectory of the SSH user's home. The home
29+ # itself is resolved on the server ($HOME), so switching the deploy user
30+ # moves the path with it — a baked-in absolute path once kept pointing at
31+ # the previous user's home and failed the deploy on permissions.
32+ DEPLOY_SUBDIR: izlek
28 33
29 jobs:34 jobs:
30 deploy:35 deploy:
@@ -101,22 +106,24 @@ jobs:
101 - name: Upload the release106 - name: Upload the release
102 run: |107 run: |
103 set -euo pipefail108 set -euo pipefail
104- deploy_path="${{ secrets.DEPLOY_PATH }}"
105 ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"109 ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"
106 110
107 # The server's login shell is not bash, and ssh would hand this111 # The server's login shell is not bash, and ssh would hand this
108 # script to it; `bash -s` takes it on stdin instead, so the login112 # script to it; `bash -s` takes it on stdin instead, so the login
109- # shell only ever parses one plain command. The deploy path goes113+ # shell only ever parses one plain command.
110- # as an argument, never interpolated into the script.114+ #
111- ssh "$ssh_target" "bash -se -- '$deploy_path'" <<'REMOTE'115+ # The deploy path is $DEPLOY_SUBDIR under the SSH user's home,
116+ # resolved on the server as $HOME: there is no path secret to go
117+ # stale, and switching the deploy user moves the deploy with it.
118+ ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR'" <<'REMOTE'
112 set -euo pipefail119 set -euo pipefail
113- deploy_path=$1120+ case "$1" in
114- case "$deploy_path" in121+ ''|/*|*..*)
115- ''|/|/home|/home/|/srv|/srv/|/var|/var/)122+ echo "Refusing a broad deploy subdirectory: '$1'" >&2
116- echo "Refusing to deploy to a broad or empty path: $deploy_path" >&2
117 exit 1123 exit 1
118 ;;124 ;;
119 esac125 esac
126+ deploy_path=$HOME/$1
120 # The deploy path is the service's working directory: config/,127 # The deploy path is the service's working directory: config/,
121 # izlek.db and izlek.key live there and are never touched from here.128 # izlek.db and izlek.key live there and are never touched from here.
122 # Only 'current' is replaced.129 # Only 'current' is replaced.
@@ -124,21 +131,21 @@ jobs:
124 rm -rf -- "$deploy_path/current.new"131 rm -rf -- "$deploy_path/current.new"
125 mkdir -p -- "$deploy_path/current.new"132 mkdir -p -- "$deploy_path/current.new"
126 REMOTE133 REMOTE
127- # A bare command with quoted arguments parses under any login134+ # stdin carries the tarball, so the script cannot; the extraction
128- # shell, so the tar extraction needs no bash wrapper.135+ # still goes through bash -c because the login shell is fish and
129- tar -C stage -czf - . | ssh "$ssh_target" "tar -xzf - -C '$deploy_path/current.new'"136+ # would not expand $HOME.
137+ tar -C stage -czf - . | ssh "$ssh_target" "bash -c 'exec tar -xzf - -C \"\$HOME/\$1/current.new\"' -- '$DEPLOY_SUBDIR'"
130 138
131 - name: Swap and restart139 - name: Swap and restart
132 run: |140 run: |
133 set -euo pipefail141 set -euo pipefail
134- deploy_path="${{ secrets.DEPLOY_PATH }}"
135 ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"142 ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"
136 143
137 # bash -s for the same reason as above: the login shell is not144 # bash -s for the same reason as above: the login shell is not
138 # bash and must never see this script.145 # bash and must never see this script.
139- ssh "$ssh_target" "bash -se -- '$deploy_path' '$SERVICE'" <<'REMOTE'146+ ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR' '$SERVICE'" <<'REMOTE'
140 set -euo pipefail147 set -euo pipefail
141- deploy_path=$1148+ deploy_path=$HOME/$1
142 service=$2149 service=$2
143 export XDG_RUNTIME_DIR=/run/user/$(id -u)150 export XDG_RUNTIME_DIR=/run/user/$(id -u)
144 # Read before anything moves: a config the health check cannot151 # Read before anything moves: a config the health check cannot
@@ -179,15 +186,15 @@ jobs:
179 - name: Verify the served stylesheet is this deploy's186 - name: Verify the served stylesheet is this deploy's
180 run: |187 run: |
181 set -euo pipefail188 set -euo pipefail
182- deploy_path="${{ secrets.DEPLOY_PATH }}"
183 ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"189 ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"
184 # One remote script: mixing runner-side and server-side variables in190 # One remote script: mixing runner-side and server-side variables in
185 # nested ssh quoting once shipped a check that failed on a healthy191 # nested ssh quoting once shipped a check that failed on a healthy
186 # deploy. Everything server-side happens inside the heredoc; only the192 # deploy. Everything server-side happens inside the heredoc; only the
187 # stylesheet bytes come back on stdout.193 # stylesheet bytes come back on stdout.
188- ssh "$ssh_target" "bash -se -- '$deploy_path'" > served-stylesheet.css <<'REMOTE'194+ ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR'" > served-stylesheet.css <<'REMOTE'
189 set -euo pipefail195 set -euo pipefail
190- listen=$(sed -n 's/^listen[[:space:]]*=[[:space:]]*"\(.*\)"/\1/p' "$1/config/izlek.toml")196+ deploy_path=$HOME/$1
197+ listen=$(sed -n 's/^listen[[:space:]]*=[[:space:]]*"\(.*\)"/\1/p' "$deploy_path/config/izlek.toml")
191 url=$(curl -fsS "http://${listen}/" | grep -oE '/_topcoat/assets/main-[a-f0-9]+\.css' | head -n1)198 url=$(curl -fsS "http://${listen}/" | grep -oE '/_topcoat/assets/main-[a-f0-9]+\.css' | head -n1)
192 if [ -z "$url" ]; then199 if [ -z "$url" ]; then
193 echo 'the served page references no stylesheet' >&2200 echo 'the served page references no stylesheet' >&2