Skip to content

Instantly share code, notes, and snippets.

@randymxd06
Last active August 1, 2024 16:35
Show Gist options
  • Save randymxd06/1c3c7fd7fdddb5777ccc5916728fb8f6 to your computer and use it in GitHub Desktop.
Save randymxd06/1c3c7fd7fdddb5777ccc5916728fb8f6 to your computer and use it in GitHub Desktop.
Creating a tag using package.json with Github Actions

Creating a tag using package.json with Github Actions

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

Implementation

1. Create the workflow

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 }}
  1. Define el nombre del flujo de trabajo como "Tag".

  2. Especifica los eventos que activarán este flujo de trabajo: push a la rama main y pull request hacia la rama main.

  3. Define un trabajo llamado "Tag" que se ejecutará en un entorno Ubuntu.

  4. Define los permisos necesarios para el trabajo, en este caso, se establece el permiso de escritura para el contenido.

  5. 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.

  6. Configura la identidad de Git para el flujo de trabajo.

  7. Obtiene la versión del archivo package.json utilizando Node.js y establece el valor como una salida llamada version.

  8. Comprueba si ya existe una etiqueta para la versión obtenida de package.json. Si existe, muestra un mensaje indicándolo.

  9. Si existe una etiqueta anterior para la versión, la elimina tanto localmente como en el repositorio remoto.

  10. Crea una nueva etiqueta con el nombre de la versión obtenida de package.json y la empuja al repositorio remoto.

2. Upload changes to our project

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

3. Verify that the tag has been created

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.

example1

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.

example2

We can visualize and track what is executed in the job we select.

example3

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.

example4

If we go back to the repository and review the tags we can see that if they are created correctly.

example5

We can tell which tag was uploaded by looking at the package.json of our project.

example6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment