jscutlery / semver

Nx plugin to automate semantic versioning and CHANGELOG generation.
MIT License
719 stars 83 forks source link

Question: How to proceed if NPM publish fails #734

Open MarkComerford opened 10 months ago

MarkComerford commented 10 months ago

We had an issue where it failed to publish to NPM due to a 404 issue. I believe this is an issue to do with NPM, however the issue I now have is that commits and tags have been created in my Github project. This means I can't just run the workflow again, as it will bump the package versions again, which isn't what we want. I can delete the tags, and revert the commits but that's not ideal.

I've tried to look at the various arguments available but there doesn't seem to be anything to perform publish only. Is there any recommended path to take if this happens?

We're running the following command as part of a Github Actions workflow:

npx nx affected --target=version --base=last-release --configuration=release --parallel=1

This is our Github actions workflow for reference:

name: Release
on:
- workflow_dispatch
jobs:
  release:
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
        token: ${{ secrets.BOT_TOKEN }}

    - name: Setup git user
      shell: bash
      run: |
        npm config set "//registry.npmjs.org/:_authToken=$NPM_TOKEN"
        git config --global user.name 'abc'
        git config --global user.email 'abc@example.com'
      env:
        NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

    - uses: volta-cli/action@v4
      with:
        node-version: "18"

    - name: Node Modules Cache
      uses: actions/cache@v3
      with:
        path: ~/.npm
        key: ${{ runner.os }}-node-18-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-node-18-
    - name: Install
      shell: bash
      run: npm ci

    - name: Version
      shell: bash
      run: |
        export CI=true
        npx nx affected --target=version --base=last-release --configuration=release --parallel=1
      env:
        NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}
        GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
        NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

    - name: Tag last-release
      shell: bash
      run: git tag -f last-release

    - name: Push changes
      uses: ad-m/github-push-action@552c074ed701137ebd2bf098e70c394ca293e87f
      with:
        github_token: ${{ secrets.BOT_TOKEN }}
        branch: ${{ github.ref }}
        force: true
        tags: true
edbzn commented 7 months ago

Hi, to avoid pushing tags and version commits if NPM publish fails you could do this:

So it could look like:

"targets": {
  "version": {
    "executor": "@jscutlery/semver:version",
    "options": {
      "postTargets": ["npm-publish", "git-push", "github-release"]
    }
  },
  "npm-publish": {
    "executor": "ngx-deploy-npm:deploy",
    "options": {
      "access": "public"
    }
  },
  "git-push": {
    "command": "git push --atomic --follow-tags"
  },
  "github-release": {
    "executor": "@jscutlery/semver:github",
    "options": {
      "tag": "{tag}",
      "notes": "{notes}"
    }
  }
}
carstenrntng commented 5 months ago

Just fyi: If you try something like above, be aware that concurrent commits to your main branch will break the above workflow. This may due to concurrent runs of your release pipeline, or something pushing directly to your main branch. You may end up with a package release to npm without release commit & tag being pushed to your main branch.

I personally find it much easier to have tested & tagged commits in git and have an additional manually-run workflow that, given a tag name, (re)publishes this tag as a package to npm in case a previous publish failed.