-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcreate-deploy-key.sh
More file actions
executable file
·50 lines (37 loc) · 1.01 KB
/
create-deploy-key.sh
File metadata and controls
executable file
·50 lines (37 loc) · 1.01 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
#!/bin/bash
# This script creates a deploy key (readonly) and saves it to your project.
set -e
set -o pipefail
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Set the GITHUB_TOKEN env variable."
exit 1
fi
URI=https://api.github.com
API_VERSION=v3
API_HEADER="Accept: application/vnd.github.${API_VERSION}+json"
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
create_deploy_key(){
repo="$1"
key="$2"
echo "Creating jenkins deploy key for $repo"
curl -i -sSL -XPOST -H "${AUTH_HEADER}" -H "${API_HEADER}" --data-binary @- "${URI}/repos/${repo}/keys" <<-EOF
{
"title": "jenkins@yoserver.cool",
"read_only": true,
"key": "${key}"
}
EOF
}
main(){
repo=$1
tmpd=$(mktemp -d --suffix=deploy_key)
if [[ "$repo" == "" ]]; then
echo "Pass a repo as the first argument: ex. jessfraz/jenkins-dsl" >&2
exit 1
fi
# create the ssh key
ssh-keygen -f "${tmpd}/id_ed25519" -t ed25519 -N ''
create_deploy_key "$repo" "$(cat "${tmpd}/id_ed25519.pub")"
echo "You can find your public and private key in $tmpd."
}
main "$@"