visit
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.
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
.
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.
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.
npm i --save-dev standard-version
yarn add --dev standard-version
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
sudo chmod +x release.sh
./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
.
{
"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.
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.
In summary, by using a that integrates with standard-version
you 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.