paint-brush
Automate Your Software Releases with Standard-Version and a Ready-to-Use Bash Script by@sagekage
270 reads

Automate Your Software Releases with Standard-Version and a Ready-to-Use Bash Script

by David E.July 31st, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Use the conventional-commits specification, standard-version, and a Bash script to automate your software releases. This guide offers a step-by-step process, a ready-to-use script, and tips for version control and changelog management.
featured image - Automate Your Software Releases with Standard-Version and a Ready-to-Use Bash Script
David E. HackerNoon profile picture
Releasing new versions of your software can be a tedious and error-prone process, especially if you're doing it manually. When the project grows up, it can be a real pain.


However, with the help of standard-version and a Bash script, you can automate your release flow and save time and effort, making this process much simpler.


What is Standard-Version?

standard-version is a tool that helps you automate the management of version numbers and changelogs in your software projects. It uses the conventional-commits specification to determine the type of changes made in each commit (e.g. "feat", "fix", "docs", etc.), and based on that, it updates the version number and generates a changelog. By automating these tasks, standard-version can help you maintain a consistent versioning scheme and keep track of changes more easily. It also simplifies the process of creating and publishing releases, as it takes care of all the necessary changes and updates.


Now, before we get started, I want to make it clear that this is just one solution, and there are plenty of other ways to integrate standard-version or other tools (like semantic-release), into your workflow. But hey, if you're anything like me and you like to keep things simple, then this might just be the solution you've been looking for.


So, assuming that you're already usingconventional-commits and have a previous release tag, let's dive into how to automate your release flow with a Bash script and standard-version.


Prerequisites

  1. Ensure that you're using conventional-commits: standard-version relies on the conventional-commits specification to determine the type of changes made in each commit (e.g. "feat", "fix", "docs", etc.). If you're not already using conventional-commits you'll need to start using it in your project.


  2. Ensure that you have a previous release tag: standard-version requires a previous release tag to determine the version number of the next release. If you don't have a previous release tag, you'll need to create one manually.


  3. Have a basic understanding of Bash scripting: You'll need to have a basic understanding of Bash scripting to modify the Bash script that we'll provide in this guide.


Step-by-Step Guide

  1. Install standard-version:
npm i --save-dev standard-version


or
yarn add --dev standard-version


  1. Create a Bash script called release.sh in the root directory of your project. Paste the following code into the script:
#!/bin/bash

target_branch=$1
current_branch=$(git rev-parse --abbrev-ref HEAD)
dev_branch=development

if [ -z $target_branch ]; then
  echo "A target_branch parameter is required: the branch where the release will be performed (version, CHANGELOG, & git TAG)"
  exit 1
else 
  echo "The release is configured to execute on the branch: ${target_branch}"
fi

if [[ -z $(git log origin/$(git branch --show-current)..HEAD) ]]; then
  echo "No commits to push to origin/${current_branch}, aborting release."
  exit 0
else
  echo "Commits to push to origin/${current_branch} detected."
fi

if [ $current_branch = $target_branch ]; then 
  echo "The release will be executed on the branch: ${current_branch}"
  # Run standard-version with a custom commit message to avoid the default [skip ci] message that skips the GitLab workflow
  npx standard-version --commit-all --releaseCommitMessageFormat 'chore(release): {{currentTag}}\n\n{{changelog}}'

  echo "Pushing changes to origin ${current_branch}"
  git push --follow-tags origin $current_branch

  # Switch to the development branch
  git checkout $dev_branch

  # Get the hash of the latest commit on the release branch
  last_commit=$(git rev-parse $target_branch)

  # Apply the latest commit from the release branch to the development branch, updating the version number and changelog
  git cherry-pick -Xtheirs $last_commit

  exit 0;
else 
  echo "Skipping release on branch: ${current_branch}";
  echo "Pushing changes to origin/${current_branch}"
  git push
  exit 0; 
fi


Save the script and make it executable using the following command:
sudo chmod +x release.sh


Now, you can execute the script by providing the target branch as a parameter:
./release.sh production


The target branch is the branch where the release process will be executed. In this example, we're interested to perform releases only in the production branch. So, if this command is executed on any other branch, the script will skip the release and acts as a conventional push.


To make this even easier, you can add the following script to your package.json file:
{
  "scripts": {
    "release": "./release.sh production"
  }
}

With this, you already have the release script configured to execute on the production branch. You should replace the target branch with the name of your own release branch. Then can execute the script using the following command:

npm run release
or
yarn run release



Note that the Bash script performs a cherry-pick to move the commit that updates the version number and changelog from the release branch to the development branch. This ensures that the next development cycle always contains the latest changes, including the version number and changelog. You can also change the name of the development branch in the script if you want.


Conclusion

Automating your release flow using standard-version and a Bash script can save you a lot of time and effort. By following the steps outlined in this guide, you'll be able to automate your release process and make it more efficient and reliable.


It's important to keep in mind the prerequisites. Remember to commit well, to always test your releases thoroughly before publishing them, and to follow best practices for versioning and changelog management.


In summary, by using a that integrates with standard-versionyou can automate your release process and make it more efficient. This guide has provided a step-by-step approach to help you get started with automating your release flow. However, it's worth exploring other solutions and finding what works best for your specific needs.

The lead image for this article was generated by HackerNoon's AI Image Generator via the prompt "laptop screen displaying arbitrary code"


바카라사이트 바카라사이트 온라인바카라