Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit c9e418a

Browse files
committed
improve status code handling and add readme
1 parent b2e87ef commit c9e418a

File tree

4 files changed

+114
-15
lines changed

4 files changed

+114
-15
lines changed

.icons/github.svg

Lines changed: 1 addition & 0 deletions
Loading

github-upload-public-key/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
display_name: Github Upload Public Key
3+
description: Automates uploading Coder public key to Github so users don't have to.
4+
icon: ../.icons/github.svg
5+
maintainer_github: f0ssel
6+
verified: false
7+
tags: [helper]
8+
---
9+
10+
# github-upload-public-key
11+
12+
<!-- Describes what this module does -->
13+
14+
```tf
15+
module "github-upload-public-key" {
16+
source = "registry.coder.com/modules/github-upload-public-key/coder"
17+
version = "1.0.13"
18+
agent_id = coder_agent.example.id
19+
}
20+
```
21+
22+
<!-- Add a screencast or screenshot here put them in .images directory -->
23+
24+
## Examples
25+
26+
### Example 1
27+
28+
Install the Dracula theme from [OpenVSX](https://open-vsx.org/):
29+
30+
```tf
31+
module "MODULE_NAME" {
32+
source = "registry.coder.com/modules/MODULE_NAME/coder"
33+
version = "1.0.2"
34+
agent_id = coder_agent.example.id
35+
extensions = [
36+
"dracula-theme.theme-dracula"
37+
]
38+
}
39+
```
40+
41+
Enter the `<author>.<name>` into the extensions array and code-server will automatically install on start.
42+
43+
### Example 2
44+
45+
Configure VS Code's [settings.json](https://code.visualstudio.com/docs/getstarted/settings#_settingsjson) file:
46+
47+
```tf
48+
module "MODULE_NAME" {
49+
source = "registry.coder.com/modules/MODULE_NAME/coder"
50+
version = "1.0.2"
51+
agent_id = coder_agent.example.id
52+
extensions = [ "dracula-theme.theme-dracula" ]
53+
settings = {
54+
"workbench.colorTheme" = "Dracula"
55+
}
56+
}
57+
```
58+
59+
### Example 3
60+
61+
Run code-server in the background, don't fetch it from GitHub:
62+
63+
```tf
64+
module "MODULE_NAME" {
65+
source = "registry.coder.com/modules/MODULE_NAME/coder"
66+
version = "1.0.2"
67+
agent_id = coder_agent.example.id
68+
offline = true
69+
}
70+
```

github-upload-public-key/main.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ resource "coder_script" "github_upload_public_key" {
1818
agent_id = var.agent_id
1919
script = templatefile("${path.module}/run.sh", {
2020
CODER_OWNER_SESSION_TOKEN : data.coder_workspace.me.owner_session_token,
21-
CODER_ACCESS_URL : data.coder_workspace.me.access_url
21+
CODER_ACCESS_URL : data.coder_workspace.me.access_url,
2222
})
2323
display_name = "Github Upload Public Key"
2424
icon = "/icon/github.svg"
2525
run_on_start = true
26-
start_blocks_login = true
2726
}

github-upload-public-key/run.sh

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,77 @@ fi
1717

1818
if [ -z "$GITHUB_TOKEN" ]; then
1919
echo "No GITHUB_TOKEN in the workspace environment!"
20+
echo "The user must be authenticated with Github before this script can be run."
2021
exit 1
2122
fi
2223

23-
PUBLIC_KEY_NAME="$CODER_ACCESS_URL Workspaces"
24-
2524
echo "Fetching Coder public SSH key..."
26-
PUBLIC_KEY=$(curl "$CODER_ACCESS_URL/api/v2/users/me/gitsshkey" \
25+
PUBLIC_KEY_RESPONSE=$(curl -L -s \
26+
-w "%{http_code}" \
2727
-H 'accept: application/json' \
2828
-H "cookie: coder_session_token=$CODER_OWNER_SESSION_TOKEN" \
29-
--fail \
30-
-s \
31-
| jq -r '.public_key'
29+
"$CODER_ACCESS_URL/api/v2/users/me/gitsshkey"
3230
)
31+
PUBLIC_KEY_RESPONSE_STATUS=$(tail -n1 <<< "$PUBLIC_KEY_RESPONSE")
32+
PUBLIC_KEY_BODY=$(sed \$d <<< "$PUBLIC_KEY_RESPONSE")
33+
34+
if [ "$PUBLIC_KEY_RESPONSE_STATUS" -ne 200 ]; then
35+
echo "Failed to fetch Coder public SSH key with status code $PUBLIC_KEY_RESPONSE_STATUS!"
36+
echo "$PUBLIC_KEY_BODY"
37+
exit 1
38+
fi
39+
40+
PUBLIC_KEY=$(jq -r '.public_key' <<< "$PUBLIC_KEY_BODY")
41+
echo "Coder public SSH key found!"
3342

3443
if [ -z "$PUBLIC_KEY" ]; then
3544
echo "No Coder public SSH key found!"
3645
exit 1
3746
fi
3847

3948
echo "Fetching GitHub public SSH keys..."
40-
GITHUB_MATCH=$(curl \
49+
GITHUB_KEYS_RESPONSE=$(curl -L -s \
50+
-w "%{http_code}" \
4151
-H "Accept: application/vnd.github+json" \
4252
-H "Authorization: Bearer $GITHUB_TOKEN" \
4353
-H "X-GitHub-Api-Version: 2022-11-28" \
44-
--fail \
45-
-s \
46-
https://api.github.com/user/keys \
47-
| jq -r --arg PUBLIC_KEY "$PUBLIC_KEY" '.[] | select(.key == $PUBLIC_KEY) | .key'
54+
https://api.github.com/user/keys
4855
)
56+
GITHUB_KEYS_RESPONSE_STATUS=$(tail -n1 <<< "$GITHUB_KEYS_RESPONSE")
57+
GITHUB_KEYS_RESPONSE_BODY=$(sed \$d <<< "$GITHUB_KEYS_RESPONSE")
58+
59+
if [ "$GITHUB_KEYS_RESPONSE_STATUS" -ne 200 ]; then
60+
echo "Failed to fetch Coder public SSH key with status code $GITHUB_KEYS_RESPONSE_STATUS!"
61+
echo "$GITHUB_KEYS_RESPONSE_BODY"
62+
exit 1
63+
fi
64+
65+
GITHUB_MATCH=$(jq -r --arg PUBLIC_KEY "$PUBLIC_KEY" '.[] | select(.key == $PUBLIC_KEY) | .key' <<< "$GITHUB_KEYS_RESPONSE_BODY")
4966

5067
if [ "$PUBLIC_KEY" = "$GITHUB_MATCH" ]; then
5168
echo "Coder public SSH key is already uploaded to GitHub!"
5269
exit 0
5370
fi
71+
5472
echo "Coder public SSH key not found in GitHub keys!"
5573
echo "Uploading Coder public SSH key to GitHub..."
56-
curl -L \
74+
CODER_PUBLIC_KEY_NAME="$CODER_ACCESS_URL Workspaces"
75+
UPLOAD_RESPONSE=$(curl -L -s \
5776
-X POST \
77+
-w "%{http_code}" \
5878
-H "Accept: application/vnd.github+json" \
5979
-H "Authorization: Bearer $GITHUB_TOKEN" \
6080
-H "X-GitHub-Api-Version: 2022-11-28" \
6181
https://api.github.com/user/keys \
62-
-d "{\"title\":\"$PUBLIC_KEY_NAME\",\"key\":\"$PUBLIC_KEY\"}"
82+
-d "{\"title\":\"$CODER_PUBLIC_KEY_NAME\",\"key\":\"nah\"}"
83+
)
84+
UPLOAD_RESPONSE_STATUS=$(tail -n1 <<< "$UPLOAD_RESPONSE")
85+
UPLOAD_RESPONSE_BODY=$(sed \$d <<< "$UPLOAD_RESPONSE")
86+
87+
if [ "$UPLOAD_RESPONSE_STATUS" -ne 201 ]; then
88+
echo "Failed to upload Coder public SSH key with status code $UPLOAD_RESPONSE_STATUS!"
89+
echo "$UPLOAD_RESPONSE_BODY"
90+
exit 1
91+
fi
6392

6493
echo "Coder public SSH key uploaded to GitHub!"

0 commit comments

Comments
 (0)