Repository navigation
161 lines (148 loc) · 6.85 KB
/
Copy pathdeploy-stack.yml
File metadata and controls
161 lines (148 loc) · 6.85 KB
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
name: Deploy Stack
# Deploys a stack or prunes previews; values and SSH key come from the KF console.
on:
workflow_call:
inputs:
action:
required: true
type: string
description: "'deploy' or 'prune-previews'"
environment:
required: true
type: string
description: "the console environment, and the GitHub Environment the job runs in: 'production' or 'sandbox'"
image_tag:
required: false
type: string
description: "image tag to deploy"
stack_name:
required: false
type: string
description: "docker swarm stack name"
hostname:
required: false
type: string
description: "public hostname for this deployment"
stack_file:
required: false
type: string
description: "compose file to deploy (stack.yml or stack.preview.yml)"
uses_gateway:
required: false
type: boolean
default: false
description: "if true, deploy behind the shared Caddy gateway"
keep_previews:
required: false
type: string
default: ""
description: "prune-previews: the PR numbers whose preview stacks stay, space-separated"
permissions: {}
jobs:
deploy:
if: inputs.action == 'deploy'
runs-on: ubuntu-latest
timeout-minutes: 15
environment: ${{ inputs.environment }}
permissions:
contents: read
packages: read
id-token: write
env:
KF: npx --yes --ignore-scripts @knowledgefutures/kf@0.7.0
KF_CONSOLE_URL: ${{ vars.KF_CONSOLE_URL || 'https://console.knowledgefutures.org' }}
steps:
# For infra/deploy-remote.sh at the commit being deployed.
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
persist-credentials: false
- name: Check inputs
env:
ENVIRONMENT: ${{ inputs.environment }}
IMAGE_TAG: ${{ inputs.image_tag }}
STACK_NAME: ${{ inputs.stack_name }}
PUBLIC_HOST: ${{ inputs.hostname }}
STACK_FILE: ${{ inputs.stack_file }}
run: |
set -euo pipefail
fail() { echo "::error::$1"; exit 1; }
[[ "$ENVIRONMENT" =~ ^(production|sandbox)$ ]] || fail "environment ${ENVIRONMENT} is not production or sandbox"
[[ "$IMAGE_TAG" =~ ^[A-Za-z0-9._-]+$ ]] || fail "image tag ${IMAGE_TAG} has characters a deploy won't send"
[[ "$STACK_NAME" =~ ^[a-z0-9-]+$ ]] || fail "stack name ${STACK_NAME} is not [a-z0-9-]"
[[ "$PUBLIC_HOST" =~ ^[a-z0-9.-]+$ ]] || fail "hostname ${PUBLIC_HOST} is not a hostname"
[[ "$STACK_FILE" =~ ^stack(\.preview)?\.yml$ ]] || fail "stack file ${STACK_FILE} is not stack.yml or stack.preview.yml"
- name: Set up Node.js
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version: 24
- name: Load the deploy key
id: ssh
env:
ENVIRONMENT: ${{ inputs.environment }}
run: $KF ci ssh-agent -e "$ENVIRONMENT"
# Registry token, then the values, on stdin.
- name: Deploy stack
env:
ENVIRONMENT: ${{ inputs.environment }}
DEPLOY_USER: ${{ steps.ssh.outputs.DEPLOY_USER }}
DEPLOY_HOST: ${{ steps.ssh.outputs.DEPLOY_HOST }}
REGISTRY_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REGISTRY_USER: ${{ github.actor }}
REPO: ${{ github.repository }}
SHA: ${{ github.sha }}
IMAGE_TAG: ${{ inputs.image_tag }}
STACK_NAME: ${{ inputs.stack_name }}
PUBLIC_HOST: ${{ inputs.hostname }}
STACK_FILE: ${{ inputs.stack_file }}
USES_GATEWAY: ${{ inputs.uses_gateway }}
run: |
set -euo pipefail
remote=$(base64 -w0 infra/deploy-remote.sh)
{
printf '%s\n' "$REGISTRY_TOKEN"
$KF ci secrets --runtime -e "$ENVIRONMENT"
} | ssh -o BatchMode=yes -o StrictHostKeyChecking=yes -l "$DEPLOY_USER" -- "$DEPLOY_HOST" \
"bash -c \"\$(echo $remote | base64 -d)\" deploy-remote deploy '$REPO' '$SHA' '$IMAGE_TAG' '$STACK_NAME' '$PUBLIC_HOST' '$STACK_FILE' '$USES_GATEWAY' '$REGISTRY_USER'"
# Removes preview stacks of PRs not in keep_previews.
prune-previews:
if: inputs.action == 'prune-previews'
runs-on: ubuntu-latest
timeout-minutes: 10
environment: ${{ inputs.environment }}
permissions:
contents: read
id-token: write
env:
KF: npx --yes --ignore-scripts @knowledgefutures/kf@0.7.0
KF_CONSOLE_URL: ${{ vars.KF_CONSOLE_URL || 'https://console.knowledgefutures.org' }}
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
persist-credentials: false
- name: Check inputs
env:
ENVIRONMENT: ${{ inputs.environment }}
KEEP: ${{ inputs.keep_previews }}
run: |
set -euo pipefail
[[ "$ENVIRONMENT" == sandbox ]] || { echo "::error::previews are sandbox's"; exit 1; }
[[ "$KEEP" =~ ^[0-9\ ]*$ ]] || { echo "::error::keep_previews is not PR numbers"; exit 1; }
- name: Set up Node.js
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version: 24
- name: Load the deploy key
id: ssh
env:
ENVIRONMENT: ${{ inputs.environment }}
run: $KF ci ssh-agent -e "$ENVIRONMENT"
- name: Prune previews
env:
DEPLOY_USER: ${{ steps.ssh.outputs.DEPLOY_USER }}
DEPLOY_HOST: ${{ steps.ssh.outputs.DEPLOY_HOST }}
KEEP: ${{ inputs.keep_previews }}
run: |
set -euo pipefail
remote=$(base64 -w0 infra/deploy-remote.sh)
ssh -o BatchMode=yes -o StrictHostKeyChecking=yes -l "$DEPLOY_USER" -- "$DEPLOY_HOST" \
"bash -c \"\$(echo $remote | base64 -d)\" deploy-remote prune-previews '$KEEP'" </dev/null