Skip to content

Commit a5bf30b

Browse files
committed
added deploy method
1 parent 88c3c1a commit a5bf30b

File tree

6 files changed

+237
-25
lines changed

6 files changed

+237
-25
lines changed

.circleci/config.yml

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ jobs:
1515
sudo chown -R circleci:circleci /usr/local/cargo
1616
fi
1717
- restore_cache:
18+
name: Restore Linux Rust Cache
1819
key: cargo-cache-linux-{{ checksum "Cargo.lock" }}-{{ checksum "Cargo.toml" }}
1920

2021
# - run:
@@ -34,9 +35,10 @@ jobs:
3435
3536
- run:
3637
name: Build Binary
37-
command: cargo build --release
38+
command: cargo build --release --target=x86_64-unknown-linux-gnu
3839

3940
- save_cache:
41+
name: Save Linux Rust Cache
4042
key: cargo-cache-linux-{{ checksum "Cargo.lock" }}-{{ checksum "Cargo.toml" }}
4143
paths:
4244
- "~/.cargo/"
@@ -46,25 +48,32 @@ jobs:
4648

4749
- run:
4850
name: "Generate hash file"
49-
command: "cd target/release && sha256sum library-loader > library-loader.checksum"
51+
command: "cd target/x86_64-unknown-linux-gnu/release && sha256sum library-loader > library-loader.sha256"
5052

5153
- run:
5254
name: "Verify hash"
53-
command: "cd target/release && sha256sum -c library-loader.checksum"
55+
command: "cd target/x86_64-unknown-linux-gnu/release && sha256sum -c library-loader.sha256"
5456

5557
- run:
5658
name: "Show hash"
57-
command: "cd target/release && cat library-loader.checksum"
59+
command: "cd target/x86_64-unknown-linux-gnu/release && cat library-loader.sha256"
5860

59-
- store_artifacts:
60-
name: Store Build
61-
path: "./target/release/library-loader"
62-
destination: "/linux/library-loader"
61+
- save_cache:
62+
name: Save Linux Build Cache
63+
key: ll-linux-dist-{{ .Environment.CIRCLE_SHA1 }}
64+
paths:
65+
- "./target/x86_64-unknown-linux-gnu/release/library-loader"
66+
- "./target/x86_64-unknown-linux-gnu/release/library-loader.sha256"
6367

64-
- store_artifacts:
65-
name: Store Build Checksum
66-
path: "./target/release/library-loader.checksum"
67-
destination: "/linux/library-loader.checksum"
68+
# - store_artifacts:
69+
# name: Store Build
70+
# path: "./target/x86_64-unknown-linux-gnu/release/library-loader"
71+
# destination: "/linux/library-loader"
72+
73+
# - store_artifacts:
74+
# name: Store Build Checksum
75+
# path: "./target/x86_64-unknown-linux-gnu/release/library-loader.sha256"
76+
# destination: "/linux/library-loader.sha256"
6877

6978
windows:
7079
docker:
@@ -80,6 +89,7 @@ jobs:
8089
sudo chown -R circleci:circleci /usr/local/cargo
8190
fi
8291
- restore_cache:
92+
name: Restore Windows Rust Cache
8393
key: cargo-cache-windows-{{ checksum "Cargo.lock" }}-{{ checksum "Cargo.toml" }}
8494

8595
- run:
@@ -121,39 +131,87 @@ jobs:
121131
command: cargo build --release --target=x86_64-pc-windows-gnu
122132

123133
- save_cache:
134+
name: Save Windows Rust Cache
124135
key: cargo-cache-windows-{{ checksum "Cargo.lock" }}-{{ checksum "Cargo.toml" }}
125136
paths:
126137
- "~/.cargo/"
127138
- "~/.rustup/"
128-
- "./target"
139+
- "./target/x86_64-pc-windows-gnu"
129140
- "/usr/local/cargo"
130141

131142
- run:
132143
name: "Generate hash file"
133-
command: "cd target/x86_64-pc-windows-gnu/release && sha256sum library-loader.exe > library-loader.exe.checksum"
144+
command: "cd target/x86_64-pc-windows-gnu/release && sha256sum library-loader.exe > library-loader.exe.sha256"
134145

135146
- run:
136147
name: "Verify hash"
137-
command: "cd target/x86_64-pc-windows-gnu/release && sha256sum -c library-loader.exe.checksum"
148+
command: "cd target/x86_64-pc-windows-gnu/release && sha256sum -c library-loader.exe.sha256"
138149

139150
- run:
140151
name: "Show hash"
141-
command: "cd target/x86_64-pc-windows-gnu/release && cat library-loader.exe.checksum"
152+
command: "cd target/x86_64-pc-windows-gnu/release && cat library-loader.exe.sha256"
153+
154+
- save_cache:
155+
name: Save Windows Build
156+
key: ll-windows-dist-{{ .Environment.CIRCLE_SHA1 }}
157+
paths:
158+
- "./target/x86_64-pc-windows-gnu/release/library-loader.exe"
159+
- "./target/x86_64-pc-windows-gnu/release/library-loader.exe.sha256"
142160

143-
- store_artifacts:
144-
name: Store Build
145-
path: "./target/x86_64-pc-windows-gnu/release/library-loader.exe"
146-
destination: "/windows/library-loader.exe"
161+
# - store_artifacts:
162+
# name: Store Build
163+
# path: "./target/x86_64-pc-windows-gnu/release/library-loader.exe"
164+
# destination: "/windows/library-loader.exe"
165+
166+
# - store_artifacts:
167+
# name: Store Build Checksum
168+
# path: "./target/x86_64-pc-windows-gnu/release/library-loader.exe.sha256"
169+
# destination: "/windows/library-loader.exe.sha256"
170+
171+
deploy:
172+
docker:
173+
- image: circleci/node:latest
174+
175+
steps:
176+
- checkout
147177

148-
- store_artifacts:
149-
name: Store Build Checksum
150-
path: "./target/x86_64-pc-windows-gnu/release/library-loader.exe.checksum"
151-
destination: "/windows/library-loader.exe.checksum"
178+
- restore_cache:
179+
name: Restore Deploy Node Cache
180+
key: deploy-cache-{{ checksum ".circleci/deploy/package.json" }}-{{ checksum ".circleci/deploy/package-lock.json" }}
181+
182+
- run:
183+
name: Install dependencies
184+
command: cd .circleci/deploy && npm install
185+
186+
- save_cache:
187+
name: Save Node Cache
188+
key: deploy-cache-{{ checksum ".circleci/deploy/package.json" }}-{{ checksum ".circleci/deploy/package-lock.json" }}
189+
paths:
190+
- "./.circleci/deploy/node_modules"
191+
192+
- restore_cache:
193+
name: Restore Linux Build
194+
key: ll-linux-dist-{{ .Environment.CIRCLE_SHA1 }}
195+
196+
- restore_cache:
197+
name: Restore Windows Build
198+
key: ll-windows-dist-{{ .Environment.CIRCLE_SHA1 }}
199+
200+
- run:
201+
name: Deploy
202+
command: node .circleci/deploy/deploy.js
152203

153204
workflows:
154205
version: 2
155206
build:
156207
jobs:
157208
- linux
158209
- windows
159-
# - macos
210+
- deploy:
211+
type: approval
212+
requires:
213+
- linux
214+
- windows
215+
filters:
216+
branches:
217+
only: master

.circleci/deploy/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

.circleci/deploy/deploy.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs');
4+
const process = require('process');
5+
const path = require('path');
6+
7+
const fetch = require('node-fetch');
8+
const semver = require('semver');
9+
10+
const File = require('./file');
11+
12+
const gh = {
13+
url: process.env['GITHUB_API_URL'] || 'https://api.github.com/',
14+
user: process.env['CIRCLE_PROJECT_USERNAME'],
15+
repo: process.env['CIRCLE_PROJECT_REPONAME'],
16+
branch: process.env['CIRCLE_BRANCH'],
17+
headers: {
18+
'Accept': 'application/vnd.github.v3+json',
19+
'Authorization': `Basic ${process.env['GITHUB_AUTH']}`
20+
}
21+
}
22+
23+
const files = [
24+
// Linux files
25+
new File('./target/x86_64-unknown-linux-gnu/release/library-loader'),
26+
new File('./target/x86_64-unknown-linux-gnu/release/library-loader.sha256'),
27+
// Windows files
28+
new File('./target/x86_64-pc-windows-gnu/release/library-loader.exe'),
29+
new File('./target/x86_64-pc-windows-gnu/release/library-loader.exe.sha256')
30+
];
31+
32+
let cargoToml = fs.readFileSync('Cargo.toml', 'utf8').toString().split('\n');
33+
let currentVersion = cargoToml.filter(l => l.includes('version = '))[0].split(' = ')[1].replace(/\"/g, '');
34+
35+
(async () => {
36+
37+
const releases = await (await fetch(`${gh.url}repos/${gh.user}/${gh.repo}/releases`, {
38+
headers: gh.headers
39+
})).json();
40+
41+
if (semver.lte(currentVersion, releases[0].tag_name)) {
42+
throw new Error('Current version is not newer than last release!');
43+
}
44+
45+
console.log('Creating new release!');
46+
47+
let newReleaseRes = await (await fetch(`${gh.url}repos/${gh.user}/${gh.repo}/releases`, {
48+
headers: gh.headers,
49+
method: 'POST',
50+
body: JSON.stringify({
51+
tag_name: currentVersion,
52+
target_commitish: gh.branch,
53+
name: `Release v${currentVersion}`,
54+
body: '',
55+
draft: false,
56+
prerelease: false
57+
})
58+
})).json();
59+
60+
let uploadUrl = new URL(newReleaseRes['upload_url'].replace('{?name,label}', ''));
61+
62+
files.forEach(async file => {
63+
64+
let baseName = path.basename(file.path);
65+
uploadUrl.searchParams.set('name', baseName);
66+
uploadUrl.searchParams.set('label', baseName);
67+
68+
let res = await fetch(uploadUrl, {
69+
headers: {
70+
...gh.headers,
71+
'Content-Type': file.mime,
72+
},
73+
method: 'POST',
74+
body: file.read()
75+
});
76+
77+
if (await res.status === 201) {
78+
console.log(`Uploaded file ${baseName}`)
79+
} else {
80+
console.error(`Failed to upload ${baseName}`)
81+
}
82+
83+
});
84+
85+
})();
86+
87+

.circleci/deploy/file.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { execSync } = require('child_process');
2+
const fs = require('fs');
3+
4+
module.exports = class File {
5+
6+
constructor(path) {
7+
8+
this.path = path;
9+
this.mime = null;
10+
11+
if (!fs.existsSync(this.path)) {
12+
throw new Error(`${this.path} does not exist`);
13+
}
14+
15+
let res = execSync(`file ${this.path} --mime-type`).toString('utf8');
16+
17+
let mime = res.split(': ')[1].trim();
18+
if (mime.length) {
19+
this.mime = mime;
20+
} else {
21+
throw new Error('Error getting mime-type');
22+
}
23+
24+
}
25+
26+
read() {
27+
28+
return fs.readFileSync(this.path)
29+
30+
}
31+
32+
}

.circleci/deploy/package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.circleci/deploy/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "deploy",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "deploy.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"node-fetch": "^2.6.0",
14+
"semver": "^6.3.0"
15+
}
16+
}

0 commit comments

Comments
 (0)