Home > Interesting > Set up semantic-release to work with bitbucket git

Set up semantic-release to work with bitbucket git

Problem:
Setting up semantic-release to work with bitbucket.

Solution:
Followed the steps in https://medium.com/coding-spaghetti/npm-version-control-using-semantic-release-and-bitbucket-cloud-5294ac6b324b to set up semantic-release.

Install the relevant packages

$ npm install --save-dev semantic-release
$ npm install --save-dev @semantic-release/git @semantic-release/npm
$ less package.json
...
"devDependencies": {
    "@babel/plugin-proposal-private-property-in-object": "^7.21.0",
    "@commitlint/cli": "^17.8.0",
    "@commitlint/config-conventional": "^17.8.0",
    "@semantic-release/git": "^10.0.1",
    "@semantic-release/npm": "^11.0.0",
    "commitlint": "^17.8.0",
    "husky": "^8.0.0",
    "semantic-release": "^22.0.5"
  },
  "release": {
    "repositoryUrl": "https://auth@bitbucket.org/<BITBUCKET_REPO_OWNER>/<BITBUCKET_REPO_SLUG>.git",
    "branches": [
      "main"
    ],
    "plugins": [
      "@semantic-release/commit-analyzer",
      "@semantic-release/npm",
      [
        "@semantic-release/git",
        {
          "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
        }
      ]
    ]
  }
...

note: you need to change and to match your project repo. (If you try and clone your repository, the last two sections of your path make up the repo owner and the repo slug.)

Ex: git clone https://username@bitbucket.org/blahblah/sampleproject.git

the BITBUCKET_REPO_OWNER is blahblah and BITBUCKET_REPO_SLUG is sampleproject

Note:
Bitbucket Personal Access Tokens. Documentation below is for Atlassian’s self-hosted source control tool Bitbucket Server instead of Bitbucket Cloud.
https://confluence.atlassian.com/bitbucketserver/personal-access-tokens-939515499.html
See https://stackoverflow.com/a/70415364

For Bitbucket Cloud I created an “App Password”
Settings > Personal Bitbucket settings > Access Management > App passwords > Create app password
Under Permissions, Repositories select Write permission

Save the created token. Replace with created token.

export BITBUCKET_TOKEN_BASIC_AUTH=<bitbucket-username>:<token>

Test the settings

$ npx semantic-release --no-ci --dry-run

To start at version v0.1.0
git tag the initial commit. git log to get the commit checksum.

$ git tag -a v0.1.0 -m "v0.1.0" <commit checksum (or part of it)>

Added a post-merge git hook

$ less .git/hooks/post-merge

#!/bin/bash

CURRENT_DIR=$(pwd)
PROJECT_NAME="proj-name"
PROJECT_DIR="lib/proj"

if [[ "${HOME}/${PROJECT_NAME}/${PROJECT_DIR}" == ${CURRENT_DIR}  ]]
then 
    npx semantic-release --no-ci
elif [[ "${HOME}/${PROJECT_NAME}" == ${CURRENT_DIR} ]]
then
    cd ${PROJECT_DIR}
    npx semantic-release --no-ci
    cd -
fi

Source:
Configuration settings for semantic-release
* https://semantic-release.gitbook.io/semantic-release/usage/ci-configuration
* https://medium.com/coding-spaghetti/npm-version-control-using-semantic-release-and-bitbucket-cloud-5294ac6b324b
* https://github.com/semantic-release/git/issues/323

  1. No comments yet.
  1. No trackbacks yet.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.