In the following case I am creating a workflow so that when changes are uploaded to the main branch a tag is created taking into account the version of the package.json
In the root folder of the project you must create the .github/workflows
folder so that Github Actions can interpret our YAML file and execute the tasks that we tell it.
Inside this folder we are going to create a YAML file, in my case I will call it tag.yml
, you call it whatever you like, inside this file we are going to put the following code
# .github/workflows/tag.yml
name: Tag
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
Tag:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
with:
fetch-depth: '0'
- name: Configure git identity
run: |
git config --global user.email "[email protected]"
git config --global user.name "GitHub Actions"
- name: Get version from package.json
id: get_version
run: echo "::set-output name=version::$(node -p "require('./package.json').version")"
- name: Check if tag exists
run: |
if git rev-parse v${{ steps.get_version.outputs.version }} >/dev/null 2>&1; then
echo "Tag v${{ steps.get_version.outputs.version }} already exists"
fi
- name: Remove existing tag if exists
run: |
if git rev-parse v${{ steps.get_version.outputs.version }} >/dev/null 2>&1; then
git push --delete origin v${{ steps.get_version.outputs.version }}
git tag -d v${{ steps.get_version.outputs.version }}
fi
- name: Create tag
run: |
git tag -a v${{ steps.get_version.outputs.version }} -m "Version ${{ steps.get_version.outputs.version }}"
git push origin v${{ steps.get_version.outputs.version }}
-
Define el nombre del flujo de trabajo como "Tag".
-
Especifica los eventos que activarán este flujo de trabajo: push a la rama main y pull request hacia la rama main.
-
Define un trabajo llamado "Tag" que se ejecutará en un entorno Ubuntu.
-
Define los permisos necesarios para el trabajo, en este caso, se establece el permiso de escritura para el contenido.
-
Configura el entorno de trabajo mediante la acción actions/checkout@v3, que clona el repositorio en la máquina virtual del flujo de trabajo.
-
Configura la identidad de Git para el flujo de trabajo.
-
Obtiene la versión del archivo package.json utilizando Node.js y establece el valor como una salida llamada version.
-
Comprueba si ya existe una etiqueta para la versión obtenida de package.json. Si existe, muestra un mensaje indicándolo.
-
Si existe una etiqueta anterior para la versión, la elimina tanto localmente como en el repositorio remoto.
-
Crea una nueva etiqueta con el nombre de la versión obtenida de package.json y la empuja al repositorio remoto.
Once created, to test it we just have to upload the changes to our Github repository
git add .
git commit -m "feat: :tada: First commit"
git push origin main
In the Github repository we must click on the icon that appears next to the commit we made or click on the Github Actions tab.
Being inside we can view the workflows that were executed, if we click on one of these we can view the jobs that were executed.
We can visualize and track what is executed in the job we select.
In the following image we can basically see all the commands or instructions that we created in the tag.yml we can also notice if there was an error and try to fix it.
If we go back to the repository and review the tags we can see that if they are created correctly.
We can tell which tag was uploaded by looking at the package.json of our project.