Skip to content

Commit

Permalink
move npm publish to GitHub Action
Browse files Browse the repository at this point in the history
  • Loading branch information
campersau committed Jan 30, 2022
1 parent 531b4ee commit 596731f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 110 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,13 @@ jobs:
name: ungit-win32-x64
path: dist/ungit-win32-x64.zip
retention-days: 7

- name: npm publish
if: matrix.os == 'ubuntu-latest' && matrix.node-version == '12' && github.repository == 'FredrikNoren/ungit' && github.ref == 'refs/heads/master'
uses: actions/github-script@v5
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
with:
script: |
const script = require('./scripts/npmpublish.js')
await script({github, context, core})
76 changes: 0 additions & 76 deletions .travis.yml

This file was deleted.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"watch": "nodemon -C --exec \"npm run build\" -e js,less -w public/source -w public/less -w components/ -i \"*.bundle.js\"",
"bumpdependencies": "ncu --upgrade --reject bootstrap",
"electronpackage": "node ./scripts/electronpackage.js",
"electronzip": "node ./scripts/electronzip.js",
"travisnpmpublish": "node ./scripts/travisnpmpublish.js"
"electronzip": "node ./scripts/electronzip.js"
},
"repository": {
"type": "git",
Expand Down
48 changes: 48 additions & 0 deletions scripts/npmpublish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const child_process = require('child_process');
const fs = require('fs').promises;
const path = require('path');

module.exports = async ({ github, context, core }) => {
core.info('Preparing npm publish');
const hash = context.sha.substring(0, 8);
const packageJson = JSON.parse(await fs.readFile('package.json'));
const version = packageJson.version;
const tag = `v${version}`;
packageJson.version += `+${hash}`;
await fs.writeFile('package.json', `${JSON.stringify(packageJson, null, 2)}\n`);
await fs.writeFile('.npmrc', '//registry.npmjs.org/:_authToken=' + process.env.NPM_TOKEN);
core.info(`Publish ${packageJson.version} to npm`);
try {
await new Promise((resolve, reject) => {
child_process.exec('npm publish', async (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
core.info(`Creating release ${tag}`);
const release = await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
name: tag,
tag_name: tag,
body: `[Changelog](https://github.com/FredrikNoren/ungit/blob/master/CHANGELOG.md#${version.replace(/\./g, '')})`
});
const filePaths = await fs.readdir('dist');
for (let i = 0; i < filePaths.length; i++) {
const filePath = path.join('dist', filePaths[i]);
core.info(`Uploading release asset ${filePath}`);
await github.rest.repos.uploadReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release.data.id,
name: path.basename(filePath).replace('ungit', `ungit-${version}`),
data: await fs.readFile(filePath)
});
}
} catch (e) {
core.info(`npm publish failed: ${e}`);
}
}
32 changes: 0 additions & 32 deletions scripts/travisnpmpublish.js

This file was deleted.

0 comments on commit 596731f

Please sign in to comment.