Skip to content

Commit 250971e

Browse files
authored
Containers: Add VS Code tasks and improve overall documentation & flexibility (#12)
* make START_DIR configurable * fix permissions order * fix start dir bug * fix logic * make START_DIR * cool loggins * reuse clone function * i'm a bash noob * even more of a bash noob * add hashed info * add ability to add tasks * add vs code instructions for task * rclone fix * document vscode tasks * fix labels * mention additional variables * add better docs for dev tools * support rclone flags
1 parent 46e8e13 commit 250971e

File tree

8 files changed

+132
-29
lines changed

8 files changed

+132
-29
lines changed

Dockerfile

+17-5
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,27 @@ ENV SHELL=/bin/bash
1313
RUN sudo apt-get update && sudo apt-get install unzip -y
1414
RUN curl https://rclone.org/install.sh | sudo bash
1515

16-
# You can add custom software and dependencies for your environment here. Some examples:
17-
18-
# RUN code-server --install-extension esbenp.prettier-vscode
19-
# RUN sudo apt-get install -y build-essential
20-
# RUN COPY myTool /home/coder/myTool
16+
# Copy rclone tasks to /tmp, to potentially be used
17+
COPY deploy-container/rclone-tasks.json /tmp/rclone-tasks.json
2118

2219
# Fix permissions for code-server
2320
RUN sudo chown -R coder:coder /home/coder/.local
2421

22+
# You can add custom software and dependencies for your environment below
23+
# -----------
24+
25+
# Install a VS Code extension:
26+
# Note: we use a different marketplace than VS Code. See https://github.com/cdr/code-server/blob/main/docs/FAQ.md#differences-compared-to-vs-code
27+
# RUN code-server --install-extension esbenp.prettier-vscode
28+
29+
# Install apt packages:
30+
# RUN sudo apt-get install -y ubuntu-make
31+
32+
# Copy files:
33+
# COPY deploy-container/myTool /home/coder/myTool
34+
35+
# -----------
36+
2537
# Port
2638
ENV PORT=8080
2739

deploy-container/README.md

+19-10
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ Docker Hub: `bencdr/code-server-deploy-container`
1313

1414
## Environment variables:
1515

16-
| Variable Name | Description | Default Value |
17-
| ------------- | ---------------------------------------------------------- | ------------- |
18-
| `PASSWORD` | Password for code-server |
19-
| `USE_LINK` | Use code-server --link instead of a password (coming soon) | false |
20-
| `GIT_REPO` | A git repository to clone | |
21-
16+
| Variable Name | Description | Default Value |
17+
| ----------------- | ------------------------------------------------------------------------------------------------ | ------------------- |
18+
| `PASSWORD` | Password for code-server | |
19+
| `HASHED_PASSWORD` | Overrrides PASSWORD. [SHA-256 hash](https://xorbin.com/tools/sha256-hash-calculator) of password |
20+
| `USE_LINK` | Use code-server --link instead of a password (coming soon) | false |
21+
| `GIT_REPO` | A git repository to clone | |
22+
| `START_DIR` | The directory code-server opens (and clones repos in) | /home/coder/project |
2223
---
2324

25+
Other code-server environment variables (such as `CODE_SERVER_CONFIG`) can also be used. See the [code-server FAQ](https://github.com/cdr/code-server/blob/main/docs/FAQ.md) for details.
26+
2427
## 💾 Persist your filesystem with `rclone`
2528

2629
This image has built-in support for [rclone](https://rclone.org/) so that your files don't get lost when code-server is re-deployed.
@@ -44,18 +47,24 @@ Now, you can add the following the environment variables in the code-server clou
4447

4548
| Environment Variable | Description | Default Value | Required |
4649
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- | -------- |
47-
| RCLONE_DATA | the encoded rclone config you copied in step 3 | n/a ||
50+
| RCLONE_DATA | the encoded rclone config you copied in step 3 | n/a | |
4851
| RCLONE_REMOTE_NAME | the name of the remote you added in step 2.<br />check with `$ rclone listremotes` | code-server-remote | |
4952
| RCLONE_SOURCE | source directory to sync files in the code-server container | the project directory: `/home/coder/project` | |
5053
| RCLONE_DESTINATION | the path in the remote that rclone syncs to. change this if you have multiple code-server environments, or if you want to better organize your files. | code-server-files | |
51-
54+
| RCLONE_VSCODE_TASKS | import push and pull shortcuts into VS Code ![rclone screenshot from VS Code](../img/rclone-vscode-tasks.png) | true |
55+
| RCLONE_AUTO_PUSH | automatically push files on startup if the rclone remote is empty (environment -> rclone remote) | true | |
56+
| RCLONE_AUTO_PULL | automatically pull files on startup if the rclone remote is not empty (rclone -> environment remote) | true | |
57+
| RCLONE_FLAGS | additional flags to attach to the push and pull script. type `rclone help flags for a list." | | |
5258
```sh
5359

54-
# How to use:
60+
# --- How to use ---
5561

62+
# Terminal:
5663
$ sh /home/coder/push_remote.sh # save your uncomitted files to the remote
57-
5864
$ sh /home/coder/pull_remote.sh # get latest files from the remote
65+
66+
# In VS Code:
67+
# ctrl + P, run task: push_remote or pull_remote
5968
```
6069

6170
---

deploy-container/entrypoint.sh

+56-14
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,81 @@
11
#!/bin/bash
22

3-
START_DIR=/home/coder/project
3+
START_DIR="${START_DIR:-/home/coder/project}"
4+
5+
PREFIX="deploy-code-server"
6+
7+
mkdir -p $START_DIR
8+
9+
# function to clone the git repo or add a user's first file if no repo was specified.
10+
project_init () {
11+
[ -z "${GIT_REPO}" ] && echo "[$PREFIX] No GIT_REPO specified" && echo "Example file. Have questions? Join us at https://community.coder.com" > $START_DIR/coder.txt || git clone $GIT_REPO $START_DIR
12+
}
413

514
# add rclone config and start rclone, if supplied
615
if [[ -z "${RCLONE_DATA}" ]]; then
7-
echo "RCLONE_DATA is not specified. Files will not persist"
16+
echo "[$PREFIX] RCLONE_DATA is not specified. Files will not persist"
817

9-
# Clone the git repo, if it exists
10-
[ -z "${GIT_REPO}" ] && echo "No GIT_REPO specified"; git clone $GIT_REPO $START_DIR
18+
# start the project
19+
project_init
1120

1221
else
13-
echo "Copying rclone config..."
22+
echo "[$PREFIX] Copying rclone config..."
1423
mkdir -p /home/coder/.config/rclone/
1524
touch /home/coder/.config/rclone/rclone.conf
1625
echo $RCLONE_DATA | base64 -d > /home/coder/.config/rclone/rclone.conf
1726

27+
# defasult to true
28+
RCLONE_VSCODE_TASKS="${RCLONE_VSCODE_TASKS:-true}"
29+
RCLONE_AUTO_PUSH="${RCLONE_AUTO_PUSH:-true}"
30+
RCLONE_AUTO_PULL="${RCLONE_AUTO_PULL:-true}"
31+
32+
if [ $RCLONE_VSCODE_TASKS = "true" ]; then
33+
# copy our tasks config to VS Code
34+
echo "[$PREFIX] Applying VS Code tasks for rclone"
35+
cp /tmp/rclone-tasks.json /home/coder/.local/share/code-server/User/tasks.json
36+
# install the extension to add to menu bar
37+
code-server --install-extension actboy168.tasks&
38+
else
39+
# user specified they don't want to apply the tasks
40+
echo "[$PREFIX] Skipping VS Code tasks for rclone"
41+
fi
42+
43+
44+
1845
# Full path to the remote filesystem
1946
RCLONE_REMOTE_PATH=${RCLONE_REMOTE_NAME:-code-server-remote}:${RCLONE_DESTINATION:-code-server-files}
2047
RCLONE_SOURCE_PATH=${RCLONE_SOURCE:-$START_DIR}
21-
echo "rclone sync $RCLONE_SOURCE_PATH $RCLONE_REMOTE_PATH -vv" > /home/coder/push_remote.sh
22-
echo "rclone sync $RCLONE_REMOTE_PATH $RCLONE_SOURCE_PATH -vv" > /home/coder/pull_remote.sh
48+
echo "rclone sync $RCLONE_SOURCE_PATH $RCLONE_REMOTE_PATH $RCLONE_FLAGS -vv" > /home/coder/push_remote.sh
49+
echo "rclone sync $RCLONE_REMOTE_PATH $RCLONE_SOURCE_PATH $RCLONE_FLAGS -vv" > /home/coder/pull_remote.sh
2350
chmod +x push_remote.sh pull_remote.sh
2451

2552
if rclone ls $RCLONE_REMOTE_PATH; then
26-
# grab the files from the remote instead of re-cloning the git repo
27-
echo "Pulling existing files from remote..."
28-
/home/coder/pull_remote.sh&
53+
54+
if [ $RCLONE_AUTO_PULL = "true" ]; then
55+
# grab the files from the remote instead of running project_init()
56+
echo "[$PREFIX] Pulling existing files from remote..."
57+
/home/coder/pull_remote.sh&
58+
else
59+
# user specified they don't want to apply the tasks
60+
echo "[$PREFIX] Auto-pull is disabled"
61+
fi
62+
2963
else
30-
# we need to clone the git repo and sync
31-
echo "Pushing initial files to remote..."
32-
[ -z "${GIT_REPO}" ] && echo "No GIT_REPO specified" && mkdir -p $START_DIR && echo "intial file" > $START_DIR/file.txt; git clone $GIT_REPO $START_DIR
33-
/home/coder/push_remote.sh&
64+
65+
if [ $RCLONE_AUTO_PUSH = "true" ]; then
66+
# we need to clone the git repo and sync
67+
echo "[$PREFIX] Pushing initial files to remote..."
68+
project_init
69+
/home/coder/push_remote.sh&
70+
else
71+
# user specified they don't want to apply the tasks
72+
echo "[$PREFIX] Auto-push is disabled"
73+
fi
74+
3475
fi
3576

3677
fi
3778

79+
echo "[$PREFIX] Starting code-server..."
3880
# Now we can run code-server with the default entrypoint
3981
/usr/bin/entrypoint.sh --bind-addr 0.0.0.0:8080 $START_DIR

deploy-container/myTool/test.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
echo "This is a demo tool that could be brought into the code-server workspace"

deploy-container/rclone-tasks.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "push_remote",
6+
"type": "shell",
7+
"command": "sh /home/coder/push_remote.sh",
8+
"presentation": {
9+
"reveal": "always"
10+
},
11+
"problemMatcher": [],
12+
"options": {
13+
"statusbar": {
14+
"label": "$(repo-push) rclone: push"
15+
}
16+
}
17+
},
18+
{
19+
"label": "pull_remote",
20+
"type": "shell",
21+
"command": "sh /home/coder/pull_remote.sh",
22+
"presentation": {
23+
"reveal": "always"
24+
},
25+
"problemMatcher": [],
26+
"options": {
27+
"statusbar": {
28+
"label": "$(repo-pull) rclone: pull"
29+
}
30+
}
31+
}
32+
]
33+
}

guides/heroku.md

+2
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ Press the button in the top right of the repo, or or click to [use this template
5050
1. Head back to Heroku and notice a new deployment has started. After it has completed, you can use these tools in your environment.
5151

5252
1. (Optional): [Configure rclone](https://github.com/cdr/deploy-code-server/tree/main/deploy-container#-persist-your-filesystem-with-rclone) so that you can save your VS Code config and files without commiting
53+
54+
See the [deploy-container README](../deploy-container) for other config vars for your environment.

guides/railway.md

+2
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@ You also need to specity a `PASSWORD` and a `GIT_REPO` to clone in your environm
4242
1. Head back to Railway and notice a new deployment was created. After it has completed, you can use these tools in your environment.
4343

4444
1. (Optional): [Configure rclone](https://github.com/cdr/deploy-code-server/tree/main/deploy-container#-persist-your-filesystem-with-rclone) so that you can save your VS Code config and files without commiting
45+
46+
See the [deploy-container README](../deploy-container) for other config vars for your environment.

img/rclone-vscode-tasks.png

6.44 KB
Loading

0 commit comments

Comments
 (0)