-
Notifications
You must be signed in to change notification settings - Fork 24
/
deploy.rs
98 lines (90 loc) · 3.23 KB
/
deploy.rs
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
/*
* Original deploy.rs source from:
* https://github.com/rust-lang/simpleinfra/blob/master/setup-deploy-keys/src/deploy.rs
*
* Licenced under the MIT license:
*
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the
* Software without restriction, including without
* limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice
* shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
use std::env;
use std::fs;
use std::io::{Read, Write};
use std::os::unix::net::UnixStream;
use std::os::unix::prelude::*;
use std::process::{Command, Stdio};
fn main() {
let slug = env::var("GITHUB_REPOSITORY").unwrap();
let key = env::var("GITHUB_DEPLOY_KEY").unwrap();
let socket = "/tmp/.github-deploy-socket";
run(Command::new("ssh-agent").arg("-a").arg(&socket));
while UnixStream::connect(&socket).is_err() {
std::thread::sleep(std::time::Duration::from_millis(5));
}
let mut decode = Command::new("base64")
.arg("-d")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
decode
.stdin
.take()
.unwrap()
.write_all(&key.as_bytes())
.unwrap();
let mut key = Vec::new();
decode.stdout.take().unwrap().read_to_end(&mut key).unwrap();
decode.wait().unwrap();
let path = "_the_key";
fs::write(&path, key).unwrap();
fs::set_permissions(&path, fs::Permissions::from_mode(0o600)).unwrap();
run(Command::new("ssh-add")
.arg(&path)
.env("SSH_AUTH_SOCK", &socket));
fs::remove_file(&path).unwrap();
let sha = env::var("GITHUB_SHA").unwrap();
let msg = format!("Deploy {} to gh-pages", sha);
drop(fs::remove_dir_all(".git"));
run(Command::new("git").arg("init"));
run(Command::new("git")
.arg("config")
.arg("user.name")
.arg("Deploy from CI"));
run(Command::new("git").arg("config").arg("user.email").arg(""));
run(Command::new("git").arg("add").arg("."));
run(Command::new("git").arg("commit").arg("-m").arg(&msg));
run(Command::new("git")
.arg("push")
.arg(format!("[email protected]:{}", slug))
.arg("master:gh-pages")
.env("GIT_SSH_COMMAND", "ssh -o StrictHostKeyChecking=no")
.env("SSH_AUTH_SOCK", &socket)
.arg("-f"));
}
fn run(cmd: &mut Command) {
println!("{:?}", cmd);
let status = cmd.status().unwrap();
assert!(status.success());
}