Commit af550690 authored by Cédric OLIVIER's avatar Cédric OLIVIER
Browse files

Merge branch 'feat/component' into 'main'

feat: migrate to CI/CD component

See merge request to-be-continuous/spectral!3
parents a1d4c1d2 e32d8113
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ include:
    file: '/templates/validation.yml'
  - project: 'to-be-continuous/bash'
    ref: '3.3'
    file: 'templates/gitlab-ci-bash.yml'
    file: '/templates/gitlab-ci-bash.yml'
  - project: 'to-be-continuous/semantic-release'
    ref: '3.7'
    file: '/templates/gitlab-ci-semrel.yml'    
+32 −8
Original line number Diff line number Diff line
@@ -5,22 +5,45 @@ This project implements a GitLab CI/CD template to lint JSON/YAML documents with

## Usage

In order to include this template in your project, add the following to your `gitlab-ci.yml`:
This template can be used both as a [CI/CD component](https://docs.gitlab.com/ee/ci/components/#use-a-component-in-a-cicd-configuration) 
or using the legacy [`include:project`](https://docs.gitlab.com/ee/ci/yaml/index.html#includeproject) syntax.

### Use as a CI/CD component

Add the following to your `gitlab-ci.yml`:

```yaml
include:
  # 1: include the component
  - component: gitlab.com/to-be-continuous/spectral/gitlab-ci-spectral@1.0.0
    # 2: set/override component inputs
    inputs:
      extra-args: "--ruleset custom.spectral.yaml" # ⚠ this is only an example
```

### Use as a CI/CD template (legacy)

Add the following to your `gitlab-ci.yml`:

```yaml
include:
  # 1: include the template
  - project: 'to-be-continuous/spectral'
    ref: '1.0.0'
    file: '/templates/gitlab-ci-spectral.yml'

variables:
  # 2: set/override template variables
  SPECTRAL_EXTRA_ARGS: "--ruleset custom.spectral.yaml" # ⚠ this is only an example
```

## Global configuration

The Spectral template uses some global configuration used throughout all jobs.

| Name                  | Description                            | Default value     |
| Input / Variable | Description                            | Default value     |
| --------------------- | -------------------------------------- | ----------------- |
| `SPECTRAL_IMAGE` | The Docker image used to run `spectral` | `registry.hub.docker.com/stoplight/spectral:latest` |
| `image` / `SPECTRAL_IMAGE` | The Docker image used to run `spectral` | `registry.hub.docker.com/stoplight/spectral:latest` |

## Jobs

@@ -30,11 +53,11 @@ This job performs a [lint analysis](https://docs.stoplight.io/docs/spectral/docs

It uses the following variable:

| Name                  | Description                              | Default value     |
| Input / Variable | Description                              | Default value     |
| --------------------- | ---------------------------------------- | ----------------- |
| `SPECTRAL_DOCUMENTS`  | Location of JSON/YAML documents to be linted. Can be either a file, a glob or fetchable resource(s) on the web          | `{,api/,src/main/resources/}*{openapi,oas,swagger,asyncapi}*.{json,yml,yaml}` <br><br>see [glob syntax](https://github.com/mrmlnc/fast-glob#basic-syntax) for more details |
| `SPECTRAL_EXTRA_ARGS`      | Extra arguments for the [Spectral CLI](https://docs.stoplight.io/docs/spectral/docs/guides/2-cli.md) | _none_ |
| `SPECTRAL_DISABLED`      | Set to `true` to disable this job          | _none_ |
| `documents` / `SPECTRAL_DOCUMENTS` | Location of JSON/YAML documents to be linted. Can be either a file, a glob or fetchable resource(s) on the web          | `{,api/,src/main/resources/}*{openapi,oas,swagger,asyncapi}*.{json,yml,yaml}` <br><br>see [glob syntax](https://github.com/mrmlnc/fast-glob#basic-syntax) for more details |
| `extra-args` / `SPECTRAL_EXTRA_ARGS` | Extra arguments for the [Spectral CLI](https://docs.stoplight.io/docs/spectral/docs/guides/2-cli.md) | _none_ |
| `disabled` / `SPECTRAL_DISABLED` | Set to `true` to disable this job          | _none_ |


#### `$SPECTRAL_DOCUMENTS` default value
@@ -71,7 +94,8 @@ extends: spectral:oas # out-of-the-box OpenAPI ruleset provided by Spe

#### `$SPECTRAL_EXTRA_ARGS` use cases

The `$SPECTRAL_EXTRA_ARGS` can be used to override the defaults for optional arguments. Below are the most probably use cases: 
The `$SPECTRAL_EXTRA_ARGS` can be used to override the defaults for optional arguments.
Below are the most probable use cases: 

- Custom Ruleset location

+2 −2
Original line number Diff line number Diff line
@@ -27,13 +27,13 @@ if [[ "$curVer" ]]; then
  log_info "Bump version from \\e[33;1m${curVer}\\e[0m to \\e[33;1m${nextVer}\\e[0m (release type: $relType)..."

  # replace in README
  sed -e "s/ref: '$curVer'/ref: '$nextVer'/" README.md > README.md.next
  sed -e "s/ref: *'$curVer'/ref: '$nextVer'/" -e "s/ref: *\"$curVer\”/ref: \”$nextVer\”/" -e "s/component: *\(.*\)@$curVer/component: \1@$nextVer/" README.md > README.md.next
  mv -f README.md.next README.md

  # replace in template and variants
  for tmpl in templates/*.yml
  do
    sed -e "s/\"$curVer\"/\"$nextVer\"/" "$tmpl" > "$tmpl.next"
    sed -e "s/command: *\[\"--service\", \"\(.*\)\", \"$curVer\"\]/command: [\"--service\", \"\1\", \"$nextVer\"]/" "$tmpl" > "$tmpl.next"
    mv -f "$tmpl.next" "$tmpl"
  done
else
+2 −0
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@
  "description": "[Spectral](https://docs.stoplight.io/docs/spectral) is a JSON/YAML Linter with custom rulesets, out of the box support for OpenAPI and AsyncAPI",
  "template_path": "templates/gitlab-ci-spectral.yml",
  "kind": "analyse",
  "prefix": "spectral",
  "is_component": true,
  "variables": [
    {
      "name": "SPECTRAL_IMAGE",
+3.6 KiB (23.2 KiB)
Loading image diff...
Loading