Skip to content

Commit

Permalink
Github release script and workflow (subquery#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
lamcc21 authored Oct 12, 2021
1 parent ebe4a98 commit 232c084
Show file tree
Hide file tree
Showing 6 changed files with 4,541 additions and 963 deletions.
44 changes: 43 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,46 +70,88 @@ jobs:

- name: build
run: yarn workspaces foreach run build
#Publish to npm

#Publish to npm
- name: Publish Common
if: steps.changed-common.outputs.changed == 'true'
working-directory: packages/common
run: echo "Changes exist in common" && yarn npm publish --access public
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Release Common
id: release-common
if: steps.changed-common.outputs.changed == 'true'
run: node .github/workflows/scripts/gh-release-script.js ./packages/common
env:
REPO_TOKEN: ${{ secrets.REPO_TOKEN }}

- name: Publish Cli
if: steps.changed-cli.outputs.changed == 'true'
working-directory: packages/cli
run: echo "Changes exist in cli" && yarn npm publish --access public
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Release Cli
id: release-cli
if: steps.changed-cli.outputs.changed == 'true'
run: node .github/workflows/scripts/gh-release-script.js ./packages/cli
env:
REPO_TOKEN: ${{ secrets.REPO_TOKEN }}

- name: Publish Types
if: steps.changed-types.outputs.changed == 'true'
working-directory: packages/types
run: echo "Changes exist in types" && yarn npm publish --access public
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Release Types
id: release-types
if: steps.changed-cli.outputs.changed == 'true'
run: node .github/workflows/scripts/gh-release-script.js ./packages/types
env:
REPO_TOKEN: ${{ secrets.REPO_TOKEN }}

- name: Publish Node
if: steps.changed-node.outputs.changed == 'true'
working-directory: packages/node
run: echo "Changes exist in node" && yarn npm publish --access public
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Release Node
id: release-node
if: steps.changed-node.outputs.changed == 'true'
run: node .github/workflows/scripts/gh-release-script.js ./packages/node
env:
REPO_TOKEN: ${{ secrets.REPO_TOKEN }}

- name: Publish Query
if: steps.changed-query.outputs.changed == 'true'
working-directory: packages/query
run: echo "Changes exist in query" && yarn npm publish --access public
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Release Query
id: release-query
if: steps.changed-query.outputs.changed == 'true'
run: node .github/workflows/scripts/gh-release-script.js ./packages/query
env:
REPO_TOKEN: ${{ secrets.REPO_TOKEN }}

- name: Publish Validator
if: steps.changed-validator.outputs.changed == 'true'
working-directory: packages/validator
run: echo "Changes exist in validator" && yarn npm publish --access public
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Release Validator
id: release-validator
if: steps.changed-validator.outputs.changed == 'true'
run: node .github/workflows/scripts/gh-release-script.js ./packages/validator
env:
REPO_TOKEN: ${{ secrets.REPO_TOKEN }}
79 changes: 79 additions & 0 deletions .github/workflows/scripts/gh-release-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const fs = require('fs');
const { exit } = require('process');
const core = require('@actions/core');
const { request } = require('@octokit/request');

const myArgs = process.argv.slice(2);
const pJson = require(`${myArgs[0]}/package.json`);

const version = pJson.version;
const repoName = pJson.name;

function checkForBetaVersion(version) {
if (version.includes('-')){
exit(0); //skip this package but continue trying to release others
}
}

function gatherReleaseInfo(logPath) {
const changeLogs = fs.readFileSync(logPath, 'utf8');
const regex = /## \[([0-9]+(\.[0-9]+)+)] - [0-9]{4}-[0-9]{2}-[0-9]{2}/i;

let lines = changeLogs.split(/\n/);
let releaseInfo = '';
let i = 0;

for(let j = 0; j < lines.length; j++){
if(lines[j].includes(`[${version}]`)){
i = j;
j = lines.length;
}
}

lines = lines.slice(i);

for(let j = 0; j < lines.length; j++){
if(j == 0){
releaseInfo += `${lines[j]}`+ '\n';
continue;
}

if(!regex.test(lines[j])){
releaseInfo += `${lines[j]}`+ '\n';
} else {
j = lines.length;
}
}

if(releaseInfo === ''){
core.setFailed("No release info found, either missing in changelog or changelog is formatted incorrectly")
}

console.log("Gathered release info...")
return releaseInfo;
}

async function publishRelease(releaseInfo) {
const repoTagName = repoName.split('/');

await request('POST /repos/{owner}/{repo}/releases', {
headers: {
authorization: `token ${process.env.REPO_TOKEN}`,
},
owner: 'subql',
name: `[${version}] ${repoName}`,
repo: 'subql',
tag_name: `${repoTagName[1]}/${version}`,
body: releaseInfo
}).catch( err => {
core.setFailed(err)
})

console.log("Release Created...")
}

checkForBetaVersion(version);

const releaseInfo = gatherReleaseInfo(`${myArgs[0]}/CHANGELOG.md`);

publishRelease(releaseInfo);
12 changes: 12 additions & 0 deletions contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ In general, we follow the "fork-and-pull" Git workflow
* Push changes to your fork
* Open a PR in our repository

### Publishing to Github Releases

In order to publish to github release, there are prerequisites:

* Firstly in the merged commit the phrase `[release]` must be present
* Next, you must add a description of the release in the respective package's `CHANGELOG.md`
* There must be some difference in the package compared with main
* Lastly, the version must be in a format of x.y.z without '-'

When a pull request to main has been accepted the workflow will
try to create releases for each package where possible.

## Coding Conventions

### Git Commit Messages
Expand Down
Loading

0 comments on commit 232c084

Please sign in to comment.