-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskfile.yml
More file actions
186 lines (174 loc) · 8.12 KB
/
Taskfile.yml
File metadata and controls
186 lines (174 loc) · 8.12 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
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
# Taskfile for webserver related tasks
# # E.g. created droplet, deploying website, etc.
version: "3"
includes:
droplet:
taskfile: taskfile-droplet.yml
tasks:
add-ssh-key:
desc: |
Generate a new SSH key pair, add the public key to known hosts on the vps, and copy the private key to clipboard.
Useful for setting up ssh keys on github actions etc.
Requires:
- optional SSH_NAME: The ssh name for the VPS (default webserver)
- optional DESC: A description for the key (default is empty)
requires:
vars: [SSH_NAME, DESC]
vars:
SSH_NAME: '{{default "webserver" .SSH_NAME}}'
DESC: '{{default "" .DESC}}'
cmds:
# Generate a new SSH key pair without a passphrase
- ssh-keygen -f /tmp/temp_ssh_key -N ""
- defer: rm /tmp/temp_ssh_key /tmp/temp_ssh_key.pub
# Add the DESC as a comment to the private key
- sed -i '$ s/$/ {{ .DESC }}/' /tmp/temp_ssh_key.pub
- ssh {{ .SSH_NAME }} "echo $(cat /tmp/temp_ssh_key.pub) >> ~/.ssh/authorized_keys"
# Copy the private key to the clipboard
- xsel --clipboard < /tmp/temp_ssh_key
# Print the private key to the console (for manual copying)
- cat /tmp/temp_ssh_key
new-slideshow:
desc: "Create a new reveal.js presentation in current directory. Optionally provide SUBDIR to specify a subdirectory."
vars:
SUBDIR: '{{default "" .SUBDIR}}'
TEMPLATE_DIR: "{{.TASKFILE_DIR}}/other-templates/revealjs-template"
preconditions:
- test -d "{{.TEMPLATE_DIR}}"
dir: "{{.TASKFILE_DIR}}" # Otherwise uses parent taskfile dir
cmds:
- echo "Creating new reveal.js presentation in {{.USER_WORKING_DIR}}/{{.SUBDIR}}"
- scripts/new-revealjs.sh "{{.TEMPLATE_DIR}}" "{{.USER_WORKING_DIR}}" "{{.SUBDIR}}"
new-static-site:
desc: |
Initialize a new static site on the webserver droplet.
Requires:
- SITE_NAME
- optional DOMAIN (to create a new caddy config from template)
requires:
vars: [SSH_NAME, SITE_NAME]
vars:
SSH_NAME: '{{default "webserver" .SSH_NAME}}'
DOMAIN: '{{default "" .DOMAIN}}'
dir: "{{.TASKFILE_DIR}}" # Otherwise uses parent taskfile dir
cmds:
- |
echo "Initializing new static site on droplet {{.SSH_NAME}}:sites/{{.SITE_NAME}}"
- scripts/setup-webserver-new-static-site.sh "{{.SSH_NAME}}" "{{.SITE_NAME}}" "{{.DOMAIN}}"
new-backend-site:
desc: |
Initialize a new site that also runs a backend service.
Requires:
- SITE_NAME: The name of the site (used for directory and caddy config)
- DOMAIN: The domain name to use for the site
- DOCKER_USER: The username for the docker registry
- optional REGISTRY: The docker registry to use (default ghcr.io)
requires:
vars: [SSH_NAME, SITE_NAME, DOCKER_USER]
vars:
REGISTRY: '{{default "ghcr.io" .REGISTRY}}'
SSH_NAME: '{{default "webserver" .SSH_NAME}}'
DOMAIN: '{{default "" .DOMAIN}}'
_FULL_REGISTRY_PATH: "{{.REGISTRY}}/{{.DOCKER_USER}}/{{.SITE_NAME}}-backend:latest"
dir: "{{.TASKFILE_DIR}}" # Otherwise uses parent taskfile dir
cmds:
- |
echo "Initializing new backend site on droplet {{.SSH_NAME}}:sites/{{.SITE_NAME}}"
- scripts/setup-webserver-new-backend-site.sh "{{.SSH_NAME}}" "{{.SITE_NAME}}" "{{._FULL_REGISTRY_PATH}}" "{{.DOMAIN}}"
remove-site:
desc: "Remove a site from the webserver droplet (requires SITE_NAME)"
prompt: 'Are you sure you want to remove site ''{{.SITE_NAME}}'' from the server "{{.SSH_NAME}}?"'
requires:
vars: [SSH_NAME, SITE_NAME]
dir: "{{.TASKFILE_DIR}}" # Otherwise uses parent taskfile dir
cmds:
- ssh {{.SSH_NAME}} 'bash -s' < scripts/webserver-remove-site.sh "{{.SITE_NAME}}"
- echo "Removing the local site configuration"
- rm -rf "{{.TASKFILE_DIR}}/sites-enabled/{{.SITE_NAME}}.caddy"
deploy-static:
desc: "Deploy website (static files) to droplet (requires STATIC_DIR and SITE_NAME)"
summary: |
Deploy static files to the webserver droplet.
Vars:
- SSH_NAME: The name of the droplet to deploy to (default "webserver")
- SITE_NAME: The name of the site to deploy
- optional STATIC_DIR: The directory containing the static files (default static)
- optional PROJECT_DIR: The root directory for the site (default user working directory)
requires:
vars: [SSH_NAME, SITE_NAME]
vars:
SSH_NAME: '{{default "webserver" .SSH_NAME}}'
STATIC_DIR: '{{default "static" .STATIC_DIR}}'
PROJECT_DIR: "{{default .USER_WORKING_DIR .PROJECT_DIR}}"
_FULL_DIR: "{{.PROJECT_DIR}}/{{.STATIC_DIR}}/"
preconditions:
- sh: test -d "{{._FULL_DIR}}"
msg: |
Directory "{{._FULL_DIR}}" does not exist (maybe need to change the STATIC_DIR variable?
(or run from different dir or explicitly set PROJECT_DIR))
cmds:
- |
echo "Deploying static files from {{._FULL_DIR}} to server at {{.SSH_NAME}}:srv/www/{{.SITE_NAME}}"
# First copy to the webserver user directory
- rsync -avz --delete {{._FULL_DIR}}/ {{.SSH_NAME}}:sites/{{.SITE_NAME}}/static/
- task: caddy-reload
vars:
SSH_NAME: "{{.SSH_NAME}}"
deploy-docker-backend:
desc: "Deploy a backend docker image to the webserver"
summary: |
Build and push a backend docker image to the webserver droplet.
Vars:
- SSH_NAME: The name of the droplet to deploy to
- SITE_NAME: The name of the site to deploy
- DOCKER_USER: The username for the docker registry
- optional REGISTRY: The docker registry to use (default ghcr.io)
- optional DOCKERFILE_PATH: The path to the Dockerfile (default Dockerfile)
- optional PROJECT_DIR: The root directory for the site (default user working directory)
requires:
vars: [SSH_NAME, SITE_NAME, DOCKER_USER]
vars:
REGISTRY: '{{default "ghcr.io" .REGISTRY}}'
DOCKER_USER: '{{default "<set default here>" .DOCKER_USER}}'
DOCKERFILE_PATH: '{{default "Dockerfile" .DOCKERFILE_PATH}}'
PROJECT_DIR: "{{default .USER_WORKING_DIR .PROJECT_DIR}}"
_FULL_DOCKERFILE_PATH: "{{.PROJECT_DIR}}/{{.DOCKERFILE_PATH}}"
_FULL_REGISTRY_PATH: "{{.REGISTRY}}/{{.DOCKER_USER}}/{{.SITE_NAME}}-backend:latest"
cmds:
- |
echo "Deploying backend docker image to server at {{.SSH_NAME}}:sites/{{.SITE_NAME}}"
- docker build -t {{._FULL_REGISTRY_PATH}} -f {{._FULL_DOCKERFILE_PATH}} {{.PROJECT_DIR}}
- echo "If the following command fails -- make sure you are logged in with write permissions (e.g. 'echo $GH_PAT | docker login -u <username> ghcr.io --password-stdin')"
- docker push {{._FULL_REGISTRY_PATH}}
- task: caddy-reload
vars:
SSH_NAME: "{{.SSH_NAME}}"
caddy-reload:
desc: "Reload the Caddy webserver after sending any configuration changes"
requires:
vars: [SSH_NAME]
vars:
SSH_NAME: '{{default "webserver" .SSH_NAME}}'
dir: "{{.TASKFILE_DIR}}" # Otherwise uses parent taskfile dir
cmds:
- scripts/send-webserver-config.sh "{{.SSH_NAME}}"
- ssh {{.SSH_NAME}} "bash -s " < scripts/webserver-update-static-files.sh
# Do a quick caddy reload (sufficient for minor changes) (or start if no services running)
# NOTE: May require logging into container repos first via `echo $GH_PAT | docker login -u <username> ghcr.io --password-stdin`
- ssh {{.SSH_NAME}} "docker exec caddy caddy reload --config /etc/caddy/Caddyfile || docker compose up -d"
- task: _caddy-restart-compose-if-changed
vars:
SSH_NAME: "{{.SSH_NAME}}"
# Definitely restart the compose since the image changed
- ssh {{.SSH_NAME}} "docker compose pull && docker compose up -d"
_caddy-restart-compose-if-changed:
silent: true
internal: true
desc: "Fully restarts the docker compose services if the compose file has changed (locally)"
requires:
vars: [SSH_NAME]
sources:
- caddy-compose.yaml
cmds:
# Fully stop and start in case any changes made to compose file
- ssh {{.SSH_NAME}} "docker compose pull && docker compose down && docker compose up -d"