HomeContact

Automatically Generate and Push Git Tags with GitHub Actions

By Shady Nagy
Published in Github
April 29, 2023
2 min read
Automatically Generate and Push Git Tags with GitHub Actions

Table Of Contents

01
Introduction
02
Creating the GitHub Actions Workflow File
03
Example GitHub Actions Workflow
04
Further Reading
05
Troubleshooting
06
Conclusion
07
Feedback and Questions

Introduction

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:

  1. Generates a new Git tag based on the existing tags in the repository.
  2. Pushes the new tag to the remote repository.
  3. Builds and deploys the application.

Creating the GitHub Actions Workflow File

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.

Example GitHub Actions Workflow

name: Deploy Application
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
if: "contains(github.event.head_commit.message, 'deploy application')"
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
fetch-depth: '0'

This step checks out your repository so that the workflow can access its files.

- name: Generate Git Tag
id: generate_tag
run: |
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" ]; then
VERSION_PATCH=0
else
VERSION_PATCH=$((VERSION_PATCH + 1))
fi
NEW_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 Tag
run: |
git config user.name "GitHub Actions"
git config user.email "github-actions@users.noreply.github.com"
git tag $NEW_TAG
git 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 Application
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
if: "contains(github.event.head_commit.message, 'deploy application')"
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
fetch-depth: '0'
- name: Generate Git Tag
id: generate_tag
run: |
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" ]; then
VERSION_PATCH=0
else
VERSION_PATCH=$((VERSION_PATCH + 1))
fi
NEW_TAG="${VERSION_PREFIX}${VERSION_MAJOR_MINOR}.${VERSION_PATCH}"
echo "Generated new tag: $NEW_TAG"
echo "NEW_TAG=$NEW_TAG" >> $GITHUB_ENV
- name: Push Git Tag
run: |
git config user.name "GitHub Actions"
git config user.email "github-actions@users.noreply.github.com"
git tag $NEW_TAG
git push origin $NEW_TAG
# Add your build, test, and deploy steps here

Further Reading

  1. GitHub Actions Documentation - This is the official documentation for GitHub Actions, which provides detailed information about setting up workflows, managing actions, and more. It’s an invaluable resource for understanding the features and capabilities of GitHub Actions.
  2. Git Tagging - This is the official documentation for GitHub Actions, which provides detailed information about setting up workflows, managing actions, and more. It’s an invaluable resource for understanding the features and capabilities of GitHub Actions.

Troubleshooting

  1. Ensure that your Git tags follow a consistent naming convention.
  2. Check for typos or syntax errors in your GitHub Actions workflow file.
  3. Verify that you have the necessary permissions to push Git tags to the remote repository.

Conclusion

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.

Feedback and Questions

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:

  1. Email: shady@shadynagy.com
  2. Twitter: @ShadyNagy_
  3. LinkedIn: Shady Nagy

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!


Tags

#GitHub#GitHubActions#GitTags#Versioning#CI/CD

Share


Previous Article
Mastering Threading in C# Concepts and Examples
Shady Nagy

Shady Nagy

Software Innovation Architect

Topics

AI
Angular
dotnet
GatsbyJS
Github
Linux
MS SQL
Oracle

Related Posts

How to Create a Directory in GitHub A Step-by-Step Guide
How to Create a Directory in GitHub A Step-by-Step Guide
November 16, 2024
2 min

Quick Links

Contact Us

Social Media