miércoles, abril 07, 2021

Invocando un pipeline de gitlab a través de la API

Este es un ejemplo muy sencillo de como ejecutar un pipeline a través del uso de su API sobrescribiendo un valor en de una variable previamente definida

En esta sección: CI / CD -> Settings -> Variables he definido una variable simple llamada GREETING con el valor Hola

mi archivo .gitlab-ci.yaml es el siguiente:

image: "tonymoyoy/ubuntu_ansible:v1"


stages:
    - build
    - test

build:
    stage: build
    script:
        - echo $GREETING
        - echo "Construyendo"
        - mkdir build
        - touch build/info2.txt
    artifacts:
        paths:
            - build/

test:
    stage: test
    script:
        - echo "Probando"
        - test -f "build/info2.txt"

En la sección: CI / CD -> Settings -> Pipeline triggers agregue un trigger llamado pew, el cual genera un Token

Con esto podemos usar curl para iniciar el pipeline, en este caso sobre el branch master y proporcionando el valor Hello a la variable GREETING


curl -X POST \
    -F token=<YOUR-TRIGGER-TOKEN> \
    -F "ref=master" \
    -F "variables[GREETING]=Hello" \
http://<YOURGITLAB>/api/v4/projects/4/trigger/pipeline


En el log del pipeline podremos ver que tomo el valor de la variable:



No hay comentarios: