Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 29, 2024 11:35 am GMT

How to setup semantic release with GitHub Actions.

Recently, My coworker is using Nx to automate his workflow, including automated release using semantic version. I found this method quite useful, so I want to re-implement on single GitHub repositories (without Nx).

First Step: Create GitHub's Personal Access Token

You can follow this link on how to create Personal Access Token.
You can choose Fine-grained tokens as you can scope specific repositories. (Or Token (Classic))

menu

Make sure that you grant repository permission to token. For my configuration I use:

Read Access

  • variables
  • environments
  • metadata (default)
  • secrets

Read/Write Access

  • code
  • commit statuses
  • issue
  • pull requests
  • workflows

I might grant unnecessary permission, so feel free to correct me.

Second Step: Setup GitHub token in repository's environment

1.Go to Settings options
setting

2.Select Environments

select environment

3.Create Environment

create environment

4.Create Environment Secret

environment secret

I use GH_TOKEN to represent GITHUB_TOKEN (GitHub not allows naming variables with prefix "GITHUB").

Third Step: Setup workflows

I use this package -> Action for Semantic Release to setup my workflow.
Use this script to create workflow

name: teston:  push:    branches:      # Change this if your primary branch is not main      - master      - devjobs:  main:    runs-on: ubuntu-latest    environment:      name: Semver #your environment name    steps:      - name: Checkout        uses: actions/checkout@v4        with:          persist-credentials: false      - name: Semantic Release        uses: cycjimmy/semantic-release-action@v4        env:          GH_TOKEN: ${{ secrets.GH_TOKEN }}

In this code snippets, it's meant to

  • setup environment
  • perform semantic release

Fourth Step: Create .releaserc

This will be semantic-release configuration file for

Create .releaserc in your project.

{    "branches": [        "master",        "dev"    ],    "plugins": [        "@semantic-release/commit-analyzer",        "@semantic-release/release-notes-generator",        [            "@semantic-release/changelog",            {                "changelogFile": "CHANGELOG.md"            }        ],        [            "@semantic-release/git",            {                "assets": [                    "CHANGELOG.md"                ]            }        ],        "@semantic-release/github"    ]}

Final Step: Add, Commit and Push

This step will be the byproduct of your work so far.

Once you commit your code with message convention, for example, feat: add some features, it will trigger the semantic-release. Default convention message with angular preset included:

  • fix -> bug fixes: patch release
  • feat -> feature: minor release
  • breaking change -> break change: major release

Once you push your code it will run GitHub Action and release your project.

github action

Then it will create Release and CHANGELOG.md in the project.

release

changelog

Conclusion

semantic-release can help you automate your versioning and release and really useful tracking and document change.

Feel free to comment or give feedbacks. I'm appreciate your time for reading .

Reference

https://www.conventionalcommits.org/en/v1.0.0/
https://github.com/marketplace/actions/action-for-semantic-release

Or my sample repository -> https://github.com/Sahanon-P/semver-sample


Original Link: https://dev.to/sahanonp/how-to-setup-semantic-release-with-github-actions-31f3

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To