Commit 22257b0c authored by FulcrandG's avatar FulcrandG
Browse files

Merge branch...

Merge branch '45-change-jobs-structure-to-have-documentation-and-versioning-inside-the-directory' into latest
parents 90c4ecdd e0a343b8
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
site/
docs/jobs/**/*
!docs/jobs/**/.pages
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ documentation:
    entrypoint: [""]
  stage: build
  script:
    - python3 builder/builder.py
    - mkdocs build -d ./public
  artifacts:
    when: always

builder/builder.py

0 → 100644
+124 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3

# This script is used to build the documentation that hub.go2scale.com will be using for every job added to the hub

# Directory skeleton for a job:
# ── jobs
#     └── <job_name>
#         ├── <job_name>.yml
#         ├── LICENSE
#         ├── job.yml
#         ├── README.md
#         └── versions
#             ├── 0.1.0.md
#             └──...

from pathlib import Path
from os import listdir
from yaml import full_load
from jinja2 import Environment, FileSystemLoader

# Job variables
jobs_dir = "jobs"
mkdocs_dir = "docs"
mkdocs_placeholder_file = "placeholder.md"
job_changelog_dir = "versions"
job_description_file = "README.md"
job_license_file = "LICENSE"
job_metadata_file = "job.yml"

mk_changelog_wrapper = "\n## Changelog\n\n* **[latest]**(current -> `<LATEST_RELEASE>`) : `<TAG_URL>`\n"
mk_license_wrapper = "??? License\n"
mk_placeholder_wrapper = "# 🚧 *Work in progress*\n\nThere is no job for this stage for now"

# Index variables
builder_dir = "builder"
template_dir = "templates"
template_name = "index.md.j2"
index_file = "index.md"
index = {"static_tests": [], "build": [], "dynamic_tests": [], "review": [], "deployment": []}

def get_conf(job_path, job_name):
  # Load yaml file
  with open(job_path + "/" + job_metadata_file) as file:
    return full_load(file)

def add_description(job_path, job_name, mkdocs_job_content):
  # Concatenate description to final file
  with open(job_path + "/" + job_description_file) as file:
    mkdocs_job_content += file.read()
  return mkdocs_job_content


def add_changelog(job_path, job_name, mkdocs_job_content):
  # Concatenate changelog to final file
  mkdocs_job_content += mk_changelog_wrapper

  # For now, we are just getting the latest release, but we will be adding a full link to the release later
  latest_release = listdir(job_path + "/" + job_changelog_dir)[-1][0:-3]
  mkdocs_job_content = mkdocs_job_content.replace("<LATEST_RELEASE>", latest_release)

  for release in listdir(job_path + "/" + job_changelog_dir)[::-1]:
    with open(job_path + "/" + job_changelog_dir + "/" + release) as file:
      mkdocs_job_content += file.read()
      # TODO replace <TAG_URL> by the link to the release
  
  # Adding a new line for consistency
  mkdocs_job_content += "\n"
  return mkdocs_job_content

def add_license(job_path, job_name, mkdocs_job_content):
  # Concatenate license to final file
  mkdocs_job_content += mk_license_wrapper

  with open(job_path + "/" + job_license_file) as file:
    for line in file.readlines():
      mkdocs_job_content += "    " + line
  return mkdocs_job_content

def create_job_doc(job):
  job_path = jobs_dir + "/" + job
  mkdocs_job_content = ""

  # Getting conf for indexing  
  conf = get_conf(job_path, job)
  index[conf["default_stage"]].append(conf)

  mkdocs_file_path = mkdocs_dir + "/" + jobs_dir + "/" + conf["default_stage"] + "/" + job + ".md"

  mkdocs_job_content = add_description(job_path, job, mkdocs_job_content)
  mkdocs_job_content = add_changelog(job_path, job, mkdocs_job_content)
  mkdocs_job_content = add_license(job_path, job, mkdocs_job_content)

  # Write final file
  with open(mkdocs_file_path, 'w+') as file:
    file.write(mkdocs_job_content)

def add_placeholder():
  # Verify that there is a .md file for every stage, or mkdocs will break
  stages = listdir(mkdocs_dir + "/" + jobs_dir)
  stages.remove(".pages")

  for stage in stages:
    if len(listdir(mkdocs_dir + "/" + jobs_dir + "/" + stage)) == 1:
      # There is only the .pages file, so mkdocs will break
      with open(mkdocs_dir + "/" + jobs_dir + "/" + stage + "/" + mkdocs_placeholder_file, "w+") as file:
        file.write(mk_placeholder_wrapper)
  
if __name__ == "__main__":
  # Iterate over every directories in jobs directory to create their job.md for the documentation
  jobs = listdir(jobs_dir)

  for job in jobs:
    create_job_doc(job)

  # Verify that there is a .md file for every stage, or mkdocs will break
  add_placeholder()

  # Using jinja2 with a template to create the index
  env = Environment(loader=FileSystemLoader(builder_dir + "/" + 'templates'))
  template = env.get_template(template_name)
  index_content = template.render(index=index)

  with open(mkdocs_dir + "/" + jobs_dir + "/" + index_file, "w+") as file:
    file.write(index_content)
+43 −0
Original line number Diff line number Diff line
# 🗂 Index

Jobs index. They are sorted using the [HUB default stages](/getting-started#stages).

## 🔎 Static tests

| Name | Description |
| ---- | ----------- |
{% for job in index["static_tests"] -%}
  | {% if job.icon is defined -%} {{ job.icon }} {% endif %}[{{- job.name }}](https://hub.go2scale.io/jobs/static_tests/{{ job.name }}/) | {{ job.description }} |
{% endfor %}

## 📦 Build

| Name | Description |
| ---- | ----------- |
{% for job in index["build"] -%}
  | {% if job.icon is defined -%} {{ job.icon }} {% endif %}[{{- job.name }}](https://hub.go2scale.io/jobs/build/{{ job.name }}/) | {{ job.description }} |
{% endfor %}

## 🛡 Dynamic tests

| Name | Description |
| ---- | ----------- |
{% for job in index["dynamic_tests"] -%}
  | {% if job.icon is defined -%} {{ job.icon }} {% endif %}[{{- job.name }}](https://hub.go2scale.io/jobs/dynamic_tests/{{ job.name }}/) | {{ job.description }} |
{% endfor %}

## 🙋 Review

| Name | Description |
| ---- | ----------- |
{% for job in index["review"] -%}
  | {% if job.icon is defined -%} {{ job.icon }} {% endif %}[{{- job.name }}](https://hub.go2scale.io/jobs/review/{{ job.name }}/) | {{ job.description }} |
{% endfor %}

## 🚀 Deployment

| Name | Description |
| ---- | ----------- |
{% for job in index["deployment"] -%}
  | {% if job.icon is defined -%} {{ job.icon }} {% endif %}[{{- job.name }}](https://hub.go2scale.io/jobs/deployment/{{ job.name }}/) | {{ job.description }} |
{% endfor %}

docs/jobs/index.md

deleted100644 → 0
+0 −37
Original line number Diff line number Diff line
# 🗂 Index

Jobs index. They are sorted using the [HUB default stages](/getting-started#stages).

## 🔎 Static tests

| Name | Description |
| ---- | ----------- |
| 🐨 [Coala](/jobs/static_tests/coala) | Check code quality using Coala |

## 📦 Build

| Name | Description |
| ---- | ----------- |
| 📒 [ApiDoc](/jobs/build/apidoc) | Creates a HTML documentation from API annotations in source code |
| 🐳 [Docker](/jobs/build/docker) | Build and publish Docker image using Kaniko |
| 📃 [Mkdocs](/jobs/build/mkdocs) | Build documentation from markdown to HTML using Mkdocs|

## 🛡 Dynamic tests

| Name | Description |
| ---- | ----------- |
| 🧱 [Trivy image analysis](/jobs/dynamic_tests/trivy_image) | Run a security analysis on docker image using Trivy |

## 🙋 Review

| Name | Description |
| ---- | ----------- |
| ☸️ [Helm](/jobs/deployment/helm) | Deploy a review environment on Kubernetes using a Helm chart |

## 🚀 Deployment

| Name | Description |
| ---- | ----------- |
| ☸️ [Helm](/jobs/deployment/helm) | Deploy on Kubernetes using a Helm chart |
| 🦊 [Pages](/jobs/deployment/pages) | Build and publish Docker image using Kaniko |
Loading