From fa406fdc21e7691c6c7bb4bd5be85f06d677ed2a Mon Sep 17 00:00:00 2001 From: BowTiedDevOps <157840260+BowTiedDevOps@users.noreply.github.com> Date: Thu, 4 Apr 2024 15:58:38 +0300 Subject: [PATCH 1/3] feat: workflow that publishes to npm if package version changed --- .github/workflows/ci.yml | 81 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..5590f14 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,81 @@ +name: Publish Packages + +on: + push: + branches: + - main + - ci/check-version-and-publish # TODO: remove this + +jobs: + check_version_change: + name: Check Version Changes + runs-on: ubuntu-latest + outputs: + CHANGED: ${{steps.check_version.outputs.changed}} + PACKAGE_PATH: ${{steps.set_path.outputs.changed_package_path}} + steps: + - name: Checkout Code + id: checkout_code + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + with: + fetch-depth: 0 + + - name: Set Path + id: set_path + run: | + # Get the paths of changed files + changes_paths=$(git diff-tree --no-commit-id --name-only -r "${{ github.sha }}" | awk -F'/' '{print $1"/"$2}') + + # Manifest names list + manifest_names=("package.json") + + # Save the manifest path of the changed package in GitHub env and exit without error + for path in ${changes_paths}; do + if [[ $path == packages/* ]]; then + for manifest in "${manifest_names[@]}"; do + if [[ -f $path/$manifest ]]; then + echo "changed_package_path=${path}" >> "$GITHUB_OUTPUT" + echo "changed_manifest_path=${path}/${manifest}" >> "$GITHUB_OUTPUT" + echo "package_changed=true" >> "$GITHUB_OUTPUT" + exit 0 + fi + done + fi + done + + # If we haven't exited yet, no package was changed + echo "No package was changed in this commit!" + echo "package_changed=false" >> "$GITHUB_OUTPUT" + + - name: Check Version + id: check_version + if: ${{ steps.set_path.outputs.package_changed == 'true' }} + uses: EndBug/version-check@53c9309fefac91a21f852d5ca69f33878280d2f5 # v2.1.3 + with: + file-name: ${{ steps.set_path.outputs.changed_manifest_path }} + + publish_to_npm: + name: Publish Changed Package + runs-on: ubuntu-latest + needs: check_version_change + if: ${{ needs.check_version_change.outputs.CHANGED == 'true' }} + steps: + - name: Checkout Code + id: checkout_code + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 #v4.1.1 + + - name: Setup Node + id: setup_node + uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 #v4.0.2 + with: + node-version: '20' + registry-url: 'https://registry.npmjs.org/' + + - name: Publish Package + id: publish_package + env: + NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }} + run: | + cd "$(echo '${{ needs.check_version_change.outputs.PACKAGE_PATH }}')" + npm ci + npm publish --access=public From 8c35c5d37cad2fed5166da5b08d37fd181475990 Mon Sep 17 00:00:00 2001 From: BowTiedDevOps <157840260+BowTiedDevOps@users.noreply.github.com> Date: Thu, 4 Apr 2024 15:59:59 +0300 Subject: [PATCH 2/3] feat: remove run on a branch on push --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5590f14..3048f93 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,6 @@ on: push: branches: - main - - ci/check-version-and-publish # TODO: remove this jobs: check_version_change: From dae18fcc1742255fd80948c2ebac35b512b939b9 Mon Sep 17 00:00:00 2001 From: BowTiedDevOps <157840260+BowTiedDevOps@users.noreply.github.com> Date: Fri, 5 Apr 2024 16:51:19 +0300 Subject: [PATCH 3/3] feat: run `npm test` before publish, use action to publish --- .github/workflows/ci.yml | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3048f93..fd82770 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest outputs: CHANGED: ${{steps.check_version.outputs.changed}} - PACKAGE_PATH: ${{steps.set_path.outputs.changed_package_path}} + PACKAGE_PATH: ${{steps.set_path.outputs.changed_path}} steps: - name: Checkout Code id: checkout_code @@ -33,7 +33,7 @@ jobs: if [[ $path == packages/* ]]; then for manifest in "${manifest_names[@]}"; do if [[ -f $path/$manifest ]]; then - echo "changed_package_path=${path}" >> "$GITHUB_OUTPUT" + echo "changed_path=${path}" >> "$GITHUB_OUTPUT" echo "changed_manifest_path=${path}/${manifest}" >> "$GITHUB_OUTPUT" echo "package_changed=true" >> "$GITHUB_OUTPUT" exit 0 @@ -69,12 +69,18 @@ jobs: with: node-version: '20' registry-url: 'https://registry.npmjs.org/' - - - name: Publish Package - id: publish_package - env: - NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }} + + - name: Test Package + id: test_package run: | - cd "$(echo '${{ needs.check_version_change.outputs.PACKAGE_PATH }}')" + cd ${{ needs.check_version_change.outputs.PACKAGE_PATH }} npm ci - npm publish --access=public + npm test + + - name: Publish Package + id: publish_package + uses: JS-DevTools/npm-publish@19c28f1ef146469e409470805ea4279d47c3d35c # v3.1.1 + with: + token: ${{ secrets.NODE_AUTH_TOKEN }} + package: ${{ needs.check_version_change.outputs.PACKAGE_PATH }} + access: public