Skip to content

Commit

Permalink
added deploy method
Browse files Browse the repository at this point in the history
  • Loading branch information
olback committed Oct 26, 2019
1 parent 88c3c1a commit a5bf30b
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 25 deletions.
108 changes: 83 additions & 25 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
sudo chown -R circleci:circleci /usr/local/cargo
fi
- restore_cache:
name: Restore Linux Rust Cache
key: cargo-cache-linux-{{ checksum "Cargo.lock" }}-{{ checksum "Cargo.toml" }}

# - run:
Expand All @@ -34,9 +35,10 @@ jobs:
- run:
name: Build Binary
command: cargo build --release
command: cargo build --release --target=x86_64-unknown-linux-gnu

- save_cache:
name: Save Linux Rust Cache
key: cargo-cache-linux-{{ checksum "Cargo.lock" }}-{{ checksum "Cargo.toml" }}
paths:
- "~/.cargo/"
Expand All @@ -46,25 +48,32 @@ jobs:

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

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

- run:
name: "Show hash"
command: "cd target/release && cat library-loader.checksum"
command: "cd target/x86_64-unknown-linux-gnu/release && cat library-loader.sha256"

- store_artifacts:
name: Store Build
path: "./target/release/library-loader"
destination: "/linux/library-loader"
- save_cache:
name: Save Linux Build Cache
key: ll-linux-dist-{{ .Environment.CIRCLE_SHA1 }}
paths:
- "./target/x86_64-unknown-linux-gnu/release/library-loader"
- "./target/x86_64-unknown-linux-gnu/release/library-loader.sha256"

- store_artifacts:
name: Store Build Checksum
path: "./target/release/library-loader.checksum"
destination: "/linux/library-loader.checksum"
# - store_artifacts:
# name: Store Build
# path: "./target/x86_64-unknown-linux-gnu/release/library-loader"
# destination: "/linux/library-loader"

# - store_artifacts:
# name: Store Build Checksum
# path: "./target/x86_64-unknown-linux-gnu/release/library-loader.sha256"
# destination: "/linux/library-loader.sha256"

windows:
docker:
Expand All @@ -80,6 +89,7 @@ jobs:
sudo chown -R circleci:circleci /usr/local/cargo
fi
- restore_cache:
name: Restore Windows Rust Cache
key: cargo-cache-windows-{{ checksum "Cargo.lock" }}-{{ checksum "Cargo.toml" }}

- run:
Expand Down Expand Up @@ -121,39 +131,87 @@ jobs:
command: cargo build --release --target=x86_64-pc-windows-gnu

- save_cache:
name: Save Windows Rust Cache
key: cargo-cache-windows-{{ checksum "Cargo.lock" }}-{{ checksum "Cargo.toml" }}
paths:
- "~/.cargo/"
- "~/.rustup/"
- "./target"
- "./target/x86_64-pc-windows-gnu"
- "/usr/local/cargo"

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

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

- run:
name: "Show hash"
command: "cd target/x86_64-pc-windows-gnu/release && cat library-loader.exe.checksum"
command: "cd target/x86_64-pc-windows-gnu/release && cat library-loader.exe.sha256"

- save_cache:
name: Save Windows Build
key: ll-windows-dist-{{ .Environment.CIRCLE_SHA1 }}
paths:
- "./target/x86_64-pc-windows-gnu/release/library-loader.exe"
- "./target/x86_64-pc-windows-gnu/release/library-loader.exe.sha256"

- store_artifacts:
name: Store Build
path: "./target/x86_64-pc-windows-gnu/release/library-loader.exe"
destination: "/windows/library-loader.exe"
# - store_artifacts:
# name: Store Build
# path: "./target/x86_64-pc-windows-gnu/release/library-loader.exe"
# destination: "/windows/library-loader.exe"

# - store_artifacts:
# name: Store Build Checksum
# path: "./target/x86_64-pc-windows-gnu/release/library-loader.exe.sha256"
# destination: "/windows/library-loader.exe.sha256"

deploy:
docker:
- image: circleci/node:latest

steps:
- checkout

- store_artifacts:
name: Store Build Checksum
path: "./target/x86_64-pc-windows-gnu/release/library-loader.exe.checksum"
destination: "/windows/library-loader.exe.checksum"
- restore_cache:
name: Restore Deploy Node Cache
key: deploy-cache-{{ checksum ".circleci/deploy/package.json" }}-{{ checksum ".circleci/deploy/package-lock.json" }}

- run:
name: Install dependencies
command: cd .circleci/deploy && npm install

- save_cache:
name: Save Node Cache
key: deploy-cache-{{ checksum ".circleci/deploy/package.json" }}-{{ checksum ".circleci/deploy/package-lock.json" }}
paths:
- "./.circleci/deploy/node_modules"

- restore_cache:
name: Restore Linux Build
key: ll-linux-dist-{{ .Environment.CIRCLE_SHA1 }}

- restore_cache:
name: Restore Windows Build
key: ll-windows-dist-{{ .Environment.CIRCLE_SHA1 }}

- run:
name: Deploy
command: node .circleci/deploy/deploy.js

workflows:
version: 2
build:
jobs:
- linux
- windows
# - macos
- deploy:
type: approval
requires:
- linux
- windows
filters:
branches:
only: master
1 change: 1 addition & 0 deletions .circleci/deploy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
87 changes: 87 additions & 0 deletions .circleci/deploy/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env node

const fs = require('fs');
const process = require('process');
const path = require('path');

const fetch = require('node-fetch');
const semver = require('semver');

const File = require('./file');

const gh = {
url: process.env['GITHUB_API_URL'] || 'https://api.github.com/',
user: process.env['CIRCLE_PROJECT_USERNAME'],
repo: process.env['CIRCLE_PROJECT_REPONAME'],
branch: process.env['CIRCLE_BRANCH'],
headers: {
'Accept': 'application/vnd.github.v3+json',
'Authorization': `Basic ${process.env['GITHUB_AUTH']}`
}
}

const files = [
// Linux files
new File('./target/x86_64-unknown-linux-gnu/release/library-loader'),
new File('./target/x86_64-unknown-linux-gnu/release/library-loader.sha256'),
// Windows files
new File('./target/x86_64-pc-windows-gnu/release/library-loader.exe'),
new File('./target/x86_64-pc-windows-gnu/release/library-loader.exe.sha256')
];

let cargoToml = fs.readFileSync('Cargo.toml', 'utf8').toString().split('\n');
let currentVersion = cargoToml.filter(l => l.includes('version = '))[0].split(' = ')[1].replace(/\"/g, '');

(async () => {

const releases = await (await fetch(`${gh.url}repos/${gh.user}/${gh.repo}/releases`, {
headers: gh.headers
})).json();

if (semver.lte(currentVersion, releases[0].tag_name)) {
throw new Error('Current version is not newer than last release!');
}

console.log('Creating new release!');

let newReleaseRes = await (await fetch(`${gh.url}repos/${gh.user}/${gh.repo}/releases`, {
headers: gh.headers,
method: 'POST',
body: JSON.stringify({
tag_name: currentVersion,
target_commitish: gh.branch,
name: `Release v${currentVersion}`,
body: '',
draft: false,
prerelease: false
})
})).json();

let uploadUrl = new URL(newReleaseRes['upload_url'].replace('{?name,label}', ''));

files.forEach(async file => {

let baseName = path.basename(file.path);
uploadUrl.searchParams.set('name', baseName);
uploadUrl.searchParams.set('label', baseName);

let res = await fetch(uploadUrl, {
headers: {
...gh.headers,
'Content-Type': file.mime,
},
method: 'POST',
body: file.read()
});

if (await res.status === 201) {
console.log(`Uploaded file ${baseName}`)
} else {
console.error(`Failed to upload ${baseName}`)
}

});

})();


32 changes: 32 additions & 0 deletions .circleci/deploy/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { execSync } = require('child_process');
const fs = require('fs');

module.exports = class File {

constructor(path) {

this.path = path;
this.mime = null;

if (!fs.existsSync(this.path)) {
throw new Error(`${this.path} does not exist`);
}

let res = execSync(`file ${this.path} --mime-type`).toString('utf8');

let mime = res.split(': ')[1].trim();
if (mime.length) {
this.mime = mime;
} else {
throw new Error('Error getting mime-type');
}

}

read() {

return fs.readFileSync(this.path)

}

}
18 changes: 18 additions & 0 deletions .circleci/deploy/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .circleci/deploy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "deploy",
"version": "1.0.0",
"description": "",
"main": "deploy.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"node-fetch": "^2.6.0",
"semver": "^6.3.0"
}
}

0 comments on commit a5bf30b

Please sign in to comment.