GitLab API – Automation

Trigger pipelines by using the GitLab API

The intention of this automation is to allow multiple pipelines to interact with each other. In this example, I have a custom npm package that needs to update the TypeScript definitions using the Introspection feature of GraphQL Server. Graphql-codegen allows us to connect to a GraphQL Server when the introspection is enabled (never enable it in production), and based on the schema definitions we can generate automatically all the TypeScript types. Every time a new change gets merged into the main branch the pipeline automatically will build and deploy the services in a low environment that we can call [DEV] at this point we can connect to the services hosted in DEV to upgrade our TypeScript definition with the new scheme generated. Because we have multiples repositories
  • Graphql Server repository
  •  Custom NPM Package that contains the TypeScripts
  • The main application that consumes the types from the custom NPM Package
Step-by-step
  1. GraphQl server gets updated and releases a new version
  2. In the release process GraphQL server kicks off the Custom NPM pipeline using `triggers`
  3. The Custom NPM package updates the types and releases a new version automatically
The diagram above illustrates the process, and also we can create a merge request automatically to merge manually or we can merge the changes automatically.

To trigger the pipeline

Lister the trigger

Use GitLab API locally to upgrade multiples applications

It is a script to update all features applications in one shot.
What it does is API request to GitLab and get all apps names and ssh URL
It will check if the app is in your system if not it will clone the app.
Then stash the changes if you have something and it will create a new branch with the name of the dependency to install.
It will install the dependency and commit the changes after the commit it will push the changes and automatically will open a Merge Request targeting the main branch.
ROOT
|-script
|-apps
|--|-app1
|--|-app2
|--|-app3
....

Step-by-Step

    1. Verify if we have installed in our system the dependency jq very handy JSON command line processor.
    2. Ask to the user to enter the dependency name and the version we want to upgrade. Example: react-dom and version 18.2.0
    3. Make a curl GET request to get the list of repositories
    4. Print in the terminal the list of repositories available, and allow the user to select which one wants to update
    5. After the selection is completed enter a loop to upgrade each repository selected
    6. If the repo does not exist clone it
    7. Stash any uncommitted changes to avoid losing changes
    8. Move to the main branch and pull the latest code
    9. Create a new branch with the dependency and version
    10. Install the dependency or upgrade it
    11. Commit the new changes and push them to the origin
    12. Open an automatic Merge Request
    13. Repeat the same operation in the next repository

Leave a Comment