From 6cd97e639364bbf5ad9b84fed1409257274bab34 Mon Sep 17 00:00:00 2001 From: Ferris <3579192+code-ape@users.noreply.github.com> Date: Sun, 8 May 2022 16:59:46 -0400 Subject: [PATCH] Adding merge CI/CD (#3) 1. Changed package name to `@urbdyn/rusty-result`. 2. Added CI/CD to publish package to Github NPM registry for every build via `.github/workflows/publish.yaml`. 3. Added CI/CD to publish package to NPM whenever there is a version change via `.github/workflows/publish.yaml`. 4. Updated Github Action dependency versions in `.github/workflows/pr.yaml`. --- .github/workflows/pr.yaml | 4 +- .github/workflows/publish.yaml | 138 +++++++++++++++++++++++++++++++++ package-lock.json | 4 +- package.json | 11 +-- 4 files changed, 148 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/publish.yaml diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index 31e933c..ebeeff0 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -19,7 +19,7 @@ jobs: node_version: ['12.22.12', '14.19.2', '16.15.0'] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node_version }} uses: actions/setup-node@v3 with: @@ -42,7 +42,7 @@ jobs: deno_version: ['v1.17.0', 'v1.21.1', 'vx.x.x'] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Use Deno ${{ matrix.deno_version }} uses: denoland/setup-deno@v1 with: diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml new file mode 100644 index 0000000..51ae60d --- /dev/null +++ b/.github/workflows/publish.yaml @@ -0,0 +1,138 @@ +name: Package Release CI +on: + push: + branches: [main] + # Ignore all markdown files, all files in the .github directory (but not its + # workflows/ directory), and test files. + paths-ignore: ['**.md', '.github/*'] + + +jobs: + + # job: create_version + create_version: + name: Create build version + runs-on: ubuntu-latest + timeout-minutes: 2 + outputs: + build_version: ${{ steps.gen_version.outputs.new_version }} + prod_build: ${{ steps.gen_version.outputs.prod_build }} + npm_tag: ${{ steps.gen_version.outputs.npm_tag }} + steps: + - uses: actions/checkout@v3 + with: { fetch-depth: '2' } + - uses: actions/setup-node@v3 + with: { node-version-file: '.nvmrc' } + - uses: salsify/action-detect-and-tag-new-version@v2 + id: detect_version + with: { create-tag: false } + + - name: Version pre-release + id: gen_version + run: | + set -ex + previous_version="${{steps.detect_version.outputs.previous-version}}" + current_version="${{steps.detect_version.outputs.current-version}}" + if [ "$previous_version" = "$current_version" ]; then + echo "Version has NOT changed. This is a dev build." + export CURRENT_COMMIT="$(git rev-parse --short HEAD)" + export NEW_PACKAGE_VERSION="$(npx semver --increment minor "$current_version")-dev.$CURRENT_COMMIT" + echo "::set-output name=new_version::$NEW_PACKAGE_VERSION" + echo "::set-output name=prod_build::false" + echo "::set-output name=npm_tag::dev" + else + echo "Version has changed. This is a prod build!" + echo "::set-output name=new_version::$current_version" + echo "::set-output name=prod_build::true" + echo "::set-output name=npm_tag::latest" + fi + + + # job: publish_github_package + publish_github_package: + name: Publish NPM package to Github + runs-on: ubuntu-latest + timeout-minutes: 5 + needs: ['create_version'] + + steps: + - uses: actions/checkout@v3 + - name: Use Node.js from .nvmrc + uses: actions/setup-node@v3 + with: + node-version-file: '.nvmrc' + cache: 'npm' + registry-url: 'https://npm.pkg.github.com/' + scope: '@urbdyn' + - run: npm ci + env: + NODE_AUTH_TOKEN: ${{secrets.GH_RD_PKG_TOKEN}} + - name: Set Version + run: | + git config --global user.email "actions@github.com" + git config --global user.name "Github Actions" + npm version --allow-same-version ${{needs.create_version.outputs.build_version}} + - run: npm publish --tag ${{needs.create_version.outputs.npm_tag}} + env: + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + + # job: publish_npm_package + publish_npm_package: + name: Publish repo release and NPM package + runs-on: ubuntu-latest + timeout-minutes: 5 + needs: ['create_version'] + if: needs.create_version.outputs.prod_build == 'true' + + steps: + - uses: actions/checkout@v3 + - name: Use Node.js from .nvmrc + uses: actions/setup-node@v3 + with: + node-version-file: '.nvmrc' + cache: 'npm' + registry-url: 'https://registry.npmjs.org' + + - name: Create Release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ needs.create_version.outputs.build_version }} + release_name: Release ${{ needs.create_version.outputs.build_version }} + body: > + This release contains the repo source code and a tar.gz of the NPM package. + It has been created for proper archiving practices. + However, please use the NPM registry to install and use the NPM package! + draft: false + prerelease: ${{ needs.create_version.outputs.prod_build != 'true' }} + + - run: npm ci + - name: Set Version + run: | + git config --global user.email "actions@github.com" + git config --global user.name "Github Actions" + npm version --allow-same-version ${{needs.create_version.outputs.build_version}} + - name: npm pack + id: npm_pack + run: | + export NPM_TGZ_FILE=$(npm pack) + echo "::set-output name=npm_tgz_file::$NPM_TGZ_FILE" + + # Upload NPM tar.gz, linux only + - name: Upload Release Asset - tgz + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./${{ steps.npm_pack.outputs.npm_tgz_file }} + asset_name: ${{ steps.npm_pack.outputs.npm_tgz_file }} + asset_content_type: application/gzip + + - run: npm publish + if: needs.create_version.outputs.npm_tag == 'latest' + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_CI_TOKEN }} diff --git a/package-lock.json b/package-lock.json index 5cb1f0c..ebdeddc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "@urbdyn/result", + "name": "@urbdyn/rusty-result", "version": "0.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "@urbdyn/result", + "name": "@urbdyn/rusty-result", "version": "0.1.0", "license": "MIT", "devDependencies": { diff --git a/package.json b/package.json index 52094f8..fb43277 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,15 @@ { - "name": "@urbdyn/result", + "name": "@urbdyn/rusty-result", "version": "0.0.1", "description": "Typescript implementation of Rust's Result", "author": "Urban Dynamics (https://www.urbdyn.com/)", "license": "MIT", "repository": { "type": "git", - "url": "https://github.com/urbdyn/result.js.git" + "url": "https://github.com/urbdyn/rusty-result.js.git" }, - "homepage": "https://github.com/urbdyn/result.js#readme", - "url": "https://github.com/urbdyn/result.js/issues", + "homepage": "https://github.com/urbdyn/rusty-result.js#readme", + "url": "https://github.com/urbdyn/rusty-result.js/issues", "main": "dist/result.js", "files": [ "dist/", @@ -18,7 +18,8 @@ "scripts": { "build": "tsc", "test": "deno test", - "fmt": "deno fmt" + "fmt": "deno fmt", + "prepack": "npm run build" }, "devDependencies": { "typescript": "^4.6.4"