Outils pour utilisateurs

Outils du site


tech:notes_gitlab-ci_pipeline_-_api

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
tech:notes_gitlab-ci_pipeline_-_api [2026/06/17 17:28] Jean-Baptistetech:notes_gitlab-ci_pipeline_-_api [2026/07/16 13:25] (Version actuelle) Jean-Baptiste
Ligne 1: Ligne 1:
 <!DOCTYPE markdown> <!DOCTYPE markdown>
-{{tag>Brouillon Gitlab CI API}}+{{tag>Brouillon Gitlab CI API Ansible}}
  
 # Notes Gitlab-ci pipeline - API # Notes Gitlab-ci pipeline - API
  
 +Voir :
 +  * https://docs.gitlab.com/api/pipelines/
 +  * https://docs.gitlab.com/security/tokens/#security-considerations
  
-## Exemple 1 - avec Access Token 
  
-  * Avec Access Token+## Exemple 1 - avec Access Token ou Personnal Access tokens 
 + 
 +Avec Access Token (associé au projet)
   * Aller dans le projet - puis : Settings / Access tokens   * Aller dans le projet - puis : Settings / Access tokens
   * Attention : par défaut les Access Tokens ont une date d'expiration   * Attention : par défaut les Access Tokens ont une date d'expiration
-  * Voir Personnal Access tokens+ 
 +Avec Personnal Access tokens (associé au compte utilisateur) 
 +  * Profile / Edit profile / Access Token / Personal Access Tokens 
 +  * Attention : par défaut les Personal Access Tokens ont une date d'expiration
  
 Launch pipeline with API call - with Private / Personal access tokens Launch pipeline with API call - with Private / Personal access tokens
Ligne 17: Ligne 24:
   --header "PRIVATE-TOKEN: saltxx-8888888888" \   --header "PRIVATE-TOKEN: saltxx-8888888888" \
   --header "Content-Type: application/json" \   --header "Content-Type: application/json" \
-  --data '{ "ref": "testjb", "variables": [ {"key": "VAR1", "value": "plop"}, {"key": "VAR2", "value": "plop"}, {"key": "VAR3", "value": "plop"}, {"key": "VAR4", "value": ""} ] }' \ +  --data '{ "ref": "master", "variables": [ {"key": "VAR1", "value": "plop"}, {"key": "VAR2", "value": "plop"}, {"key": "VAR3", "value": "plop"}, {"key": "VAR4", "value": ""} ] }' \ 
-  --url "http://gitlab.acme.local/api/v4/projects/100/pipeline"+  --url "${CI_API_V4_URL}/projects/100/pipeline"
 ~~~ ~~~
  
 Voir https://docs.gitlab.com/api/rest/ Voir https://docs.gitlab.com/api/rest/
 +
 +
 +### Notes
 +
 +CI_API_V4_URL est une variable magique dont la valeur est de la forme : `http://gitlab.acme.local/api/v4`
 +
 +Dans notre cas la variable `ref` correspond au nom de la branche
    
  
 ## Exemple 2 - Avec Trigger Tokens ## Exemple 2 - Avec Trigger Tokens
 +
 +Voir :
 +  * https://medium.com/@alla.shamis/automating-gitlab-ci-cd-using-api-triggers-and-passing-variables-like-a-pro-e864310765d8
  
 Voir aussi : Voir aussi :
   * https://docs.gitlab.com/ci/pipelines/downstream_pipelines/#multi-project-pipelines   * https://docs.gitlab.com/ci/pipelines/downstream_pipelines/#multi-project-pipelines
 +  * https://blog.stephane-robert.info/docs/pipeline-cicd/gitlab/industrialisation/multi-projet/
  
 Aller dans le projet, puis Settings/ CI/CD / Pipeline trigger tokens  Aller dans le projet, puis Settings/ CI/CD / Pipeline trigger tokens 
Ligne 35: Ligne 53:
 curl --request POST \ curl --request POST \
      --form token=glptt-99999999999999 \      --form token=glptt-99999999999999 \
-     --form ref=testjb \+     --form ref=master \
      --form "variables[VAR1]=plop" \      --form "variables[VAR1]=plop" \
      --form "variables[VAR2]=plop" \      --form "variables[VAR2]=plop" \
      --form "variables[VAR3]=plop" \      --form "variables[VAR3]=plop" \
      --form "variables[VAR4]=plop" \      --form "variables[VAR4]=plop" \
-     "http://gitlab.acme.local/api/v4/projects/100/trigger/pipeline"  +     "${CI_API_V4_URL}/projects/100/trigger/pipeline"  
 ~~~ ~~~
 +
 +Avec Ansible
 +
 +`.gitlab-ci.yml`
 +~~~yaml
 +launch:
 +  tags:
 +    # - gitlab-runner-sh-001
 +    - gitlab-runner-docker-001
 +  image: registry.acme.local/repository/local-docker/ansible-builder:0.0.4
 +  stage: build
 +  script: ansible-playbook play-launch-ci.yml -e url="${CI_API_V4_URL}" -e token="${TRIGGER_TOKEN}"
 +~~~
 +
 +`play-launch-ci.yml`
 +~~~yaml
 +#! /usr/bin/env ansible-playbook
 +
 +---
 +
 +- name: Launch CI
 +  gather_facts: false
 +  hosts: localhost
 +
 +  vars:
 +    branch: "{{ lookup('env', 'BRANCH') }}"
 +    line: "{{ lookup('env', 'LINE') }}"
 +    kind: "{{ lookup('env', 'KIND') }}"
 +
 +  tasks:
 +    - name: Assert check environment vars are correctly defined
 +      ansible.builtin.assert:
 +        that:
 +          - url is defined and url != ''
 +          - token is defined and token != ''
 +          - branch is defined and branch != ''
 +          - line is defined and line != ''
 +          - kind is defined and kind != ''
 +        msg: "Env var must be defined"
 +
 +    - name: Call API
 +      # no_log: true
 +      register: reg_uri
 +      until: reg_uri.status == 201
 +      retries: 3
 +      delay: 5    # Every 5 seconds
 +      ansible.builtin.uri:
 +        url: "{{ url }}/projects/100/trigger/pipeline"
 +        # validate_certs: false
 +        status_code: 201
 +        follow_redirects: none
 +        method: POST
 +        body_format: json
 +        headers:
 +          Content-Type: "application/json"
 +        body: |
 +          {
 +            "ref": "main",
 +            "token": "{{ token }}",
 +            "variables": {
 +              "BRANCH": "{{ branch }}",
 +              "KIND": "{{ kind }}",
 +              "LINE": "{{ line }}"
 +            }
 +          }
 +~~~
 +
    
 Voir https://docs.gitlab.com/ci/triggers/ Voir https://docs.gitlab.com/ci/triggers/
  
  
 +### Autres
  
 +~~~yaml
 +000-colo-switch:prod:
 +  stage: deploy
 +  variables:
 +    PROJECT: "projectname"
 +  rules: # whether to include the job in the pipeline
 +    - if: $CI_PIPELINE_SOURCE == "trigger"
 +      when: always
 +    - when: never
 +~~~
 +Source: https://forum.gitlab.com/t/triggering-a-specific-job-of-the-pipeline-via-api/42397/2
  
  
tech/notes_gitlab-ci_pipeline_-_api.1781710094.txt.gz · Dernière modification : de Jean-Baptiste

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki