|
| 1 | +--- |
| 2 | +title: Streamline Your API Testing with Microcks GitLab Components |
| 3 | +date: 2025-10-16 |
| 4 | +image: "images/blog/gitlab-components.png" |
| 5 | +author: "Salahddine ABERKAN" |
| 6 | +type: "regular" |
| 7 | +description: "Discover how to seamlessly integrate API mocking and testing into your GitLab CI/CD pipelines using the new Microcks GitLab Components" |
| 8 | +draft: false |
| 9 | +tags: |
| 10 | + - "open-source" |
| 11 | + - "microcks" |
| 12 | + - "gitlab" |
| 13 | + - "CI" |
| 14 | +--- |
| 15 | + |
| 16 | +In today's fast-paced development environment, API testing and contract validation are critical to shipping reliable software. But how do you efficiently integrate these practices into your GitLab CI/CD pipelines without complex configurations? That's exactly what we're solving with the **Microcks GitLab Components**! |
| 17 | + |
| 18 | +A reusable CI/CD components is now available in the [GitLab CI/CD Catalog](https://gitlab.com/explore/catalog/microcks-cncf/microcks-community/microcks-gitlab-components). These components make it incredibly easy to import API artifacts and run contract conformance tests directly from your pipeline—with just a few lines of YAML! |
| 19 | + |
| 20 | +By leveraging GitLab Components, we've made Microcks integration as simple as including a component reference in your pipeline configuration. Let's see how! |
| 21 | + |
| 22 | +## What You Can Do with Microcks GitLab Components |
| 23 | + |
| 24 | +The Microcks GitLab Components provide two main capabilities: |
| 25 | + |
| 26 | +1. **Import API Artifacts** - Push your OpenAPI, AsyncAPI, Postman collections, and other specifications into your Microcks instance |
| 27 | +2. **Run Contract Tests** - Validate that your deployed API endpoints conform to their specifications |
| 28 | + |
| 29 | +## Getting Started |
| 30 | + |
| 31 | +### Prerequisites |
| 32 | + |
| 33 | +Before you begin, make sure you have: |
| 34 | + |
| 35 | +* A running Microcks instance with its API URL (e.g., `https://microcks.apps.acme.com/api/`) |
| 36 | +* A [Service Account](/documentation/explanations/service-account) configured in your Microcks Keycloak realm |
| 37 | +* The Service Account's `client_id` and `client_secret` |
| 38 | + |
| 39 | +> **Pro tip**: Store your credentials as masked GitLab CI variables for security! Navigate to Settings → CI/CD → Variables in your GitLab project and add: |
| 40 | +> - `MICROCKS_URL` → Your Microcks API endpoint |
| 41 | +> - `KEYCLOAK_CLIENT_ID` → Service Account client ID |
| 42 | +> - `KEYCLOAK_CLIENT_SECRET` → Service Account client secret |
| 43 | +
|
| 44 | +### Finding the Components in the Catalog |
| 45 | + |
| 46 | +The Microcks GitLab Components are published in the [GitLab CI/CD Catalog](https://gitlab.com/explore/catalog/microcks-cncf/microcks-community/microcks-gitlab-components) |
| 47 | + |
| 48 | +From there, you can browse documentation, view available versions, and see usage examples. Let's explore how to use each component! |
| 49 | + |
| 50 | +## Importing API Artifacts into Microcks |
| 51 | + |
| 52 | +The first component, `microcks-import`, allows you to push your API specifications into Microcks. This is perfect for keeping your mocks in sync with your latest API definitions. |
| 53 | + |
| 54 | +Here's a simple example: |
| 55 | + |
| 56 | +```yaml |
| 57 | +# .gitlab-ci.yml |
| 58 | +include: |
| 59 | + - component: gitlab.com/microcks-cncf/microcks-community/microcks-gitlab-components/microcks-import@~latest |
| 60 | + inputs: |
| 61 | + specs: "specs/weather-forecast-openapi.yml:true,specs/weather-forecast-postman.json:false" |
| 62 | + microcks_url: "https://microcks.apps.acme.com/api/" |
| 63 | + keycloak_client_id: $KEYCLOAK_CLIENT_ID |
| 64 | + keycloak_client_secret: $KEYCLOAK_CLIENT_SECRET |
| 65 | + stage: import |
| 66 | + |
| 67 | +stages: |
| 68 | + - import |
| 69 | +``` |
| 70 | +
|
| 71 | +Some important things to notice here: |
| 72 | +* You can customize the `stage` name to fit your pipeline structure |
| 73 | +* Using `@~latest` ensures you always get the most recent component version |
| 74 | + |
| 75 | +Once this runs, your API specifications will be imported into Microcks, and your development team can immediately start consuming mock endpoints! |
| 76 | + |
| 77 | +## Running Contract Conformance Tests |
| 78 | + |
| 79 | +The second component, `microcks-test`, runs contract tests against your deployed API endpoints. This ensures your implementation matches your specification—automatically catching breaking changes before they reach production! |
| 80 | + |
| 81 | +Here's how to use it: |
| 82 | + |
| 83 | +```yaml |
| 84 | +# .gitlab-ci.yml |
| 85 | +include: |
| 86 | + - component: gitlab.com/microcks-cncf/microcks-community/microcks-gitlab-components/microcks-test@~latest |
| 87 | + inputs: |
| 88 | + api_name_version: "Weather Forecast API:1.0.0" |
| 89 | + test_endpoint: "https://my-weather-api.example.com" |
| 90 | + test_runner: "OPEN_API_SCHEMA" |
| 91 | + microcks_url: "https://microcks.apps.acme.com/api/" |
| 92 | + keycloak_client_id: $KEYCLOAK_CLIENT_ID |
| 93 | + keycloak_client_secret: $KEYCLOAK_CLIENT_SECRET |
| 94 | + stage: test |
| 95 | + wait_for: "15sec" |
| 96 | +
|
| 97 | +stages: |
| 98 | + - test |
| 99 | +``` |
| 100 | + |
| 101 | +## Putting It All Together: A Pipeline sample |
| 102 | + |
| 103 | +Let's see how both components work together in a real-world pipeline. [The following example](https://gitlab.com/saberkan_personal/microcks-gitlab-components/-/tree/testing_components?ref_type=heads) demonstrates how to import a local spec file, and how to run the test against a running API. |
| 104 | + |
| 105 | +To run this example, you will need to: |
| 106 | +- Configure `$MICROCKS_DN` and `$PASTRY_API_DN` as GitLab CI variable. |
| 107 | +- Run the [Pastry API](https://microcks.io/documentation/tutorials/getting-started-tests/#deploying-the-api-implementation) |
| 108 | +- Configure a running [Microcks instance in Dev mode](https://microcks.io/documentation/guides/installation/docker-compose/#development-mode) |
| 109 | +- Load the [Pastry sample](https://microcks.io/documentation/tutorials/getting-started/#loading-a-sample) |
| 110 | + |
| 111 | + |
| 112 | +```yaml |
| 113 | +# .gitlab-ci.yml |
| 114 | +include: |
| 115 | + - component: $CI_SERVER_FQDN/microcks-cncf/microcks-community/microcks-gitlab-components/microcks-import@~latest |
| 116 | + inputs: |
| 117 | + specs: "samples/HelloService.postman.json:true" |
| 118 | + microcks_url: "http://$MICROCKS_DN:8080/api/" |
| 119 | + stage: import |
| 120 | + - component: $CI_SERVER_FQDN/microcks-cncf/microcks-community/microcks-gitlab-components/microcks-test@~latest |
| 121 | + inputs: |
| 122 | + api_name_version: "API Pastry - 2.0:2.0.0" |
| 123 | + test_endpoint: "http://$PASTRY_API_DN/" |
| 124 | + test_runner: "OPEN_API_SCHEMA" |
| 125 | + microcks_url: "http://$MICROCKS_DN:8080/api/" |
| 126 | + stage: test |
| 127 | +
|
| 128 | +stages: |
| 129 | + - import |
| 130 | + - test |
| 131 | +``` |
| 132 | + |
| 133 | +If the contract test fails, the pipeline stops, preventing broken APIs from reaching production. It's continuous verification in action! |
| 134 | + |
| 135 | +## Version Pinning for Stability |
| 136 | + |
| 137 | +While `@~latest` is great for always getting improvements, you might want stability in production pipelines. Simply specify a version: |
| 138 | + |
| 139 | +```yaml |
| 140 | +include: |
| 141 | + - component: gitlab.com/microcks-cncf/microcks-community/microcks-gitlab-components/[email protected] |
| 142 | +``` |
| 143 | + |
| 144 | +This ensures your pipeline behavior remains consistent even as new component versions are released. |
| 145 | + |
| 146 | +## Under the Hood |
| 147 | + |
| 148 | +Behind the scenes, these components leverage the [Microcks CLI](https://github.com/microcks/microcks-cli). |
| 149 | + |
| 150 | +The beauty of GitLab Components is that all this complexity is abstracted away—you just declare what you want, and the component handles the rest! |
| 151 | + |
| 152 | +## Wrap-Up |
| 153 | + |
| 154 | +With the Microcks GitLab Components, integrating API mocking and contract testing into your CI/CD pipelines has never been easier. You get: |
| 155 | + |
| 156 | +- **Simple integration** - Just include the component, no complex scripts |
| 157 | +- **Reusable configuration** - Use across all your projects |
| 158 | +- **Version control** - Pin versions or stay on latest |
| 159 | + |
| 160 | +Ready to give it a try? Check out the components in the [GitLab CI/CD Catalog](https://gitlab.com/explore/catalog/microcks-cncf/microcks-community/microcks-gitlab-components) and start mocking! |
| 161 | + |
| 162 | +## Improve the GitLab Components! |
| 163 | + |
| 164 | +The Microcks GitLab Components are designed to cover the most common use cases, but we know there's always room for improvement! The underlying [Microcks CLI](https://github.com/microcks/microcks-cli) supports many **advanced options** and **flags** that aren't yet exposed as component inputs. Feel free to contibute! |
0 commit comments