Question3r
Question3r

Reputation: 3812

Is it possible to automate .Net assembly versioning using semantic-release?

I have a C# solution containing a single project and multiple libraries using .Net 6. I'm using conventional commits (commitlint with husky) and want to use semantic-release to deploy the latest build as a ZIP file on Github based on the commit messages.


The setup I tried for C# projects:

.

npm install semantic-release -D
npm install @semantic-release/changelog -D
npm install @semantic-release/npm -D
npm install @semantic-release/github -D
npm install @semantic-release/git -D

.

{
  "plugins": [
    "@semantic-release/commit-analyzer",
    "@semantic-release/release-notes-generator",
    "@semantic-release/changelog",
    "@semantic-release/npm",
    [
      "@semantic-release/github",
      {
        "assets": [
          {
            "path": "my-project.zip",
            "label": "my-project-${nextRelease.gitTag}.zip",
            "name": "my-project-${nextRelease.gitTag}.zip"
          }
        ]
      }
    ],
    "@semantic-release/git"
  ]
}

.

name: Release on push on main branch
 
on:
  push:
    branches:
      - main
 
jobs:
  release-on-push-on-main-branch:
    runs-on: ubuntu-latest
 
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

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

      - name: Install Node dependencies
        run: npm install
 
      - name: Setup .Net
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '6.0.x'
 
      - name: Install .Net dependencies
        run: dotnet restore ./SolutionDir
 
      - name: Run build
        run: dotnet build ./SolutionDir
 
      - name: Run publish
        run: dotnet publish ./SolutionDir
 
      - name: Rename publish directory of MyProject to my-project and move it to root
        run: mv ./SolutionDir/MyProject/bin/Debug/net6.0 ./my-project
 
      - name: ZIP my-project directory
        run: zip -r my-project.zip my-project
 
      - name: Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: npx semantic-release --branches main

It seems to work, whenever I push to the main branch it will deploy the distribution (containing the DLL) with the latest version to the Github releases.


But as you know semantic-release does not release the correct package version because it doesn't know about the assembly version yet. It takes the version from the package.json file.

What I want to achieve:

Is there something I can use?

Upvotes: 2

Views: 2751

Answers (1)

latonz
latonz

Reputation: 1750

You could dry-run semantic-release before dotnet publish to fetch the version number of the release (using the @semantic-release/exec plugin). Then pass this fetched version number to dotnet publish:

Add the @semantic-release/exec plugin to your npm install commands and adjust your .releaserc.json file to store the next release version (for example in an environment variable):

{
   "plugins": [
     ...,
     [
       '@semantic-release/exec',
       { verifyReleaseCmd: 'echo RELEASE_VERSION=\${nextRelease.version} >> $GITHUB_ENV' }
     ]
   ]
 }

Insert the dry run before the dotnet publish and adjust the dotnet publish step:

...
- name: Fetch release version
  env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  run: npx semantic-release --branches main --dry-run
- name: Run publish
  run: dotnet publish ./SolutionDir -p:Version=${{ env.RELEASE_VERSION }}
...

Note: instead of your Rename publish directory of MyProject to my-project and move it to root step, you could simply use the -o param of the dotnet publish command.

Upvotes: 6

Related Questions