
In this blog post, we will walk through the process of automatically generating and pushing Git tags using GitHub Actions. This can be useful when you want to create a new release based on a specific commit or when you want to automate the versioning of your application.
We will demonstrate this by creating a GitHub Actions workflow that:
Before we dive into the example workflow, let’s first understand where to create the YAML file for the GitHub Actions workflow.
In your GitHub repository, create a new directory named .github
at the root level. Inside the .github
directory, create another directory named workflows
. The YAML file containing your GitHub Actions workflow should be placed inside the workflows
directory. For example, you can name your file deploy_application.yml
.
Your repository structure should look like this:
your_repository/.github/workflows/deploy_application.yml...
Now, let’s proceed with the example GitHub Actions workflow.
name: Deploy Applicationon:push:branches:- mainjobs:build:runs-on: ubuntu-latestif: "contains(github.event.head_commit.message, 'deploy application')"steps:- name: Checkout repositoryuses: actions/checkout@v2with:fetch-depth: '0'
This step checks out your repository so that the workflow can access its files.
- name: Generate Git Tagid: generate_tagrun: |VERSION_PREFIX="v"VERSION_MAJOR_MINOR="1.0"VERSION_PATCH=$(git tag --list "${VERSION_PREFIX}${VERSION_MAJOR_MINOR}.*" --sort=-version:refname | head -n 1 | grep -oE '[0-9]+$')if [ -z "$VERSION_PATCH" ]; thenVERSION_PATCH=0elseVERSION_PATCH=$((VERSION_PATCH + 1))fiNEW_TAG="${VERSION_PREFIX}${VERSION_MAJOR_MINOR}.${VERSION_PATCH}"echo "Generated new tag: $NEW_TAG"echo "NEW_TAG=$NEW_TAG" >> $GITHUB_ENV
This step generates a new Git tag based on the existing tags in the repository.
- name: Push Git Tagrun: |git config user.name "GitHub Actions"git config user.email "github-actions@users.noreply.github.com"git tag $NEW_TAGgit push origin $NEW_TAG
This step pushes the new Git tag to the remote repository.
# Add your build, test, and deploy steps here
Add your application-specific build, test, and deploy steps in this section. So the full yml will be
name: Deploy Applicationon:push:branches:- mainjobs:build:runs-on: ubuntu-latestif: "contains(github.event.head_commit.message, 'deploy application')"steps:- name: Checkout repositoryuses: actions/checkout@v2with:fetch-depth: '0'- name: Generate Git Tagid: generate_tagrun: |VERSION_PREFIX="v"VERSION_MAJOR_MINOR="1.0"VERSION_PATCH=$(git tag --list "${VERSION_PREFIX}${VERSION_MAJOR_MINOR}.*" --sort=-version:refname | head -n 1 | grep -oE '[0-9]+$')if [ -z "$VERSION_PATCH" ]; thenVERSION_PATCH=0elseVERSION_PATCH=$((VERSION_PATCH + 1))fiNEW_TAG="${VERSION_PREFIX}${VERSION_MAJOR_MINOR}.${VERSION_PATCH}"echo "Generated new tag: $NEW_TAG"echo "NEW_TAG=$NEW_TAG" >> $GITHUB_ENV- name: Push Git Tagrun: |git config user.name "GitHub Actions"git config user.email "github-actions@users.noreply.github.com"git tag $NEW_TAGgit push origin $NEW_TAG# Add your build, test, and deploy steps here
In this blog post, we demonstrated how to automatically generate and push Git tags using GitHub Actions. By incorporating this technique into your workflow, you can simplify the process of versioning and releasing your applications. Feel free to customize the provided example to suit your specific project needs.
We’d love to hear your feedback on this tutorial! If you have any questions or suggestions for improvement, please don’t hesitate to reach out. You can leave a comment below, or you can contact us through the following channels:
We’ll do our best to address any questions or concerns you may have. We look forward to hearing from you and helping you make the most of GitHub Actions for automating the generation and pushing of Git tags!
Quick Links
Legal Stuff