diff --git a/.github/workflows/lint-and-generate-html-from-markdown.yml b/.github/workflows/lint-and-generate-html-from-markdown.yml new file mode 100644 index 00000000..e50557e2 --- /dev/null +++ b/.github/workflows/lint-and-generate-html-from-markdown.yml @@ -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' }} diff --git a/.operations/gen-html.js b/.operations/gen-html.js index 6a4abd10..fa6808bc 100644 --- a/.operations/gen-html.js +++ b/.operations/gen-html.js @@ -7,6 +7,7 @@ const { readdir, readFile, writeFile } = require('graceful-fs'); const imagemin = require('imagemin'); const imageminJpegtran = require('imagemin-jpegtran'); const imageminPngquant = require('imagemin-pngquant'); +const CIInfo = require('ci-info'); const converter = new showdown.Converter(); @@ -21,8 +22,8 @@ const imageminOpts = { console.info(`Working in [${process.cwd()}]`); -const { GITHUB_TOKEN, TRAVIS_BRANCH, TRAVIS, TRAVIS_REPO_SLUG } = process.env; -const isCI = !!TRAVIS; +const { isCI } = CIInfo; +const { GITHUB_TOKEN, OWNER_AND_REPO, BRANCH, IS_PR, IS_FORK } = getConfigFromEnv(); readDirPromise('./') .then(async (fileNames) => { @@ -35,15 +36,15 @@ readDirPromise('./') const templateHTML = await readFilePromise(templateFilePath); const processedTemplateHTML = await inlineResources(templateHTML); 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 outFilePath = path.join('.operations', 'out', outFileName); console.info(`Writing output to [${outFilePath}]`); await writeFilePromise(outFilePath, outputHTML); - if (isCI && TRAVIS_BRANCH === 'master') { - const repo = new Repository(TRAVIS_REPO_SLUG, { + if(shouldUpdateGitHubPages()) { + const repo = new Repository(OWNER_AND_REPO, { token: GITHUB_TOKEN }); @@ -51,7 +52,7 @@ readDirPromise('./') await repo.writeFile('gh-pages', outFileName, outputHTML, ':loudspeaker: :robot: Automatically updating built HTML file', {}); } } 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); } } @@ -60,6 +61,40 @@ readDirPromise('./') 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) { diff --git a/.operations/package.json b/.operations/package.json index f4629e88..3db88347 100644 --- a/.operations/package.json +++ b/.operations/package.json @@ -6,7 +6,7 @@ "scripts": { "build": "cd .. && node .operations/gen-html.js", "test": "echo \"Error: no test specified\" && exit 1", - "lint": "./node_modules/.bin/markdownlint ../README.md" + "lint": "markdownlint ../README.md" }, "repository": { "type": "git", @@ -20,11 +20,13 @@ "homepage": "https://github.com/goldbergyoni/nodebestpractices#readme", "dependencies": { "cheerio": "^1.0.0-rc.2", + "ci-info": "^3.1.1", "github-api": "^3.0.0", "graceful-fs": "^4.1.15", "imagemin": "^6.0.0", "imagemin-jpegtran": "^6.0.0", "imagemin-pngquant": "^6.0.0", + "is-ci": "^3.0.0", "markdownlint-cli": "^0.18.0", "showdown": "^1.9.0" } diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 50b0bb43..00000000 --- a/.travis.yml +++ /dev/null @@ -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 diff --git a/README.md b/README.md index 520652ba..0618ea6a 100644 --- a/README.md +++ b/README.md @@ -272,7 +272,7 @@ function someFunction() { } // Avoid -function someFunction() +function someFunction() { // code block }