Create a git tag with GitHub Actions
Last updated on October 13, 2024GitHub Actions allow us to automate, customize and execute software development workflows right into our repository. There are a lot of examples, custom workflows and documentation.
In this tutorial we are going to create a manual GitHub Action to create a new git tag with a version and a description. For this task, we will use the action negz/create-tag@v1.
Create a new manual workflow
We will use the assistant from GitHub in the Actions tab.
Pressing the New workflow button will take us to a list of workflows template. We will use the Manual workflow template
.
This action will open the workflow assistant based on the manual workflow template.
We can use this window to edit our custom workflow or our favourite editor. This assistant will help you to add new actions from the Marketplace.
The create tag action workflow
This will be the content of our github action to create new tags.
name: Create tag
on:
workflow_dispatch:
inputs:
version:
description: 'The tag version to be created based on the master branch'
default: '1.0.0'
required: true
description:
description: 'The tag description. For example: First release!'
default: 'First release!'
required: true
jobs:
create-tag:
runs-on: ubuntu-latest
steps:
- name: Create tag
uses: negz/create-tag@v1
with:
# Github Token
token: 'MY_GITHUB_TOKEN'
# Version (v1.0.0)
version: 'v${{ github.event.inputs.version }}'
# Tag message
message: '${{ github.event.inputs.description }}'
Our action will do the following tasks:
- It will ask us for 2 required parameters: a version and a description.
- It will run the
create-tag
job from the jobs definition. - This job will use a virtual machine based on the latest version of Ubuntu to run a list of steps.
- We will have an unique step called
Create tag
which uses thenegz/create-tag@v1
action. - This action needs 3 parameters:
- A GitHub Token used to create and push a new tag in our repository.
- The version of our tag filled with the input variable
version
. - The message of our tag filled with the input variable
message
.
Create a GitHub token
We can create new GitHub tokens from this page.
Pressing the button Generate new token
will open the assistant for generating new tokens.
We have to give it a name, set the expiration policy and we will check the repo scope
. Then, it will generate the token and you must save it somewhere because you will not be able to see it again.
NOTE: Be careful where you publish this token because anyone can access your repositories and take control of them.
If your repository is private, you can put it as plain text in the token
parameter of the Create tag
step.
Create an action secret
The best option is to use a function called Actions secrets
, which allows us to define variables and store their content securely.
Using the New repository secret
button, we will define a new variable called GIT_TOKEN
with our real GitHub token as the value.
After adding the action secret, we can access the value from the GitHub action using the variable ${{ secrets.GIT_TOKEN }}
.
name: Create tag
on:
workflow_dispatch:
inputs:
version:
description: 'The tag version to be created based on the master branch'
default: '1.0.0'
required: true
description:
description: 'The tag description. For example: First release!'
default: 'First release!'
required: true
jobs:
create-tag:
runs-on: ubuntu-latest
steps:
- name: Create tag
uses: negz/create-tag@v1
with:
# Github Token
token: '${{ secrets.GIT_TOKEN }}'
# Version (v1.0.0)
version: 'v${{ github.event.inputs.version }}'
# Tag message
message: '${{ github.event.inputs.description }}'
Execute the GitHub Action
When the action file is pushed to our repository, we will see the action in the Actions tab
.
This action will create a new tag with the version 3.2.0
and the description Deals feature release
for a private project called rekomind.
References
- GitHub Actions: https://docs.github.com/es/actions
- Create tag action: https://github.com/negz/create-tag