How to setup semantic-release without the need for npm?
Asked Answered
D

5

11

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?

Dominican answered 5/6, 2021 at 15:12 Comment(1)
If you are looking for an alternative, you can achieve similar functionality with Reliza Hub - worklifenotes.com/2020/02/27/… (I'm working on this project)Elate
C
6

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

{
  "plugins": [
    ["@semantic-release/npm", {
      "npmPublish": false,
    }],
  ]
}
Cowcatcher answered 16/6, 2021 at 12:35 Comment(1)
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 checksThury
H
1

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.

Hora answered 6/5, 2023 at 6:20 Comment(0)
K
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"
Kab answered 31/8, 2021 at 13:13 Comment(0)
G
0

You can simply use the [following library]https://github.com/semantic-release/github instead of the @semantic-release/apm-config one.

npm i @semantic-release/github

I would still add this in your package.json file

"private": true
Guinness answered 12/5 at 22:7 Comment(0)
C
-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.

Crosseyed answered 6/11, 2021 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.