From 935d0412a817ea6b51c2dd5122a59d76bb46f543 Mon Sep 17 00:00:00 2001 From: David Sancho Date: Sun, 27 Sep 2020 14:26:27 +0200 Subject: [PATCH] chore: Change CI --- .github/workflows/ci.yml | 190 +++++++++++++++++++++++---- .github/workflows/print-esy-cache.js | 13 ++ .github/workflows/release.js | 107 +++++++++++++++ .github/workflows/release.yml | 182 ------------------------- 4 files changed, 281 insertions(+), 211 deletions(-) create mode 100644 .github/workflows/print-esy-cache.js create mode 100644 .github/workflows/release.js delete mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1543a45..5cd3984 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,52 +4,184 @@ on: push: branches: - master + tags: + - v* pull_request: branches: - master jobs: - test: - name: Native + build-test: runs-on: ${{ matrix.os }} strategy: + fail-fast: false matrix: - os: [ubuntu-latest, macos-latest] - node-version: [12.x] + os: [ubuntu-latest, macos-latest, windows-latest] steps: - - uses: actions/checkout@v1 - - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 with: - node-version: ${{ matrix.node-version }} - - - name: Cache esy dependencies - uses: actions/cache@v1 - id: cache - with: - path: _export - key: ${{ runner.OS }}-build-${{ hashFiles('esy.lock/index.json') }} - restore-keys: | - ${{ runner.OS }}-build-${{ env.cache-name }}- - ${{ runner.OS }}-build- - ${{ runner.OS }}- - + node-version: 12 - name: Install esy - run: npm install -g esy@0.6.6 + run: npm install -g esy@0.6.7 - - name: Import dependencies - if: steps.cache.outputs.cache-hit == 'true' - run: esy import-build _export/* + - name: Try to restore install cache + uses: actions/cache@v2 + with: + path: ~/.esy/source + key: source-${{ matrix.os }}-${{ hashFiles('**/index.json') }} - name: Install dependencies run: esy install - - name: Build + - name: Print esy cache + id: print_esy_cache + run: node .github/workflows/print-esy-cache.js + + - name: Try to restore dependencies cache + id: deps-cache + uses: actions/cache@v2 + with: + path: ${{ steps.print_esy_cache.outputs.esy_cache }} + key: build-${{ matrix.os }}-${{ hashFiles('**/index.json') }} + restore-keys: build-${{ matrix.os }}- + + - name: Build dependencies + if: steps.deps-cache.outputs.cache-hit != 'true' + run: esy build-dependencies + + - name: Build project run: esy build - - name: Run tests + - name: Run Native tests run: esy test - - name: Export dependencies - run: esy export-dependencies \ No newline at end of file + - name: Create npm package + run: esy npm-release + - uses: actions/upload-artifact@v2 + with: + name: ${{ matrix.os }} + path: _release/ + + # Cleanup build cache if dependencies have changed + - name: Clean build cache + if: steps.deps-cache.outputs.cache-hit != 'true' + run: esy cleanup . + + prepare-publish: + name: Prepare publish to npm + needs: build-test + runs-on: ubuntu-latest + steps: + - uses: actions/setup-node@v1 + with: + node-version: 12 + - uses: actions/checkout@v2 + + # Download platform artifacts + - name: Download Linux release + uses: actions/download-artifact@v2 + with: + name: ubuntu-latest + path: ubuntu-latest + - name: Download Windows release + uses: actions/download-artifact@v2 + with: + name: windows-latest + path: windows-latest + - name: Download macOS release + uses: actions/download-artifact@v2 + with: + name: macos-latest + path: macos-latest + # Move artifacts in place + - name: Move artifacts + run: | + mkdir -p _release/platform-linux + mkdir -p _release/platform-windows-x64 + mkdir -p _release/platform-darwin + cp -a ubuntu-latest/. _release/platform-linux + cp -a windows-latest/. _release/platform-windows-x64 + cp -a macos-latest/. _release/platform-darwin + - name: Prepare package + run: node .github/workflows/release.js + # Create a npm package that can easily be published and tested + - name: npm pack + run: npm pack . + working-directory: _release + - name: move package + run: mv _release/*.tgz react-rules-of-hooks-ppx.tgz + # Upload artifacts + - uses: actions/upload-artifact@v2 + with: + name: release + path: _release/ + - uses: actions/upload-artifact@v2 + with: + name: release-tarball + path: react-rules-of-hooks-ppx.tgz + + publish: + needs: prepare-publish + name: (only on release) Publish + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Use Node.js 12.x + uses: actions/setup-node@v1 + with: + node-version: 12.x + + - name: Print short SHA + id: sha + run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)" + + - name: Download release + uses: actions/download-artifact@v2 + with: + name: release + path: release + + - name: Zip release folder + run: zip -r release.zip release + + - name: Create GitHub release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: Release ${{ github.ref }} + draft: false + prerelease: false + + - name: Upload ubuntu-latest to Github release + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: release.zip + asset_name: release.zip + asset_content_type: application/gzip + + - name: Release nightly NPM package + if: github.event_name == 'pull_request' + run: | + npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN + npm version prerelease --preid nightly.${{ steps.sha.outputs.sha_short }} -no-git-tag-version + npm publish --access public --tag nightly + working-directory: ./_release + env: + NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }} + + - name: Release NPM package + if: "startsWith(github.ref, 'refs/tags/v')" + run: | + npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN + npm publish --access public + working-directory: ./_release + env: + NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }} diff --git a/.github/workflows/print-esy-cache.js b/.github/workflows/print-esy-cache.js new file mode 100644 index 0000000..570c32e --- /dev/null +++ b/.github/workflows/print-esy-cache.js @@ -0,0 +1,13 @@ +const fs = require("fs"); +const os = require("os"); +const path = require("path"); + +const ESY_FOLDER = process.env.ESY__PREFIX + ? process.env.ESY__PREFIX + : path.join(os.homedir(), ".esy"); +const esy3 = fs + .readdirSync(ESY_FOLDER) + .filter((name) => name.length > 0 && name[0] === "3") + .sort() + .pop(); +console.log(`::set-output name=esy_cache::${path.join(ESY_FOLDER, esy3, "i")}`); diff --git a/.github/workflows/release.js b/.github/workflows/release.js new file mode 100644 index 0000000..1a06140 --- /dev/null +++ b/.github/workflows/release.js @@ -0,0 +1,107 @@ +const fs = require("fs"); +const path = require("path"); + +console.log("Creating package.json"); + +// From the project root pwd +const mainPackageJsonPath = fs.existsSync("esy.json") + ? "esy.json" + : "package.json"; + +const exists = fs.existsSync(mainPackageJsonPath); + +if (!exists) { + console.error("No package.json or esy.json at " + mainPackageJsonPath); + process.exit(1); +} + +// Now require from this script's location. +const mainPackageJson = require(path.join("..", "..", mainPackageJsonPath)); +const bins = Array.isArray(mainPackageJson.esy.release.bin) + ? mainPackageJson.esy.release.bin.reduce( + (acc, curr) => Object.assign({ [curr]: "bin/" + curr }, acc), + {} + ) + : Object.keys(mainPackageJson.esy.release.bin).reduce( + (acc, currKey) => + Object.assign( + { [currKey]: "bin/" + mainPackageJson.esy.release.bin[currKey] }, + acc + ), + {} + ); + +const rewritePrefix = + mainPackageJson.esy && + mainPackageJson.esy.release && + mainPackageJson.esy.release.rewritePrefix; + +const packageJson = JSON.stringify( + { + ...mainPackageJson, + scripts: { + postinstall: rewritePrefix + ? "ESY_RELEASE_REWRITE_PREFIX=true node ./postinstall.js" + : "node ./postinstall.js", + }, + bin: bins, + files: [ + "_export/", + "bin/", + "postinstall.js", + "esyInstallRelease.js", + "platform-linux/", + "platform-darwin/", + "platform-windows-x64/", + ], + }, + null, + 2 +); + +fs.writeFileSync( + path.join(__dirname, "..", "..", "_release", "package.json"), + packageJson, + { + encoding: "utf8", + } +); + +try { + console.log("Copying LICENSE"); + fs.copyFileSync( + path.join(__dirname, "..", "..", "LICENSE"), + path.join(__dirname, "..", "..", "_release", "LICENSE") + ); +} catch (e) { + console.warn("No LICENSE found"); +} + +console.log("Copying README.md"); +fs.copyFileSync( + path.join(__dirname, "..", "..", "README.md"), + path.join(__dirname, "..", "..", "_release", "README.md") +); + +console.log("Copying postinstall.js"); +fs.copyFileSync( + path.join(__dirname, "release-postinstall.js"), + path.join(__dirname, "..", "..", "_release", "postinstall.js") +); + +console.log("Creating placeholder files"); +const placeholderFile = `:; echo "You need to have postinstall enabled"; exit $? +@ECHO OFF +ECHO You need to have postinstall enabled`; +fs.mkdirSync(path.join(__dirname, "..", "..", "_release", "bin")); + +Object.keys(bins).forEach((name) => { + if (bins[name]) { + const binPath = path.join(__dirname, "..", "..", "_release", bins[name]); + fs.writeFileSync(binPath, placeholderFile); + fs.chmodSync(binPath, 0777); + } else { + console.log("bins[name] name=" + name + " was empty. Weird."); + console.log(bins); + } +}); diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index 194dbf8..0000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,182 +0,0 @@ -name: Release - -on: - push: - branches: - - master - pull_request: - branches: - - master - -jobs: - build: - name: Build and test - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: [ubuntu-latest, macos-latest, windows-latest] - steps: - - uses: actions/checkout@v1 - - - name: Use Node.js 12.x - uses: actions/setup-node@v1 - with: - node-version: 12.x - registry-url: "https://registry.npmjs.org" - - - name: Cache esy dependencies - uses: actions/cache@v1 - id: cache - with: - path: _export - key: ${{ runner.OS }}-build-${{ hashFiles('esy.lock/index.json') }} - restore-keys: | - ${{ runner.OS }}-build-${{ env.cache-name }}- - ${{ runner.OS }}-build- - ${{ runner.OS }}- - - - name: Install esy - run: npm install -g esy@0.6.6 - - # - name: Import dependencies - # if: steps.cache.outputs.cache-hit == 'true' - # run: esy import-build _export/* - - - name: Install dependencies - run: esy install - - - name: Build - run: esy build - - - name: Run tests - run: esy test - - - name: Export dependencies - run: esy export-dependencies - - - name: Upload artifacts for ${{ matrix.os }} - uses: actions/upload-artifact@master - with: - name: ${{ matrix.os }}-artifact - path: _build/default/bin/Bin.exe - - publish: - needs: build - name: Publish - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 - - - name: Use Node.js 12.x - uses: actions/setup-node@v1 - with: - node-version: 12.x - - - name: Fetch git tags - run: git fetch --tags --force - - - name: Make NPM release skeleton - run: node scripts/release-make-skeleton.js - - - name: Download linux artifacts - if: success() - uses: actions/download-artifact@master - with: - name: ubuntu-latest-artifact - path: _release/platform-linux-x64 - - - name: Download macOS artifacts - if: success() - uses: actions/download-artifact@master - with: - name: macos-latest-artifact - path: _release/platform-darwin-x64 - - # - name: Download windows artifacts - # if: success() - # uses: actions/download-artifact@master - # with: - # name: windows-latest-artifact - # path: _release/platform-windows-x64 - - - name: Create archives for Github release - run: | - cd _release/platform-darwin-x64 - zip -r ../../rules-of-hooks-ppx-darwin-x64.zip . - cd ../.. - - cd _release/platform-linux-x64 - zip -r ../../rules-of-hooks-ppx-linux-x64.zip . - cd ../.. - - # cd _release/platform-windows-x64 - # zip -r ../../rules-of-hooks-ppx-windows-x64.zip . - # cd ../.. - - - name: Create Github release - id: create_release - if: "startsWith(github.ref, 'refs/tags/v')" - uses: actions/create-release@v1.0.0 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ github.ref }} - release_name: Release ${{ github.ref }} - draft: false - prerelease: false - - - name: Upload rules-of-hooks-ppx-darwin-x64.zip to Github release - if: "startsWith(github.ref, 'refs/tags/v')" - uses: actions/upload-release-asset@v1.0.1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: rules-of-hooks-ppx-darwin-x64.zip - asset_name: rules-of-hooks-ppx-darwin-x64.zip - asset_content_type: application/gzip - - - name: Upload rules-of-hooks-ppx-linux-x64.zip to Github release - if: "startsWith(github.ref, 'refs/tags/v')" - uses: actions/upload-release-asset@v1.0.1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: rules-of-hooks-ppx-linux-x64.zip - asset_name: rules-of-hooks-ppx-linux-x64.zip - asset_content_type: application/gzip - - # - name: Upload rules-of-hooks-ppx-windows-x64.zip to Github release - # if: github.ref != 'refs/heads/master' - # uses: actions/upload-release-asset@v1.0.1 - # env: - # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - # with: - # upload_url: ${{ steps.create_release.outputs.upload_url }} - # asset_path: rules-of-hooks-ppx-windows-x64.zip - # asset_name: rules-of-hooks-ppx-windows-x64.zip - # asset_content_type: application/gzip - - - name: Print short SHA - id: sha - run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)" - - - name: Release nightly NPM package - if: github.event_name == 'pull_request' - run: | - npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN - npm version prerelease --preid nightly.${{ steps.sha.outputs.sha_short }} -no-git-tag-version - npm publish --access public --tag nightly - working-directory: ./_release - env: - NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }} - - - name: Release NPM package - if: "startsWith(github.ref, 'refs/tags/v')" - run: | - npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN - npm publish --access public - working-directory: ./_release - env: - NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}