me9867
me9867

Reputation: 1599

Auto tagging a commit in Github

When I make a chunk of changes I wish to tag as a version. I add git tag v1.4 etc

How can I automatically add this to the commit I do for these changes, at the moment I am tagging the commit # after I have done the commit and pushed it.

ie:

git tag -a v1.2 cd8a721 -m "Message here"

Upvotes: 1

Views: 5445

Answers (2)

Raman Nikhil
Raman Nikhil

Reputation: 31

I tried using Github actions where I've added the below yaml file in the root directory .github/workflows or you can try adding the workflows from Github Actions like setup new workflow add the yaml

name: Auto Tag

on:
    push:
    branches:
      - main

jobs:
  tag:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Create tag
        id: tag
        run: echo "TAG=v1.0.${{ github.run_number }}" >> $GITHUB_ENV

      - name: Push tag
        run: |
          git config --local user.email "[email protected]"
          git config --local user.name "GitHub Actions"
          git tag ${{ env.TAG }}
          git push origin ${{ env.TAG }}

And from the Repository setting enable the permissions for Read & write

You can find the settings here: Repository setting -> Actions -> General -> Workflow permissions

enter image description here

Upvotes: 2

CodeWizard
CodeWizard

Reputation: 142064

How can I automatically add this to the commit I do for these changes

You can do it withe GitHub web hooks.

Read about the events here: https://developer.github.com/webhooks/#events


Note

Its much better to use annotated tag git tag -a since it will create a commit like tag with the same information as commit.


Another way id to have local hook - when you commit set the tag and then push the branches and the tags.

post-commit hook (local hook)

#!/bin/sh

# get the last commit Id
lastCommit = $(git log -1 HEAD)

tag = <generate the tag message you want to set>

git tag -a ...

echo "                                         "
echo "                   |ZZzzz                "
echo "                   |                     "
echo "                   |                     "
echo "      |ZZzzz      /^\            |ZZzzz  "
echo "      |          |~~~|           |       "
echo "      |        |-     -|        / \      "
echo "     /^\       |[]+    |       |^^^|     "
echo "  |^^^^^^^|    |    +[]|       |   |     "
echo "  |    +[]|/\/\/\/\^/\/\/\/\/|^^^^^^^|   "
echo "  |+[]+   |~~~~~~~~~~~~~~~~~~|    +[]|   "
echo "  |       |  []   /^\   []   |+[]+   |   "
echo "  |   +[]+|  []  || ||  []   |   +[]+|   "
echo "  |[]+    |      || ||       |[]+    |   "
echo "  |_______|------------------|_______|   "
echo "                                         "
echo "                                         "
echo "      You have just committed and tagged " 
echo "      your code                          "

Upvotes: 3

Related Questions