Skip to content

Commit 062b462

Browse files
JohnStarichBlaimi
andauthored
feat: build and publish container images for easy integration (#620)
* feat: Containerfile to create image from current source Signed-off-by: Matthias Blümel <[email protected]> * feat: Build and publish container images for easy integration * Run test-docker in CI with a valid IBM ruleset extension * Add docs for container image use on README * Add deploy step to push images Signed-off-by: John Starich <[email protected]> --------- Signed-off-by: Matthias Blümel <[email protected]> Signed-off-by: John Starich <[email protected]> Co-authored-by: Matthias Blümel <[email protected]>
1 parent 5312a71 commit 062b462

File tree

7 files changed

+112
-1
lines changed

7 files changed

+112
-1
lines changed

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
Dockerfile
3+
.git

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ before_install:
1515
script:
1616
- npm run test-travis
1717
- npm run lint
18+
- npm run test-docker
1819

1920
deploy:
2021
- provider: script
@@ -23,3 +24,10 @@ deploy:
2324
on:
2425
node: 18
2526
branch: main
27+
- provider: script
28+
skip_cleanup: true
29+
script: ./scripts/deploy.sh "$TRAVIS_TAG"
30+
on:
31+
node: 18
32+
all_branches: true
33+
condition: $TRAVIS_TAG =~ ^ibm-openapi-validator@.*$

Dockerfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM node:18-alpine
2+
LABEL org.opencontainers.image.source="https://github.com/IBM/openapi-validator"
3+
LABEL org.opencontainers.image.documentation="https://github.com/IBM/openapi-validator#container-image"
4+
LABEL org.opencontainers.image.licenses="Apache-2.0"
5+
LABEL org.opencontainers.image.title="OpenAPI Validator"
6+
LABEL org.opencontainers.image.description="The IBM OpenAPI Validator lets you validate OpenAPI 3.x documents according to the OpenAPI 3.x specification, as well as IBM-defined best practices."
7+
8+
WORKDIR /src
9+
# Copy and cache dependencies first for faster local rebuilds.
10+
COPY ./package.json ./package-lock.json /src/
11+
COPY ./packages/ruleset/package.json /src/packages/ruleset/package.json
12+
COPY ./packages/validator/package.json /src/packages/validator/package.json
13+
COPY ./packages/utilities/package.json /src/packages/utilities/package.json
14+
RUN npm ci
15+
16+
# Add validator to executable $PATH for easy integration in custom downstream images.
17+
ENV PATH=/usr/local/lib/node_modules:$PATH
18+
# Add IBM ruleset to Node module search path for easy volume mounts and ruleset extensions.
19+
ENV NODE_PATH=/usr/local/lib/node_modules
20+
COPY . /src
21+
RUN npm run link
22+
23+
WORKDIR /data
24+
ENTRYPOINT ["/usr/local/bin/lint-openapi"]

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ and [OpenAPI 3.1.x](https://github.com/OAI/OpenAPI-Specification/blob/master/ver
3535
* [Download an executable binary](#download-an-executable-binary)
3636
* [Build from source](#build-from-source)
3737
+ [Build platform-specific binaries](#build-platform-specific-binaries)
38+
* [Container image](#container-image)
39+
+ [Building your own](#building-your-own)
3840
- [Usage](#usage)
3941
* [Command Syntax](#command-syntax)
4042
* [Configuration](#configuration)
@@ -105,6 +107,44 @@ _If you installed the validator using `npm install -g ibm-openapi-validator`, yo
105107
#### Build platform-specific binaries
106108
It is also possible to build platform specific binaries from the source code by running `npm run pkg` in the project root directory. The binaries (lint-openapi-macos, lint-openapi-linux, lint-openapi-windows.exe) will be in the project's `packages/validator/bin` directory.
107109

110+
### Container image
111+
112+
Run the validator with the container image by mounting your API definition.
113+
114+
If it is named `openapi.yaml` in the current directory, then run:
115+
```bash
116+
docker run \
117+
--rm --tty \
118+
--volume "$PWD:/data:ro" \
119+
ibmdevxsdk/openapi-validator:latest \
120+
openapi.yaml
121+
```
122+
123+
You should **replace `latest` with a specific tagged version** to avoid any surprises when new releases are published.
124+
125+
Flag and argument syntax is the same as described in [Usage](#usage), but file paths are relative to `/data`.
126+
127+
To use a custom ruleset named `ruleset.yaml` in the current directory, run:
128+
```bash
129+
docker run \
130+
--rm --tty \
131+
--volume "$PWD:/data:ro" \
132+
ibmdevxsdk/openapi-validator:latest \
133+
--ruleset ruleset.yaml \
134+
openapi.yaml
135+
```
136+
137+
#### Building your own
138+
139+
If the existing image doesn't suit your needs, you could extend it and build your own.
140+
141+
For example, to build a validator image with your own custom ruleset package installed, make a `Dockerfile` like this:
142+
143+
```Dockerfile
144+
FROM ibmdevxsdk/openapi-validator:latest
145+
RUN npm install -g ${your-ruleset-pkg-here}
146+
```
147+
108148
## Usage
109149
### Command Syntax
110150
```bash

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111
"repository": "https://github.com/IBM/openapi-validator",
1212
"scripts": {
1313
"clean": "rm -rf node_modules/ && npm run clean --workspaces",
14+
"build-docker": "docker build -t ibmdevxsdk/openapi-validator:latest .",
1415
"test": "npm run test --workspaces",
1516
"test-ruleset": "npm run test --workspace packages/ruleset",
1617
"test-utilities": "npm run test --workspace packages/utilities",
1718
"test-validator": "npm run test --workspace packages/validator",
19+
"test-docker": "npm run build-docker && docker run --rm --volume \"$PWD\"/packages/validator/test/cli-validator/alternate-spectral-configs/extends-default.yaml:/data/ruleset.yaml --volume \"$PWD\"/packages/validator/test/cli-validator/mock-files/oas3/clean.yml:/data/openapi.yaml ibmdevxsdk/openapi-validator:latest --ruleset ruleset.yaml openapi.yaml",
1820
"jest": "jest",
1921
"test-travis": "npm run test-travis --workspaces",
2022
"lint": "npm run lint --workspaces",
2123
"fix": "npm run fix --workspaces",
2224
"pkg": "npm run pkg --workspace packages/validator",
23-
"link": "npm run link --workspace packages/validator",
25+
"link": "npm run link --workspace packages/validator --workspace packages/ruleset",
2426
"unlink": "npm run unlink --workspace packages/validator",
2527
"all": "npm run test && npm run lint",
2628
"format-readme-toc": "markdown-toc --maxdepth 5 -i README.md"

packages/ruleset/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"repository": "https://github.com/IBM/openapi-validator",
99
"scripts": {
1010
"clean": "rm -rf node_modules",
11+
"link": "npm install -g",
1112
"jest": "jest",
1213
"test": "jest test",
1314
"test-travis": "jest --silent --runInBand --no-colors --testNamePattern='^((?!@skip-ci).)*$' test",

scripts/deploy.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
deploy() {
6+
local travis_tag=$1
7+
login "$DOCKER_HUB_TOKEN"
8+
deploy_docker "$travis_tag"
9+
}
10+
11+
login() {
12+
local token=$1
13+
if [[ -z "$token" ]]; then
14+
echo 'No Docker Hub token available' >&2
15+
exit 2
16+
fi
17+
docker login --username ibmdevxsdk --password-stdin <<<"$token"
18+
}
19+
20+
21+
deploy_docker() {
22+
local tag=$1
23+
[[ "$tag" =~ ibm-openapi-validator@([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+) ]]
24+
local docker_tag=${BASH_REMATCH[1]}
25+
26+
npm run build-docker
27+
docker tag ibmdevxsdk/openapi-validator:latest ibmdevxsdk/openapi-validator:"$docker_tag"
28+
29+
docker push ibmdevxsdk/openapi-validator:"$docker_tag"
30+
docker push ibmdevxsdk/openapi-validator:latest
31+
}
32+
33+
deploy "$@"

0 commit comments

Comments
 (0)