Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 43 additions & 39 deletions .github/actions/deploy/action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: Uitrollen naar de server
description: Zet een image op de eigen server via SSH (zie deploy/README.nl.md). Slaat over met een waarschuwing als DEPLOY_HOST niet ingesteld is.
description: >-
Vraagt de eigen server om een image uit te rollen en wacht op het resultaat (zie deploy/README.nl.md).
De server haalt zelf op: deze stap maakt een GitHub-deployment (task deploy:server) aan, de dienst
projectx-agent op de server rolt uit en meldt success of failure terug. Slaat over met een waarschuwing
als DEPLOY_URL niet ingesteld is.

inputs:
environment:
Expand All @@ -9,67 +13,67 @@ inputs:
description: Volledige image-naam, bv. ghcr.io/dafkedd/projectx-eid@sha256:…
required: true
url:
description: Adres om na het uitrollen te controleren (vars.DEPLOY_URL)
description: Adres van de omgeving (vars.DEPLOY_URL), wordt na het uitrollen gecontroleerd
required: false
host:
required: false
port:
required: false
user:
required: false
ssh-key:
required: false
known-hosts:
required: false
registry-token:
description: Token waarmee de server het image van ghcr.io haalt (github.token, alleen tijdens deze job geldig)
token:
description: github.token, met permissions deployments write
required: true
timeout-minutes:
description: Hoe lang wachten op de server
required: false
default: "10"

runs:
using: composite
steps:
- name: Uitrollen
id: deploy
shell: bash
env:
ENVIRONMENT: ${{ inputs.environment }}
IMAGE: ${{ inputs.image }}
DEPLOY_HOST: ${{ inputs.host }}
DEPLOY_PORT: ${{ inputs.port }}
DEPLOY_USER: ${{ inputs.user }}
DEPLOY_SSH_KEY: ${{ inputs.ssh-key }}
DEPLOY_KNOWN_HOSTS: ${{ inputs.known-hosts }}
REGISTRY_TOKEN: ${{ inputs.registry-token }}
URL: ${{ inputs.url }}
GH_TOKEN: ${{ inputs.token }}
WAIT: ${{ inputs.timeout-minutes }}
run: |
if [ -z "$DEPLOY_HOST" ]; then
echo "::warning::Geen DEPLOY_HOST: $ENVIRONMENT niet uitgerold (zie deploy/README.nl.md)."
if [ -z "$URL" ]; then
echo "::warning::Geen DEPLOY_URL voor $ENVIRONMENT: niet uitgerold (zie deploy/README.nl.md)."
echo "skipped=true" >> "$GITHUB_OUTPUT"
exit 0
fi
if [ -z "$DEPLOY_SSH_KEY" ] || [ -z "$DEPLOY_KNOWN_HOSTS" ]; then
echo "::error::DEPLOY_SSH_KEY en DEPLOY_KNOWN_HOSTS zijn nodig om naar $DEPLOY_HOST uit te rollen."
exit 1
fi
dir="$RUNNER_TEMP/ssh"
install -d -m 700 "$dir"
printf '%s\n' "$DEPLOY_SSH_KEY" > "$dir/key"
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > "$dir/known_hosts"
chmod 600 "$dir/key" "$dir/known_hosts"
# De server staat voor deze sleutel alleen "deploy <omgeving> <image>" toe; het token gaat via stdin.
printf '%s' "$REGISTRY_TOKEN" | ssh -i "$dir/key" -p "${DEPLOY_PORT:-22}" \
-o UserKnownHostsFile="$dir/known_hosts" -o StrictHostKeyChecking=yes -o BatchMode=yes \
-o ConnectTimeout=20 -o ServerAliveInterval=15 \
"${DEPLOY_USER:-deploy}@$DEPLOY_HOST" "deploy $ENVIRONMENT $IMAGE"
rm -rf "$dir"
body=$(jq -nc --arg ref "$GITHUB_SHA" --arg env "$ENVIRONMENT" --arg image "$IMAGE" --arg url "$URL" \
--argjson prod "$([ "$ENVIRONMENT" = production ] && echo true || echo false)" \
'{ref: $ref, environment: $env, task: "deploy:server", auto_merge: false, required_contexts: [],
production_environment: $prod, description: "Uitrollen op de eigen server",
payload: {image: $image, url: $url}}')
id=$(gh api "repos/$GITHUB_REPOSITORY/deployments" --input - --jq .id <<<"$body")
echo "Deployment $id aangemaakt; wachten tot de server hem oppikt (projectx-agent)..."
deadline=$(( $(date +%s) + WAIT * 60 ))
last=""
while [ "$(date +%s)" -lt "$deadline" ]; do
status=$(gh api "repos/$GITHUB_REPOSITORY/deployments/$id/statuses?per_page=1" --jq '.[0] | "\(.state)\t\(.description)"' || true)
state=${status%%$'\t'*}
if [ -n "$status" ] && [ "$status" != "$last" ]; then echo "server: ${status/$'\t'/: }"; last=$status; fi
case "$state" in
success) exit 0 ;;
failure|error) echo "::error::Uitrollen naar $ENVIRONMENT mislukt: ${status#*$'\t'}"; exit 1 ;;
esac
sleep 10
done
echo "::error::De server reageerde niet binnen $WAIT minuten. Draait projectx-agent? (journalctl -u projectx-agent)"
exit 1

- name: Controleren
if: inputs.url != '' && inputs.host != ''
if: steps.deploy.outputs.skipped != 'true'
shell: bash
env:
ENVIRONMENT: ${{ inputs.environment }}
URL: ${{ inputs.url }}
run: |
for i in $(seq 1 20); do
if curl -fsS -o /dev/null --max-time 10 "$URL"; then
echo "OK: $URL"
echo "### ${{ inputs.environment }}: $URL" >> "$GITHUB_STEP_SUMMARY"
echo "### $ENVIRONMENT: $URL" >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
sleep 6
Expand Down
32 changes: 9 additions & 23 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ name: Release
# 5. release hetzelfde image naar productie, tag v<versie> + GitHub-release met de Windows-setup
# (environment "production"), en @dafkedd/eid naar npmjs.com en GitHub Packages
# als die versie daar nog niet staat
# Uitrollen gaat via SSH naar de eigen server (deploy/README.nl.md). Zonder secret DEPLOY_HOST wordt er
# niet uitgerold, alleen gewaarschuwd.
# Uitrollen: de eigen server haalt het image zelf op (deploy/README.nl.md); GitHub hoeft niet bij de
# server te kunnen. Zonder variabele DEPLOY_URL op een environment wordt er niet uitgerold, alleen gewaarschuwd.
# Met "Required reviewers" op een environment wacht die stap op een knop "Approve" in het tabblad Actions.
# Bestaat tag v<versie> al, dan wordt er geen release gemaakt: verhoog eerst de versie (npm run version:patch).

Expand Down Expand Up @@ -75,20 +75,15 @@ jobs:
url: ${{ vars.DEPLOY_URL }}
permissions:
contents: read
packages: read
deployments: write
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/deploy
with:
environment: qa
image: ${{ needs.image.outputs.image }}
url: ${{ vars.DEPLOY_URL }}
host: ${{ secrets.DEPLOY_HOST }}
port: ${{ secrets.DEPLOY_PORT }}
user: ${{ secrets.DEPLOY_USER }}
ssh-key: ${{ secrets.DEPLOY_SSH_KEY }}
known-hosts: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
registry-token: ${{ github.token }}
token: ${{ github.token }}

acc:
name: ACC
Expand All @@ -100,20 +95,15 @@ jobs:
url: ${{ vars.DEPLOY_URL }}
permissions:
contents: read
packages: read
deployments: write
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/deploy
with:
environment: acc
image: ${{ needs.image.outputs.image }}
url: ${{ vars.DEPLOY_URL }}
host: ${{ secrets.DEPLOY_HOST }}
port: ${{ secrets.DEPLOY_PORT }}
user: ${{ secrets.DEPLOY_USER }}
ssh-key: ${{ secrets.DEPLOY_SSH_KEY }}
known-hosts: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
registry-token: ${{ github.token }}
token: ${{ github.token }}

release:
name: Release
Expand All @@ -124,8 +114,9 @@ jobs:
url: ${{ vars.DEPLOY_URL }}
permissions:
contents: write
packages: write # image lezen + @dafkedd/eid naar GitHub Packages
packages: write # @dafkedd/eid naar GitHub Packages
id-token: write # npm provenance
deployments: write
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -136,12 +127,7 @@ jobs:
environment: production
image: ${{ needs.image.outputs.image }}
url: ${{ vars.DEPLOY_URL }}
host: ${{ secrets.DEPLOY_HOST }}
port: ${{ secrets.DEPLOY_PORT }}
user: ${{ secrets.DEPLOY_USER }}
ssh-key: ${{ secrets.DEPLOY_SSH_KEY }}
known-hosts: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
registry-token: ${{ github.token }}
token: ${{ github.token }}
- uses: actions/setup-node@v4
with:
node-version: 22
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ like in Azure DevOps:
| ACC | `acc` | Deploy the same image to https://acc.dafkedd.be |
| Release | `production` | Same image to https://prod.dafkedd.be, then tag `v<version>` + GitHub release with the Windows setup, zip and sha256 |

Deployment goes over SSH to our own Debian server with Docker and Caddy. Setup and secrets:
Our own Debian server with Docker and Caddy fetches each new version itself (no inbound SSH needed). Setup:
[deploy/README.md](deploy/README.md).

Making a release:
Expand Down
2 changes: 1 addition & 1 deletion README.nl.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ draaien alleen de tests (`ci.yml`). Alleen een push naar `release` start de rele
| ACC | `acc` | Hetzelfde image naar https://acc.dafkedd.be |
| Release | `production` | Hetzelfde image naar https://prod.dafkedd.be, dan tag `v<versie>` + GitHub-release met de Windows-setup, zip en sha256 |

Uitrollen gaat via SSH naar onze eigen Debian-server met Docker en Caddy. Instellen en secrets:
Onze eigen Debian-server met Docker en Caddy haalt elke nieuwe versie zelf op (geen inkomende SSH nodig). Instellen:
[deploy/README.nl.md](deploy/README.nl.md).

Een release maken:
Expand Down
88 changes: 49 additions & 39 deletions deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,29 @@ ProjectX runs on one Debian server, in Docker, with [Caddy](https://caddyserver.
| ACC | https://acc.dafkedd.be | `projectx-acc` |
| Production | https://prod.dafkedd.be | `projectx-production` |

On a push to `release`, the release pipeline (`.github/workflows/release.yml`) builds one Docker image,
pushes it to `ghcr.io` and deploys **the same** image to QA, ACC and production in turn. GitHub logs in
over SSH as the user `deploy`. On the server that key can do nothing but start `/opt/projectx/bin/deploy`:
no shell, no reading files. If a new version does not become healthy, the server puts the previous
version back.
## How it works

The server **pulls**: GitHub never needs to reach the server, so the SSH port can stay closed or limited
to a few IP addresses.

1. On a push to `release`, the pipeline (`.github/workflows/release.yml`) builds one Docker image and
pushes it to `ghcr.io` (public; the image contains no secrets).
2. The QA, ACC and Release steps each create a GitHub deployment (task `deploy:server`) with that image,
and wait.
3. The `projectx-agent` service on the server checks for a new deployment every 20 seconds, pulls the
image, starts it and reports `success` or `failure` back. If the new version does not become healthy,
the server puts the previous one back.
4. The pipeline sees the answer and then checks the environment's address.

The agent uses a GitHub token that can only read and update this repo's deployments, and it only runs
deployments created by the pipeline itself (`github-actions[bot]`).

## One-time setup

### 1. DNS

At the registrar of `dafkedd.be`, create three A records pointing to the server:
At the registrar of `dafkedd.be`, create three A records pointing to the server. Leave existing records
(such as MX for mail) alone.

| Name | Type | Value |
| --- | --- | --- |
Expand All @@ -34,42 +46,31 @@ Check (may take a while):
nslookup qa.dafkedd.be
```

### 2. Prepare the server
Ports 80 and 443 must reach the server from outside (for Let's Encrypt and the websites).

Log in (on OVH as `debian`; root usually may not log in directly) and run the script with `sudo`. It installs Docker, creates the user `deploy`, sets up Caddy and
prints the values for GitHub at the end. You can run it again later.
### 2. GitHub token for the server

```bash
ssh -p 22822 debian@51.75.77.10
```
Go to [Fine-grained tokens → Generate new token](https://github.com/settings/personal-access-tokens/new):

```bash
curl -fsSL https://raw.githubusercontent.com/DafkeDD/ProjectX-eID/developer/deploy/setup-server.sh -o setup-server.sh
sudo bash setup-server.sh --host 51.75.77.10 --ssh-port 22822 --qa qa.dafkedd.be --acc acc.dafkedd.be --prod prod.dafkedd.be --email jij@dafkedd.be
```
- **Token name**: `projectx-server`
- **Expiration**: e.g. 1 year (set a reminder to renew it)
- **Repository access**: *Only select repositories* → `DafkeDD/ProjectX-eID`
- **Permissions → Repository permissions → Deployments**: *Read and write*

Let's Encrypt only uses the email address to warn you when a certificate is about to expire.
If your hosting provider has a firewall, open ports 80 and 443 (and keep 22822 open).
Click **Generate token** and copy it (it starts with `github_pat_`). Paste it nowhere except in step 3.

### 3. Secrets in GitHub
### 3. Prepare the server

Go to **Settings → Secrets and variables → Actions → Secrets → New repository secret** and create
these five, with the values the script printed:

| Secret | Value |
| --- | --- |
| `DEPLOY_HOST` | `51.75.77.10` |
| `DEPLOY_PORT` | `22822` |
| `DEPLOY_USER` | `deploy` |
| `DEPLOY_KNOWN_HOSTS` | the lines starting with `[51.75.77.10]:22822` |
| `DEPLOY_SSH_KEY` | the private key, including `-----BEGIN …` and `-----END …` |

Then delete the copy of the key on the server:
Log in to the server and run the script as root (or with `sudo`). It installs Docker, sets up Caddy and
the agent, and asks for the token at the end. You can run it again later.

```bash
rm /opt/projectx/github-deploy-key
curl -fsSL https://raw.githubusercontent.com/DafkeDD/ProjectX-eID/developer/deploy/setup-server.sh -o setup-server.sh
bash setup-server.sh --qa qa.dafkedd.be --acc acc.dafkedd.be --prod prod.dafkedd.be --email jij@dafkedd.be
```

Let's Encrypt only uses the email address to warn you when a certificate is about to expire.

### 4. Address per environment

Go to **Settings → Environments**, open `qa`, `acc` and `production` (or create them with
Expand All @@ -81,21 +82,21 @@ Go to **Settings → Environments**, open `qa`, `acc` and `production` (or creat
| `acc` | `https://acc.dafkedd.be` |
| `production` | `https://prod.dafkedd.be` |

After deploying, the pipeline checks that address. Also set **Required reviewers** on `acc` and
`production`, so the pipeline waits for your approval before those steps.
Without `DEPLOY_URL` the pipeline skips that environment with a warning. Also set **Required reviewers**
on `acc` and `production`, so the pipeline waits for your approval before those steps.

## Deploying

Release as described in the main README: PR `main → release`. The **Actions** tab shows
**Image → QA → ACC → Release**. As long as `DEPLOY_HOST` is not set, QA, ACC and production skip the
deployment with a warning.
**Image → QA → ACC → Release**, and the "Uitrollen" step shows what the server reports back.

**Back to a previous version:** in **Actions**, open the run of that version and choose
**Re-run jobs** on the step of the environment. That puts exactly that run's image back.
**Back to a previous version:** in **Actions**, open the run of that version and choose **Re-run jobs**
on the step of the environment. That creates a new deployment with that run's image.

## Looking around on the server

```bash
journalctl -u projectx-agent -f
docker ps
docker logs --tail 100 projectx-production
docker logs --tail 100 caddy
Expand All @@ -104,9 +105,18 @@ cat /opt/projectx/production/.env

| Path | What |
| --- | --- |
| `/opt/projectx/bin/deploy` | Script GitHub calls over SSH |
| `/opt/projectx/bin/agent` | The `projectx-agent` service: fetch deployments and roll them out |
| `/opt/projectx/bin/deploy` | Deploy one environment: `deploy <qa\|acc\|production> <image>` |
| `/opt/projectx/agent.env` | The GitHub token (readable only by `deploy`) |
| `/opt/projectx/<environment>/` | `compose.yml` and `.env` (which image runs) |
| `/opt/projectx/caddy/` | Caddy's `Caddyfile` and `compose.yml` |

**Renewing the token:** create a new token (step 2), then on the server:

```bash
rm /opt/projectx/agent.env
bash setup-server.sh --qa qa.dafkedd.be --acc acc.dafkedd.be --prod prod.dafkedd.be --email jij@dafkedd.be
```

To change a domain: run `setup-server.sh` again with the new names, and update
`installer/eid-installer.json` (the websites the eID program allows).
Loading
Loading