forked from docs/doc-exports
247 lines
8.8 KiB
YAML
247 lines
8.8 KiB
YAML
name: Install Pandoc from Source
|
|
on:
|
|
pull_request:
|
|
types: [opened, reopened, synchronize, edited]
|
|
|
|
jobs:
|
|
install-pandoc:
|
|
runs-on: ubuntu-latest # adjust to your runner label
|
|
steps:
|
|
- name: Checkout Code
|
|
uses: actions/checkout@v3
|
|
|
|
- name: Download Pandoc 2.19.2
|
|
run: |
|
|
curl -L -o pandoc.tar.gz \
|
|
https://github.com/jgm/pandoc/releases/download/2.19.2/pandoc-2.19.2-linux-amd64.tar.gz
|
|
|
|
- name: Extract and Install Pandoc
|
|
run: |
|
|
tar xvzf pandoc.tar.gz --strip-components 1 -C /usr/local/
|
|
chmod +x /usr/local/bin/pandoc
|
|
|
|
- name: Verify Installation
|
|
run: pandoc --version
|
|
|
|
install-convertor:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Checkout Code
|
|
uses: actions/checkout@v3
|
|
|
|
- name: Set up Python
|
|
uses: actions/setup-python@v4
|
|
with:
|
|
python-version: '3.10' # adjust version as needed
|
|
|
|
- name: Clone doc-convertor from Gitea
|
|
env:
|
|
GITEA_TOKEN: ${{ secrets.TEST_TOKEN }}
|
|
GITEA_SERVER_URL: ${{ gitea.server_url }}
|
|
run: |
|
|
git clone https://${GITEA_TOKEN}@gitea.eco.tsi-dev.otc-service.com/docs/doc-convertor.git
|
|
|
|
- name: Create virtualenv and install package in editable mode
|
|
run: |
|
|
python -m venv ~/.venv
|
|
source ~/.venv/bin/activate
|
|
pip install --upgrade pip
|
|
pip install -e ./doc-convertor
|
|
|
|
parse-metadata-yaml:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- uses: actions/checkout@v3
|
|
|
|
- name: Set up Python
|
|
uses: actions/setup-python@v4
|
|
with:
|
|
python-version: '3.10' # adjust version as needed
|
|
|
|
- name: Install PyYAML
|
|
run: pip install pyyaml
|
|
|
|
- name: Load YAML and set outputs
|
|
id: metadata
|
|
shell: python
|
|
run: |
|
|
# python3 <<EOF > parsed.env
|
|
import yaml, os
|
|
with open("metadata.yaml") as f:
|
|
data = yaml.safe_load(f)
|
|
print(f"SERVICE_TITLE={data['categories']['security-services'][0]['service_title']}")
|
|
print(f"SERVICE_TYPE={data['categories']['security-services'][0]['service_type']}")
|
|
print(f"SERVICE_CATEGORY={data['categories']['security-services'][0]['service_category']}")
|
|
# EOF
|
|
# cat parsed.env >> $GITHUB_ENV
|
|
|
|
- name: Use config values
|
|
run: |
|
|
echo "SERVICE_TITLE: $SERVICE_TITLE"
|
|
echo "SERVICE_TYPE: $SERVICE_TYPE"
|
|
echo "SERVICE_CATEGORY: $SERVICE_CATEGORY"
|
|
|
|
|
|
generate-docs:
|
|
runs-on: ubuntu-latest
|
|
env:
|
|
DOCS_UPDATE_DATA_FILE: metadata.yaml
|
|
PROJECT_SRC_DIR: ./docs
|
|
|
|
steps:
|
|
- name: Checkout source repo
|
|
uses: actions/checkout@v3
|
|
|
|
- name: Install Python and dependencies
|
|
uses: actions/setup-python@v4
|
|
with:
|
|
python-version: 3.10
|
|
|
|
- name: Install PyYAML (to parse YAML files)
|
|
run: pip install pyyaml
|
|
|
|
- name: Read project docs configuration and extract needed vars
|
|
id: read_docs_config
|
|
run: |
|
|
python3 <<EOF
|
|
import yaml
|
|
import os
|
|
import json
|
|
from pathlib import Path
|
|
|
|
data_file = os.getenv('DOCS_UPDATE_DATA_FILE')
|
|
with open(data_file) as f:
|
|
data = yaml.safe_load(f)
|
|
|
|
# Simulate categories[docs_service_category] defaulting to {}
|
|
# docs_service_category = data.get('categories', '')
|
|
docs_service_category = "application"
|
|
print(docs_service_category)
|
|
categories = data.get('categories', {})
|
|
print(categories)
|
|
|
|
doc_exports_var = categories.get(docs_service_category, {})
|
|
print(doc_exports_var)
|
|
json_str = json.dumps(doc_exports_var, indent=4)
|
|
json_str = "doc_exports_var=" + json_str
|
|
print(json_str)
|
|
with open(os.environ['GITHUB_OUTPUT'], 'a') as fh:
|
|
fh.write(json_str)
|
|
|
|
# Write doc_exports_var to output & env file, serialized as JSON for easy later processing
|
|
with open('doc_exports_var.json', 'w') as jf:
|
|
json.dump(doc_exports_var, jf)
|
|
EOF
|
|
sleep 5
|
|
echo "Loaded doc_exports_var and saved to doc_exports_var.json"
|
|
ls -la ./doc_exports_var.json
|
|
cat ./doc_exports_var.json
|
|
|
|
# - name: Get last git changed files
|
|
# id: git_log
|
|
# run: |
|
|
# cd $PROJECT_SRC_DIR
|
|
# ls
|
|
# echo > ../changed_files.txt
|
|
# git branch
|
|
# git status
|
|
# git log -1 --name-only --pretty= > ../changed_files.txt || true
|
|
# cat ../changed_files.txt
|
|
# cat ../doc_exports_var.json
|
|
|
|
|
|
- name: Install jq
|
|
run: sudo apt-get update && sudo apt-get install -y jq
|
|
|
|
- name: Debug event file
|
|
run: |
|
|
echo "Gitea event file path: $GITEA_EVENT_PATH"
|
|
cat "$GITEA_EVENT_PATH"
|
|
|
|
- name: List changed files in PR
|
|
id: changed_files
|
|
run: |
|
|
PR_NUMBER=$(jq -r .pull_request.number "$GITEA_EVENT_PATH")
|
|
REPO=$(jq -r .repository.full_name "$GITEA_EVENT_PATH")
|
|
|
|
echo "PR #$PR_NUMBER from repo $REPO"
|
|
|
|
# Extract owner and repo name
|
|
OWNER=$(echo "$REPO" | cut -d'/' -f1)
|
|
REPO_NAME=$(echo "$REPO" | cut -d'/' -f2)
|
|
|
|
# Use Gitea API to get changed files
|
|
# Requires an API token with repo read access
|
|
curl -s -H "Authorization: token ${{ secrets.TEST_TOKEN }}" \
|
|
"https://gitea.eco.tsi-dev.otc-service.com/api/v1/repos/$OWNER/$REPO_NAME/pulls/$PR_NUMBER/files" \
|
|
| jq -r '.[].filename' > changed_files.txt
|
|
|
|
echo "Changed files:"
|
|
cat changed_files.txt
|
|
|
|
- name: Generate RST docs
|
|
run: |
|
|
python3 <<EOF
|
|
import json
|
|
import os
|
|
import re
|
|
from subprocess import run
|
|
|
|
# Load doc_exports_var from JSON
|
|
with open('doc_exports_var.json') as f:
|
|
doc_exports_var = json.load(f)
|
|
|
|
# Read changed files
|
|
with open('changed_files.txt') as f:
|
|
changed_files = f.read()
|
|
|
|
def file_changed(pattern):
|
|
return re.search(pattern, changed_files) is not None
|
|
|
|
# Loop through docs (assuming structure similar to ansible)
|
|
for doc0 in doc_exports_var.get('docs', []):
|
|
# Here doc0 = {service_type:..., repository:..., service_title:..., ...}
|
|
# We expect doc0 to contain a 'docs' list (per your subelements('docs'))
|
|
for doc1 in doc0.get('docs', []):
|
|
html_loc = doc1.get('html_location')
|
|
rst_loc = doc1.get('rst_location')
|
|
|
|
# Skip if html location or rst location is not defined
|
|
if not (html_loc and rst_loc):
|
|
continue
|
|
|
|
# Check if changed_files contain relevant paths
|
|
if (file_changed(html_loc) or
|
|
file_changed('metadata.yaml') or
|
|
file_changed('playbooks') or
|
|
file_changed('templates')):
|
|
|
|
# Build variables for convert_doc role
|
|
env_vars = {
|
|
"DOC_LABEL": f"{doc0.get('service_type')}_{doc1.get('type')}",
|
|
"DEST": os.path.expanduser(f"{doc0.get('repository')}/{doc1.get('rst_location')}"),
|
|
"SOURCE": os.path.join(os.getenv('PROJECT_SRC_DIR'), doc1.get('html_location')),
|
|
"DOC_TITLE": f"{doc0.get('service_title')} - {doc1.get('title')}",
|
|
"DOC_SHORT_TITLE": doc1.get('title'),
|
|
"DOC_ENVIRONMENT": doc0.get('service_environment'),
|
|
"DOC_LINK": doc1.get('link'),
|
|
"DOC_TYPE": doc1.get('type'),
|
|
"DOC_SERVICE": doc0.get('service_title'),
|
|
"DOC_SERVICE_CATEGORY": doc0.get('service_category'),
|
|
"DOC_SERVICE_TYPE": doc0.get('service_type'),
|
|
"DOC_REPO_NAME": doc0.get('repository'),
|
|
"DOC_PDF_NAME": doc1.get('pdf_name'),
|
|
}
|
|
|
|
# Export env vars for convert_doc command
|
|
env_str = " ".join([f'{k}="{v}"' for k, v in env_vars.items() if v])
|
|
|
|
print(f"Generating docs for {env_vars['DOC_LABEL']}")
|
|
|
|
# Convert the respective doc with metadata parameters
|
|
result = run(f"otc-doc-convertor {env_str}", shell=True)
|
|
if result.returncode != 0:
|
|
print(f"Error running convert_doc for {env_vars['DOC_LABEL']}")
|
|
exit(result.returncode)
|
|
EOF
|