mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-10-29 16:46:01 +08:00
Merge pull request #966 from rluvaton/migrate-travis-to-github-actions
This commit is contained in:
49
.github/workflows/lint-and-generate-html-from-markdown.yml
vendored
Normal file
49
.github/workflows/lint-and-generate-html-from-markdown.yml
vendored
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
name: Lint & Generate HTML from Markdown
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
shell: bash
|
||||||
|
working-directory: .operations
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
name: Lint
|
||||||
|
runs-on: ubuntu-20.04
|
||||||
|
env:
|
||||||
|
NODE_ENV: test
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v1
|
||||||
|
|
||||||
|
- name: Setup Node.js environment
|
||||||
|
uses: actions/setup-node@v2.1.2
|
||||||
|
with:
|
||||||
|
node-version: '14'
|
||||||
|
|
||||||
|
- run: npm install
|
||||||
|
- run: npm run lint
|
||||||
|
|
||||||
|
build:
|
||||||
|
name: Build
|
||||||
|
runs-on: ubuntu-20.04
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v1
|
||||||
|
|
||||||
|
- name: Setup Node.js environment
|
||||||
|
uses: actions/setup-node@v2.1.2
|
||||||
|
with:
|
||||||
|
node-version: '14'
|
||||||
|
|
||||||
|
- run: npm install
|
||||||
|
- run: npm run build
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
IS_FORK: ${{ github.repository != 'goldbergyoni/nodebestpractices' }}
|
||||||
@ -7,6 +7,7 @@ const { readdir, readFile, writeFile } = require('graceful-fs');
|
|||||||
const imagemin = require('imagemin');
|
const imagemin = require('imagemin');
|
||||||
const imageminJpegtran = require('imagemin-jpegtran');
|
const imageminJpegtran = require('imagemin-jpegtran');
|
||||||
const imageminPngquant = require('imagemin-pngquant');
|
const imageminPngquant = require('imagemin-pngquant');
|
||||||
|
const CIInfo = require('ci-info');
|
||||||
|
|
||||||
const converter = new showdown.Converter();
|
const converter = new showdown.Converter();
|
||||||
|
|
||||||
@ -21,8 +22,8 @@ const imageminOpts = {
|
|||||||
|
|
||||||
console.info(`Working in [${process.cwd()}]`);
|
console.info(`Working in [${process.cwd()}]`);
|
||||||
|
|
||||||
const { GITHUB_TOKEN, TRAVIS_BRANCH, TRAVIS, TRAVIS_REPO_SLUG } = process.env;
|
const { isCI } = CIInfo;
|
||||||
const isCI = !!TRAVIS;
|
const { GITHUB_TOKEN, OWNER_AND_REPO, BRANCH, IS_PR, IS_FORK } = getConfigFromEnv();
|
||||||
|
|
||||||
readDirPromise('./')
|
readDirPromise('./')
|
||||||
.then(async (fileNames) => {
|
.then(async (fileNames) => {
|
||||||
@ -35,15 +36,15 @@ readDirPromise('./')
|
|||||||
const templateHTML = await readFilePromise(templateFilePath);
|
const templateHTML = await readFilePromise(templateFilePath);
|
||||||
const processedTemplateHTML = await inlineResources(templateHTML);
|
const processedTemplateHTML = await inlineResources(templateHTML);
|
||||||
const outputHTML = await processMDFile(fileName, processedTemplateHTML);
|
const outputHTML = await processMDFile(fileName, processedTemplateHTML);
|
||||||
console.info(`Completed Generation in [${(Date.now() - startTime) / 1000}s]`);
|
console.info(`Completed Generation in [${computeElapsedTime(startTime)}s]`);
|
||||||
|
|
||||||
const outFileName = path.parse(fileName).name + '.html';
|
const outFileName = path.parse(fileName).name + '.html';
|
||||||
const outFilePath = path.join('.operations', 'out', outFileName);
|
const outFilePath = path.join('.operations', 'out', outFileName);
|
||||||
console.info(`Writing output to [${outFilePath}]`);
|
console.info(`Writing output to [${outFilePath}]`);
|
||||||
await writeFilePromise(outFilePath, outputHTML);
|
await writeFilePromise(outFilePath, outputHTML);
|
||||||
|
|
||||||
if (isCI && TRAVIS_BRANCH === 'master') {
|
if(shouldUpdateGitHubPages()) {
|
||||||
const repo = new Repository(TRAVIS_REPO_SLUG, {
|
const repo = new Repository(OWNER_AND_REPO, {
|
||||||
token: GITHUB_TOKEN
|
token: GITHUB_TOKEN
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -51,7 +52,7 @@ readDirPromise('./')
|
|||||||
await repo.writeFile('gh-pages', outFileName, outputHTML, ':loudspeaker: :robot: Automatically updating built HTML file', {});
|
await repo.writeFile('gh-pages', outFileName, outputHTML, ':loudspeaker: :robot: Automatically updating built HTML file', {});
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`Failed to generate from [${fileName}] in [${(Date.now() - startTime) / 1000}s]`, err);
|
console.error(`Failed to generate from [${fileName}] in [${computeElapsedTime(startTime)}s]`, err);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -60,6 +61,40 @@ readDirPromise('./')
|
|||||||
console.log(`🎉 Finished gen-html 🎉`);
|
console.log(`🎉 Finished gen-html 🎉`);
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function getConfigFromEnv() {
|
||||||
|
if (CIInfo.GITHUB_ACTIONS) {
|
||||||
|
return getConfigFromGithubActionEnv()
|
||||||
|
}
|
||||||
|
return process.env;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getConfigFromGithubActionEnv() {
|
||||||
|
const config = {
|
||||||
|
...process.env,
|
||||||
|
|
||||||
|
OWNER_AND_REPO: process.env.GITHUB_REPOSITORY,
|
||||||
|
|
||||||
|
IS_PR: CIInfo.IS_PR !== null ? CIInfo.IS_PR : process.env.GITHUB_EVENT_NAME === 'pull_request',
|
||||||
|
|
||||||
|
// We assume we're in PR and and we get the source for the PR
|
||||||
|
BRANCH: process.env.GITHUB_HEAD_REF,
|
||||||
|
};
|
||||||
|
|
||||||
|
if(!config.IS_PR) {
|
||||||
|
// GITHUB_REF example: `refs/heads/main`
|
||||||
|
config.BRANCH = process.env.GITHUB_REF.substring('refs/heads/'.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
function shouldUpdateGitHubPages() {
|
||||||
|
return isCI && !IS_FORK && !IS_PR && BRANCH === 'master';
|
||||||
|
}
|
||||||
|
|
||||||
|
function computeElapsedTime(startTime) {
|
||||||
|
return (Date.now() - startTime) / 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async function processMDFile(filePath = '/', templateHTML = null) {
|
async function processMDFile(filePath = '/', templateHTML = null) {
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "cd .. && node .operations/gen-html.js",
|
"build": "cd .. && node .operations/gen-html.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"lint": "./node_modules/.bin/markdownlint ../README.md"
|
"lint": "markdownlint ../README.md"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -20,11 +20,13 @@
|
|||||||
"homepage": "https://github.com/goldbergyoni/nodebestpractices#readme",
|
"homepage": "https://github.com/goldbergyoni/nodebestpractices#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cheerio": "^1.0.0-rc.2",
|
"cheerio": "^1.0.0-rc.2",
|
||||||
|
"ci-info": "^3.1.1",
|
||||||
"github-api": "^3.0.0",
|
"github-api": "^3.0.0",
|
||||||
"graceful-fs": "^4.1.15",
|
"graceful-fs": "^4.1.15",
|
||||||
"imagemin": "^6.0.0",
|
"imagemin": "^6.0.0",
|
||||||
"imagemin-jpegtran": "^6.0.0",
|
"imagemin-jpegtran": "^6.0.0",
|
||||||
"imagemin-pngquant": "^6.0.0",
|
"imagemin-pngquant": "^6.0.0",
|
||||||
|
"is-ci": "^3.0.0",
|
||||||
"markdownlint-cli": "^0.18.0",
|
"markdownlint-cli": "^0.18.0",
|
||||||
"showdown": "^1.9.0"
|
"showdown": "^1.9.0"
|
||||||
}
|
}
|
||||||
|
|||||||
10
.travis.yml
10
.travis.yml
@ -1,10 +0,0 @@
|
|||||||
language: node_js
|
|
||||||
node_js: lts/*
|
|
||||||
script:
|
|
||||||
- cd .operations
|
|
||||||
- npm i
|
|
||||||
- export PR=https://api.github.com/repos/$TRAVIS_REPO_SLUG/pulls/$TRAVIS_PULL_REQUEST
|
|
||||||
- export BRANCH=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then echo $TRAVIS_BRANCH; else echo `curl -s $PR | jq -r .head.ref`; fi)
|
|
||||||
- echo "TRAVIS_BRANCH=$TRAVIS_BRANCH, PR=$PR, BRANCH=$BRANCH"
|
|
||||||
- npm run lint
|
|
||||||
- npm run build
|
|
||||||
Reference in New Issue
Block a user