Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to proceed if NPM publish fails #734

Open
MarkComerford opened this issue Sep 12, 2023 · 2 comments
Open

Question: How to proceed if NPM publish fails #734

MarkComerford opened this issue Sep 12, 2023 · 2 comments
Labels
question Further information is requested

Comments

@MarkComerford
Copy link

MarkComerford commented Sep 12, 2023

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 edbzn added the question Further information is requested label Dec 14, 2023
@edbzn
Copy link
Member

edbzn commented Dec 14, 2023

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

  • set --push to false as the push occurs before post-targets
  • write a custom post-target "git-push" that pushes the new version
  • declare the "git-push" target next to the NPM publish so it's not executed if the publish fails

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
Copy link

carstenrntng commented Jan 31, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants