Skip to content

Publish APIs

Publish APIs #166

Workflow file for this run

###############################################################
# Copyright (c) 2024 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0
###############################################################
name: Publish APIs
on:
workflow_dispatch:
schedule:
- cron: "0 4 * * *"
env:
# API collector and specs download "docs" path
API_COLLECTOR_DIR: "src/api-collector"
jobs:
collect_openapi:
runs-on: ubuntu-latest
outputs:
specs_exists: ${{ steps.check_specs.outputs.exists }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: "${{ env.API_COLLECTOR_DIR }}/go.mod"
- name: Collect OpenAPI specs
id: collect_specs
run: |
RANDOM=$(uuidgen)
echo "RANDOM=${RANDOM}" >> $GITHUB_OUTPUT
cd ${{ env.API_COLLECTOR_DIR }}
go run main.go -owner ${{ github.repository_owner }} -token ${{ secrets.GITHUB_TOKEN }}
- name: Move multiple OpenAPI specs files in one directory into individual directories
run: |
# Find all directories containing multiple *.yaml or *.yml files
find "${{ env.API_COLLECTOR_DIR }}/docs" -type d | while read -r DIR; do
# Count how many YAML/YML files are in the current directory
FILES=($(find "$DIR" -maxdepth 1 -type f \( -name "*.yaml" -o -name "*.yml" \)))
# If there are more than one YAML file in the directory
if [ ${#FILES[@]} -gt 1 ]; then
# Loop through each YAML file
for FILE in "${FILES[@]}"; do
# Extract the filename without the extension
FILENAME=$(basename "$FILE")
BASENAME="${FILENAME%.*}"
# Create a directory named after the file (without the extension)
TARGET_DIR="$DIR/$BASENAME"
mkdir -p "$TARGET_DIR"
# Move the file into the newly created directory
mv "$FILE" "$TARGET_DIR/"
done
fi
done
echo "Multiple OpenAPI specs files in one directory have been organized into individual directories."
- name: Check for specs
id: check_specs
run: |
if [ -d "${{ env.API_COLLECTOR_DIR }}/docs" ]; then
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
fi
- uses: actions/upload-artifact@v4
if: steps.check_specs.outputs.exists == 'true'
with:
name: openapi-${{ steps.collect_specs.outputs.RANDOM }}
path: ${{ env.API_COLLECTOR_DIR }}/docs
generate_matrix:
runs-on: ubuntu-latest
needs: collect_openapi
if: needs.collect_openapi.outputs.specs_exists == 'true'
outputs:
specs: ${{ steps.create_specs_list.outputs.matrix }}
steps:
- uses: actions/checkout@v4
- name: Download OpenAPI specs artifacts
uses: actions/download-artifact@v4
with:
path: docs
pattern: openapi-*
merge-multiple: true
- name: Create OpenAPI specs list
id: create_specs_list
run: |
FILES_ARRAY=$(find docs -type f \( -name "*.yaml" -o -name "*.yml" \) | sed 's/.*/\"&\"/' | paste -sd "," -)
echo "matrix={\"specs\": [${FILES_ARRAY}]}" >> $GITHUB_OUTPUT
generate_swagger_ui:
needs: generate_matrix
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJson(needs.generate_matrix.outputs.specs) }}
steps:
- uses: actions/checkout@v4
- name: Download OpenAPI specs artifacts
uses: actions/download-artifact@v4
with:
path: docs
pattern: openapi-*
merge-multiple: true
- name: Determine spec file directory
id: determine_directory
run: |
FILE_PATH="${{ matrix.specs }}"
DIR_PATH=$(dirname "${FILE_PATH}")
PRODUCT_PATH=$(dirname "${DIR_PATH}")
RANDOM=$(uuidgen)
echo "DIR_PATH=${DIR_PATH}" >> $GITHUB_OUTPUT
echo "PRODUCT_PATH=${PRODUCT_PATH}" >> $GITHUB_OUTPUT
echo "RANDOM=${RANDOM}" >> $GITHUB_OUTPUT
- name: Generate Swagger UI
uses: Legion2/swagger-ui-action@v1
with:
output: ${{ steps.determine_directory.outputs.DIR_PATH }}/swagger-ui
spec-file: ${{ matrix.specs }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Generate Directory Listings
uses: jayanta525/[email protected]
with:
FOLDER: ${{ steps.determine_directory.outputs.DIR_PATH }}
- uses: actions/upload-artifact@v4
with:
name: swagger-${{ steps.determine_directory.outputs.RANDOM }}
path: docs
deploy_swagger_ui:
needs: generate_swagger_ui
runs-on: ubuntu-latest
steps:
- name: Download All Artifacts
uses: actions/download-artifact@v4
with:
path: docs
pattern: swagger-*
merge-multiple: true
- name: Generate Directory Listings
uses: jayanta525/[email protected]
with:
FOLDER: docs
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: docs