7

I have a C# project and would like to add semantic versioning to it. So whenever I push to the main branch I want to create a new release and autogenerate a new version number based on the commit types. I think semantic-release does the job very well since I'm already using commitlint with husky.

For reproduction:

.

name: Release

on:
  push:
    branches:
      - `main`

jobs:
  release:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Setup Node
        uses: actions/setup-node@v2
        with:
          node-version: 14.x

      - name: Install dependencies
        run: npm install

      - name: Release
        env:
          GITHUB_TOKEN: ${{ secrets.SEMANTIC_RELEASE }}
        run: npx semantic-release
  • After pushing it the workflow should fail with the following error message

[2:51:48 PM] [semantic-release] › ✔ Completed step "fail" of plugin "@semantic-release/github" An npm token (https://github.com/semantic-release/npm/blob/master/README.md#npm-registry-authentication) must be created and set in the NPM_TOKEN environment variable on your CI environment.

Please make sure to create an npm token (https://docs.npmjs.com/getting-started/working_with_tokens#how-to-create-new-tokens) and to set it in the NPM_TOKEN environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org/.

AggregateError: SemanticReleaseError: No npm token specified. at module.exports (/home/runner/.npm/_npx/1561/lib/node_modules/semantic-release/node_modules/@semantic-release/npm/lib/get-error.js:6:10) at module.exports (/home/runner/.npm/_npx/1561/lib/node_modules/semantic-release/node_modules/@semantic-release/npm/lib/set-npmrc-auth.js:45:31) at module.exports (/home/runner/.npm/_npx/1561/lib/node_modules/semantic-release/node_modules/@semantic-release/npm/lib/verify-auth.js:17:9) at verifyConditions (/home/runner/.npm/_npx/1561/lib/node_modules/semantic-release/node_modules/@semantic-release/npm/index.js:36:13) at async validator (/home/runner/.npm/_npx/1561/lib/node_modules/semantic-release/lib/plugins/normalize.js:34:24) at async /home/runner/.npm/_npx/1561/lib/node_modules/semantic-release/lib/plugins/pipeline.js:37:34 at async Promise.all (index 0) at async next (/home/runner/.npm/_npx/1561/lib/node_modules/semantic-release/node_modules/p-reduce/index.js:16:18) at /home/runner/.npm/_npx/1561/lib/node_modules/semantic-release/lib/plugins/pipeline.js:54:11 at async Object.pluginsConf. [as verifyConditions] (/home/runner/.npm/_npx/1561/lib/node_modules/semantic-release/lib/plugins/index.js:80:11) at async run (/home/runner/.npm/_npx/1561/lib/node_modules/semantic-release/index.js:95:3) at async module.exports (/home/runner/.npm/_npx/1561/lib/node_modules/semantic-release/index.js:260:22) at async module.exports (/home/runner/.npm/_npx/1561/lib/node_modules/semantic-release/cli.js:55:5) Error: Process completed with exit code 1.

I don't want to publish to the npm registry, it should just create a new release version.

Did I miss something or is semantic-release the wrong tool for my project?

Question3r
  • 2,166
  • 19
  • 100
  • 200
  • If you are looking for an alternative, you can achieve similar functionality with Reliza Hub - https://worklifenotes.com/2020/02/27/automatic-version-increments-with-reliza-hub-2-strategies/ (I'm working on this project) – taleodor Jun 25 '21 at 02:48

4 Answers4

4

You don't have to publish to the npm registry. You can set that up in the .releaserc.

{
  "plugins": [
    ["@semantic-release/npm", {
      "npmPublish": false,
    }],
  ]
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Marko Francekovic
  • 1,450
  • 10
  • 10
  • This is correct but even when using this plugin config the package still expects an NPM_TOKEN to be set on the CI runner, its fails like in the OG post snippet: `No npm token specified.` I tried spoofing the token but this also fails authentication checks – Kolvin Jul 19 '22 at 20:57
0

I realize this is an old question, yet maybe someone will find my solution usefull. I adapted it from this blog post by Sohrab Hosseini

\\ .gitlab-ci.yml
cut-version:
  image: node:16-alpine3.13
  stage: publish
    - npm i -g semantic-release @semantic-release/gitlab
  artifacts:
    reports:
      dotenv: vars.env
  script:
    - |-
      cat > .releaserc <<RELEASERC
      {
        "plugins": [
          "@semantic-release/commit-analyzer",
          "@semantic-release/release-notes-generator",
          "@semantic-release/gitlab",
          [
            "@semantic-release/exec", {
              "successCmd": "echo \"VERSION=\${nextRelease.version}\" >> vars.env"
            }
          ]
        ]
      }
      RELEASERC
    - semantic-release
  only:
    - main

publish-build:
  stage: publish
  needs:
    - job: cut-version
  script:
    - echo "$VERSION"
phn
  • 85
  • 6
0

Check this doc: https://semantic-release.gitbook.io/semantic-release/support/faq#why-is-the-package.jsons-version-not-updated-in-my-repository

you need to set up package.json "private": true, then the semantic-release will not publish the npm package and only update the version and git push. Meanwhile, the NPM_TOKEN is not necessary. (or you can setup the NPM_TOKEN to any string in CI system if get some error.)

if you do not want to update package.json, you can remove the npm plugin.

Xin Meng
  • 1,982
  • 3
  • 20
  • 32
-1

Instead of using hacks to achieve your desired usage, I can recommend using an alternative to semantic-release called atomic-release. It's an SDK to create atomic releases which ensure a failure during a release rolls back any previous actions taken.

You can create your own release strategy and utilize gitSemanticRelease

Disclaimer: I'm the author of atomic-release.

elad.chen
  • 2,375
  • 5
  • 25
  • 37