1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
# Bastion Server — Setup Guide
Full instruction to build the server from a fresh Debian installation.
## Prerequisites
- Fresh Debian server
- Root access
- Domain `swave.lol` with DNS A records pointing to the server:
- `swave.lol`
- `blog.swave.lol`
- `jenkins.swave.lol`
- `cgit.swave.lol`
- `gerrit.swave.lol`
## 1. Install Docker
Based on https://docs.docker.com/engine/install/debian/#install-using-the-repository
```bash
# Install prerequisites
apt-get update
apt-get install -y ca-certificates curl gnupg
# Add Docker GPG key
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
# Add Docker repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# Verify
docker run hello-world
```
## 2. Clone the bastion repository
```bash
cd /root/Projects
git clone <your-repo-url> bastion
cd bastion
```
## 3. Start Portainer
Portainer manages all other containers and stacks via web UI.
```bash
docker run -d \
--name="portainer" \
--restart on-failure \
-p 9000:9000 \
-p 8000:8000 \
-v /var/run/docker.sock:/var/run/docker.sock \
-v portainer_data:/data \
portainer/portainer-ce:latest
```
Access Portainer at `http://<server-ip>:9000` and create admin account.
## 4. Git Server
### 4.1 Create host directories
```bash
# Directory for SSH public keys (for git access)
mkdir -p /var/git_ssh_keys
# Directory for git repositories
mkdir -p /var/git/repos
# Copy your public SSH keys for git access (use ed25519, RSA is not supported)
# cp ~/.ssh/id_ed25519.pub /var/git_ssh_keys/mykey.pub
```
### 4.2 Build and start
```bash
cd /root/Projects/bastion/git-server
# Build the git-server image
docker build -t git-server .
# Start (this also creates the git-network)
docker compose -f server.yaml up -d
```
### 4.3 Create a test repository
```bash
# On the server
cd /var/git/repos
git init --bare test.git
chown -R 1000:1000 test.git # git user inside container
# From a client machine
git clone ssh://git@<server-ip>/repos/test.git
or
git clone git://<server-ip>/test.git
```
### 4.4 Import an existing repository
To make a one-time copy of an existing repository (e.g., from GitHub):
```bash
cd /var/git/repos
git clone --bare https://github.com/user/repo.git
chown -R 1000:1000 repo.git
```
This copies all branches, tags, and history. The remote reference is removed,
so it becomes a standalone copy with no link back to the original.
To keep syncing from the original source, use `--mirror` instead:
```bash
cd /var/git/repos
git clone --mirror https://github.com/user/repo.git
chown -R 1000:1000 repo.git
```
This keeps the remote configured so you can periodically pull updates:
```bash
cd /var/git/repos/repo.git
git remote update
```
`--mirror` syncs all refs exactly, including deleted branches.
## 5. Ghost (Blog)
### 5.1 Prepare environment
```bash
cd /root/Projects/bastion/ghost
# Copy the mysql init script to the expected location
mkdir -p /var/lib/docker/mysql-init-script
cp mysql-init-script/create-multiple-databases.sh /var/lib/docker/mysql-init-script/
# Edit stack.env (or .env for non-Portainer use) with your credentials
# IMPORTANT: Change default passwords before first run!
```
Key settings in `stack.env`:
- `DOMAIN` — your domain (e.g. `swave.lol`)
- `DATABASE_ROOT_PASSWORD` — MySQL root password
- `DATABASE_USER` / `DATABASE_PASSWORD` — Ghost database credentials
- `UPLOAD_LOCATION` — where Ghost stores content
### 5.2 Start Ghost
```bash
cd /root/Projects/bastion/ghost
docker compose up -d
```
Ghost will be available at `http://<server-ip>:2368` for testing before Nginx is configured.
To start with ActivityPub support (optional):
```bash
docker compose --profile activitypub up -d
```
## 6. Jenkins
### 6.1 Create host directories
```bash
mkdir -p /var/jenkins_home
chown -R 1000:1000 /var/jenkins_home # jenkins user inside container runs as UID 1000
```
### 6.2 Build and start
```bash
cd /root/Projects/bastion/jenkins
# Build Jenkins image (includes Docker CLI and docker-workflow plugin)
docker compose build
# Start Jenkins
docker compose up -d
```
### 6.3 Get initial admin password
```bash
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
```
Jenkins will be available at `http://<server-ip>:8080/jenkins` for initial setup.
### 6.4 Jenkins build setup (DooD)
Jenkins uses Docker-out-of-Docker: it spawns sibling containers on the host for builds.
Each project defines its own build image in the Jenkinsfile:
```groovy
pipeline {
agent {
docker { image 'node:20' }
}
stages {
stage('Build') {
steps {
sh 'npm install && npm run build'
}
}
}
}
```
See `doc/Jenkinsfile.example` for a full multi-stage example.
### 6.5 Pipeline durability and stuck builds
Jenkins has a known bug ([JENKINS-50407](https://issues.jenkins.io/browse/JENKINS-50407))
where pipeline flow executions can become corrupted. This happens when a build
is interrupted uncleanly — for example, by a timeout, manual abort, Jenkins
restart mid-build, or a disk issue. When it occurs, new pipeline builds will
silently fail: they show "Started by user ..." in the console output and
nothing else. The Jenkins log will contain warnings like:
```
WARNING o.j.p.w.f.FlowExecutionList$DefaultStorage#unregister:
<job>#<build> was not in the list to begin with: []
```
Two measures are in place to prevent and recover from this:
#### Prevention: Pipeline durability setting
By default, Jenkins aggressively writes pipeline state to disk at every step
so that running builds can be resumed after a crash. This persistence layer
is what gets corrupted. Since our builds run in ephemeral Docker containers
(DooD), there is nothing useful to resume — a fresh build is always needed.
Switching to "Performance-optimized" durability disables most of the state
persistence, which eliminates the main source of corruption and also makes
pipelines run faster.
After initial Jenkins setup:
1. Go to **Manage Jenkins** > **Configure System**
2. Find **Pipeline Speed/Durability Setting**
3. Change it to **Performance-optimized: much less durability**
4. Click **Save**
The tradeoff: if Jenkins crashes mid-build, the running build is lost and
cannot be resumed. This is acceptable because the Docker build container
is also lost on crash, so there is nothing to resume anyway.
#### Recovery: Startup cleanup script
The file `jenkins/init.groovy.d/clear-stuck-builds.groovy` is mounted into
the Jenkins container at `/var/jenkins_home/init.groovy.d/`. Jenkins
automatically executes all `.groovy` files in this directory on every startup.
The script iterates through all registered pipeline flow executions and
removes any that are marked as complete but are still tracked in the
execution list. This prevents stuck executions from blocking future builds.
If builds ever get stuck again (showing only "Started by user ..." with no
further output), simply restart the Jenkins container:
```bash
docker restart jenkins
```
The cleanup script will run automatically and clear the stuck state. No
manual deletion of build directories is needed.
## 7. Gerrit (Code Review)
### 7.1 Create host directories
```bash
mkdir -p /var/gerrit/{etc,git,db,index,cache}
chown -R 1000:1000 /var/gerrit # gerrit user inside container runs as UID 1000
```
### 7.2 Start Gerrit
Gerrit is part of the git-server stack. It requires `jenkins_network` to exist,
so Jenkins must be started first.
```bash
cd /root/Projects/bastion/git-server
docker compose -f server.yaml up -d gerrit
```
Gerrit shares `/var/git/repos` with git-server for repository access.
## 8. Nginx (Reverse Proxy + SSL)
Nginx must be started AFTER Git Server, Ghost, Jenkins, and Gerrit, because it joins
their networks as external.
### 8.1 Create host directories
```bash
# Nginx config directory
mkdir -p /var/nginx/conf
# Copy nginx.conf
cp /root/Projects/bastion/nginx/nginx.conf /var/nginx/conf/
# DH parameters (this takes a few minutes)
mkdir -p /var/dh_param
openssl dhparam -out /var/dh_param/dhparam-2048.pem 2048
# Let's Encrypt directories
mkdir -p /var/letsencrypt/etc
mkdir -p /var/letsencrypt/lts_site
# Copy the ACME challenge placeholder page
cp /root/Projects/bastion/nginx/letsencrypt/index.html /var/letsencrypt/lts_site/
```
### 8.2 First run — HTTP only (no SSL yet)
Before we have SSL certificates, we need to temporarily disable the SSL server
blocks so Nginx can start and serve the ACME challenge for certbot.
```bash
# Edit the nginx.conf copy on the host to comment out SSL server blocks
# Keep only the port 80 server block
nano /var/nginx/conf/nginx.conf
```
Comment out everything from `# Ghost — swave.lol` to the end of file.
Keep only the `listen 80` server block.
```bash
cd /root/Projects/bastion/nginx
docker compose up -d
```
### 8.3 Obtain SSL certificates
```bash
cd /root/Projects/bastion/nginx
bash run_certbot.sh
```
This requests certificates for:
- `swave.lol`
- `blog.swave.lol`
- `jenkins.swave.lol`
- `cgit.swave.lol`
- `gerrit.swave.lol`
IMPORTANT: All DNS records must be pointing to the server before running certbot.
### 8.4 Enable SSL
```bash
# Restore full nginx.conf with SSL blocks
cp /root/Projects/bastion/nginx/nginx.conf /var/nginx/conf/
# Restart Nginx
cd /root/Projects/bastion/nginx
docker compose restart
```
### 8.5 Verify
All services should now be accessible:
| URL | Service |
|-----|---------|
| `https://swave.lol` | Ghost (blog) |
| `https://blog.swave.lol` | Ghost (blog, alias) |
| `https://swave.lol/jenkins` | Jenkins (path-based) |
| `https://jenkins.swave.lol` | Jenkins (subdomain) |
| `https://swave.lol/cgit` | Cgit (path-based) |
| `https://cgit.swave.lol` | Cgit (subdomain) |
| `https://swave.lol/gerrit` | Gerrit (path-based) |
| `https://gerrit.swave.lol` | Gerrit (subdomain) |
| `http://<server-ip>:9000` | Portainer |
| `ssh://git@<server-ip>/repos/<repo>.git` | Git (SSH) |
| `git://<server-ip>/<repo>.git` | Git (daemon, read-only) |
## Network Architecture
```
git-network (172.22.0.0/16)
├── git-server (172.22.0.2)
├── cgit (172.22.0.3)
├── gerrit (172.22.0.4)
└── nginx (172.22.0.254)
ghost_network
├── ghost
├── ghost-db
├── activitypub (optional)
└── nginx
jenkins_network (172.23.0.0/16)
├── jenkins (172.23.0.2)
├── git-server (172.23.0.3)
├── gerrit (172.23.0.4)
└── nginx
```
Nginx sits on all networks so it can reverse proxy to every service.
## Startup Order
The correct order to start all services:
```bash
# 1. Portainer (standalone, no dependencies)
docker run -d --name="portainer" --restart on-failure \
-p 9000:9000 -p 8000:8000 \
-v /var/run/docker.sock:/var/run/docker.sock \
-v portainer_data:/data portainer/portainer-ce:latest
# 2. Git Server (creates git-network)
docker compose -f /root/Projects/bastion/git-server/server.yaml up -d
# 3. Ghost (creates ghost_network)
docker compose -f /root/Projects/bastion/ghost/compose.yml up -d
# 4. Jenkins (creates jenkins_network)
docker compose -f /root/Projects/bastion/jenkins/docker-compose.yaml up -d
# 5. Gerrit (joins git-network + jenkins_network, both must exist)
docker compose -f /root/Projects/bastion/git-server/server.yaml up -d gerrit
# 6. Nginx (joins all networks — must be last)
docker compose -f /root/Projects/bastion/nginx/docker-compose.yaml up -d
```
## Ports Summary
| Port | Service | Protocol |
|------|---------|----------|
| 22 | Git Server (SSH) | TCP |
| 80 | Nginx (HTTP, redirects to 443) | TCP |
| 443 | Nginx (HTTPS) | TCP |
| 2368 | Ghost (direct, for testing) | TCP |
| 8080 | Jenkins (direct, for testing) | TCP |
| 9000 | Portainer (web UI) | TCP |
| 9418 | Git Server (git daemon) | TCP |
| 50000 | Jenkins (agent communication) | TCP |
|