New release process

This commit is contained in:
Alexander Zobnin
2020-06-04 12:52:56 +03:00
parent a416bcfc4f
commit ec48fa0e55
8 changed files with 205 additions and 3 deletions

View File

@@ -120,6 +120,43 @@ jobs:
- store_artifacts:
path: /tmp/circleci-test-results
make-github-release:
working_directory: ~/alexanderzobnin/grafana-zabbix
docker:
- image: circleci/golang:1.13-node
environment:
CI_GIT_USER: CircleCI
CI_GIT_EMAIL: ci@grafana.com
steps:
- checkout
- restore_cache:
keys:
- dependency-cache-npm-{{ checksum "yarn.lock" }}
- restore_cache:
keys:
- dependency-cache-go-{{ checksum "go.sum" }}
- run:
name: Install Dependencies
command: 'make install'
no_output_timeout: 15m
- save_cache:
key: dependency-cache-npm-{{ checksum "yarn.lock" }}
paths:
- ./node_modules
- save_cache:
key: dependency-cache-go-{{ checksum "go.sum" }}
paths:
- ./vendor
- /go/pkg/mod
- run:
name: Build plugin for all platforms
command: 'make dist'
no_output_timeout: 15m
- run:
name: Package plugin
command: './.circleci/make-package.sh'
no_output_timeout: 15m
make-release:
working_directory: ~/alexanderzobnin/grafana-zabbix
docker:
@@ -233,7 +270,7 @@ workflows:
filters: *filter-only-release
- test:
filters: *filter-only-release
- make-release:
- make-github-release:
requires:
- build
- codespell

View File

@@ -0,0 +1,31 @@
const axios = require('axios');
const githubURL = (owner, repo) => `https://api.github.com/repos/${owner}/${repo}`;
class GithubClient {
constructor(owner, repo, required = false) {
const username = process.env.GITHUB_USERNAME;
const token = process.env.GITHUB_ACCESS_TOKEN;
const clientConfig = {
baseURL: githubURL(owner, repo),
timeout: 10000,
};
if (required && !username && !token) {
throw new Error('operation needs a GITHUB_USERNAME and GITHUB_ACCESS_TOKEN environment variables');
}
if (username && token) {
clientConfig.auth = { username: username, password: token };
}
this.client = this.createClient(clientConfig);
}
createClient(clientConfig) {
return axios.create(clientConfig);
}
}
module.exports = GithubClient;

View File

@@ -0,0 +1,84 @@
const GithubClient = require('./githubClient');
const fs = require('fs');
const path = require('path');
const GRAFANA_ZABBIX_OWNER = 'alexanderzobnin';
const GRAFANA_ZABBIX_REPO = 'grafana-zabbix';
const github = new GithubClient(GRAFANA_ZABBIX_OWNER, GRAFANA_ZABBIX_REPO, true);
async function main() {
const tag = process.env.CIRCLE_TAG;
const tagRegex = /v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)/;
if (!tagRegex.test(tag)) {
console.error(`Release tag should has format v1.2.3[-meta], got ${tag}`);
process.exit(1);
}
const releaseVersion = tag.slice(1);
let releaseId;
try {
const latestRelease = await github.client.get(`releases/tags/v${releaseVersion}`);
releaseId = latestRelease.data.id;
} catch (reason) {
if (reason.response.status !== 404) {
// 404 just means no release found. Not an error. Anything else though, re throw the error
throw reason;
} else {
console.error(`No release found`);
process.exit(1);
}
}
try {
const updateReleaseResponse = await github.client.put(`releases/${releaseId}`, {
tag_name: `v${releaseVersion}`,
name: `${releaseVersion}`,
body: `Grafana-Zabbix ${releaseVersion}`,
draft: false,
prerelease: false,
});
await publishAssets(
`./grafana-zabbix-${releaseVersion}.zip`,
`https://uploads.github.com/repos/${GRAFANA_ZABBIX_OWNER}/${GRAFANA_ZABBIX_REPO}/releases/${updateReleaseResponse.data.id}/assets`
);
} catch (reason) {
console.error(reason.data || reason.response || reason);
// Rethrow the error so that we can trigger a non-zero exit code to circle-ci
throw reason;
}
}
async function publishAssets(fileName, destUrl) {
// Add the assets. Loop through files in the ci/dist folder and upload each asset.
const fileStat = fs.statSync(`${fileName}`);
const fileData = fs.readFileSync(`${fileName}`);
return await github.client.post(`${destUrl}?name=${fileName}`, fileData, {
headers: {
'Content-Type': resolveContentType(path.extname(fileName)),
'Content-Length': fileStat.size,
},
maxContentLength: fileStat.size * 2 * 1024 * 1024,
});
}
const resolveContentType = (extension) => {
if (extension.startsWith('.')) {
extension = extension.substr(1);
}
switch (extension) {
case 'zip':
return 'application/zip';
case 'json':
return 'application/json';
case 'sha1':
return 'text/plain';
default:
return 'application/octet-stream';
}
};
main();

27
.circleci/make-package.sh Executable file
View File

@@ -0,0 +1,27 @@
#!/bin/bash
# Exit script if you try to use an uninitialized variable.
set -o nounset
# Exit script if a statement returns a non-true return value.
set -o errexit
# Use the error status of the first failure, rather than that of the last item in a pipeline.
set -o pipefail
RELEASE_VER=$(echo "$CIRCLE_TAG" | grep -Po "(?<=v)[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)")
if [ -z "$RELEASE_VER" ]; then
echo "No release version provided"
exit 1
fi
if [[ $RELEASE_VER =~ ^[0-9]+(\.[0-9]+){2}(-.+|[^-.]*) ]]; then
echo "Preparing release $RELEASE_VER"
else
echo "Release should has format 1.2.3[-meta], got $RELEASE_VER"
exit 1
fi
# Create zip package
PACKAGE_NAME="grafana-zabbix-${RELEASE_VER}.zip"
echo "packaging into $PACKAGE_NAME"
mv ./dist alexanderzobnin-zabbix-app
zip -r $PACKAGE_NAME ./alexanderzobnin-zabbix-app