Forráskód Böngészése

feat(action): add depoly action

Signed-off-by: Swilder-M <poxiaobbs@gmail.com>
Swilder-M 3 éve
szülő
commit
95e8b57760
3 módosított fájl, 206 hozzáadás és 0 törlés
  1. 13 0
      .ci/generate_version.py
  2. 33 0
      .ci/remove_unused.py
  3. 160 0
      .github/workflows/deploy_docs.yaml

+ 13 - 0
.ci/generate_version.py

@@ -0,0 +1,13 @@
+import sys
+import json
+
+version_list = sys.argv[1:]
+
+if __name__ == '__main__':
+    version_list.sort(
+        key=lambda v: [int(u) for u in v.split('.')],
+        reverse=True
+    )
+    version_list = [f'v{version}' for version in version_list]
+    version_list.insert(0, 'latest')
+    print(json.dumps(version_list))

+ 33 - 0
.ci/remove_unused.py

@@ -0,0 +1,33 @@
+import os
+import sys
+import json
+
+docs_path = sys.argv[1]
+
+
+def get_markdown_file(dir_config, base_path):
+    current_files = []
+    for row in dir_config:
+        if row.get('children'):
+            current_files += get_markdown_file(row['children'], base_path)
+        else:
+            current_files.append(
+                f'{base_path}/README.md' if row['path'] == './'
+                else f'{base_path}/{row["path"]}.md'
+            )
+    return current_files
+
+
+if __name__ == '__main__':
+    r = open(f'{docs_path}/directory.json', 'r')
+    directory_config = json.load(r)
+    markdown_files = get_markdown_file(directory_config['cn'], f'{docs_path}/zh_CN')
+    markdown_files += get_markdown_file(directory_config['en'], f'{docs_path}/en_US')
+
+    for file_path, dir_list, file_list in os.walk(docs_path):
+        for file_name in file_list:
+            if not file_name.endswith('.md'):
+                continue
+            if os.path.join(file_path, file_name) not in markdown_files:
+                print(f'Remove {os.path.join(file_path, file_name)}')
+                os.remove(os.path.join(file_path, file_name))

+ 160 - 0
.github/workflows/deploy_docs.yaml

@@ -0,0 +1,160 @@
+name: Deploy Docs
+
+concurrency: 
+  group: ${{ github.ref }}
+  cancel-in-progress: true
+
+on:
+  push:
+    branches:
+      - master
+
+jobs:
+  deploy:
+    runs-on: ubuntu-20.04
+    if: github.repository_owner == 'lf-edge'
+    steps:
+    - name: clone docs
+      uses: actions/checkout@v2
+      with:
+        fetch-depth: 0
+        path: docs-files
+
+    - name: clone frontend
+      uses: actions/checkout@v2
+      with:
+        repository: 'emqx/docs-emqx-com-frontend'
+        token: ${{ secrets.CI_GIT_TOKEN }}
+        path: frontend
+
+    - name: use node.js
+      uses: actions/setup-node@v1
+      with:
+        node-version: 14.15
+
+    - name: use python
+      uses: actions/setup-python@v2
+      with:
+        python-version: '3.8'
+        architecture: 'x64'
+
+    - name: set env
+      run: |
+         BRANCH=$(echo ${{ github.ref }} | sed -r  "s ^refs/heads/|^refs/tags/(.*) \1 g")
+         if [ "$BRANCH" = "master" ];then
+         VERSION="latest"
+         else
+         VERSION=v$BRANCH
+         fi
+         echo "DOCS_BRANCH=$BRANCH" >> $GITHUB_ENV
+         echo "VERSION=$VERSION" >> $GITHUB_ENV
+         echo "DOCS_TYPE=kuiper" >> $GITHUB_ENV
+
+    - name: get last version
+      run: |
+         cd docs-files
+         last_sha_url=https://docs.emqx.com/sha/${DOCS_TYPE}
+         if [[ "$VERSION" != "latest" ]];
+         then
+            is_changed=1
+         elif curl -fs $last_sha_url > /dev/null;
+         then
+            last_sha=$(curl $last_sha_url)
+            echo $last_sha;
+            if [[ -n $(git diff --name-only "$last_sha"...HEAD -- "docs") ]]
+            then 
+                is_changed=1
+            else
+                is_changed=0
+            fi
+         else
+            is_changed=1
+         fi
+         echo "CHANGED=$is_changed" >> $GITHUB_ENV
+
+    - name: remove unused files
+      if: ${{ env.CHANGED == '1' }}
+      run: |
+        cd docs-files
+        python3 .ci/remove_unused.py $(pwd)/docs
+
+    - name: move files
+      if: ${{ env.CHANGED == '1' }}
+      run: |
+        rm frontend/docs/en/README.md || true
+        rm frontend/docs/zh/README.md || true
+        rm frontend/docs/*.md || true
+        rm frontend/README.md
+        mkdir -p frontend/docs/en/${DOCS_TYPE}/${VERSION}/
+        mkdir -p frontend/docs/zh/${DOCS_TYPE}/${VERSION}/
+        mkdir -p frontend/docs/.vuepress/public/api/
+        cp -r docs-files/docs/en_US/* frontend/docs/en/${DOCS_TYPE}/${VERSION}/
+        cp -r docs-files/docs/zh_CN/* frontend/docs/zh/${DOCS_TYPE}/${VERSION}/
+        cp docs-files/docs/directory.json frontend/docs/.vuepress/config/directory.json
+        echo "[\"latest\"]" > frontend/docs/.vuepress/public/api/${DOCS_TYPE}_versions.json
+
+    - name: build docs
+      if: ${{ env.CHANGED == '1' }}
+      run: |
+        cd frontend
+        yarn && yarn build
+        if [[ "$VERSION" == "latest" ]];
+        then
+            mkdir -p docs/.vuepress/dist/sha/
+            touch docs/.vuepress/dist/sha/${DOCS_TYPE}
+            echo $GITHUB_SHA >> docs/.vuepress/dist/sha/${DOCS_TYPE}
+        fi
+
+    - name: upload dist
+      if: ${{ env.CHANGED == '1' }}
+      run: |
+        pip3 install coscmd
+        coscmd config -a ${{ secrets.COS_ID }} -s ${{ secrets.COS_KEY }} -b ${{ secrets.COS_NAME }} -r ${{ secrets.COS_REGION }}
+        coscmd delete -r -f en/${DOCS_TYPE}/${VERSION} || true
+        coscmd delete -r -f zh/${DOCS_TYPE}/${VERSION} || true
+        coscmd config -a ${{ secrets.COS_ID }} -s ${{ secrets.COS_KEY }} -b ${{ secrets.COS_NAME }} -e cos.accelerate.myqcloud.com
+        cd frontend/docs/.vuepress/dist/
+        zip -rq docs-dist-${DOCS_TYPE}-${VERSION}.zip ./
+        coscmd upload docs-dist-${DOCS_TYPE}-${VERSION}.zip docs-zip/
+        sleep 90
+
+    - name: flush CDN
+      if: ${{ env.CHANGED == '1' }}
+      run: |
+        pip3 install tccli
+        tccli configure set secretId ${{ secrets.COS_ID }}
+        tccli configure set secretKey ${{ secrets.COS_KEY }}
+        tccli configure set region ${{ secrets.COS_REGION }}
+        tccli cdn PurgePathCache --cli-unfold-argument --Paths https://docs.emqx.com/zh/${DOCS_TYPE}/${VERSION}/ https://docs.emqx.com/en/${DOCS_TYPE}/${VERSION}/ https://docs.emqx.com/sha/ --FlushType delete
+        tccli cdn PurgeUrlsCache --cli-unfold-argument --Urls https://docs.emqx.com/api/${DOCS_TYPE}_versions.json https://docs.emqx.com/sitemap_${DOCS_TYPE}_${VERSION}.xml
+
+    - name: clone docsearch
+      uses: actions/checkout@v2
+      with:
+        repository: 'Swilder-M/docsearch-scraper-simple'
+        path: docsearch
+
+    - name: Install pipenv
+      run: |
+          python -m pip install --upgrade pipenv wheel
+
+    - id: cache-pipenv
+      uses: actions/cache@v1
+      with:
+        path: ~/.local/share/virtualenvs
+        key: ${{ runner.os }}-pipenv-${{ hashFiles('**/Pipfile.lock') }}
+
+    - name: install dependencies
+      if: steps.cache-pipenv.outputs.cache-hit != 'true'
+      run: |
+        cd docsearch
+        pipenv install --keep-outdated
+
+    - name: update current version
+      if: ${{ env.VERSION == 'latest' && env.CHANGED == '1' }}
+      env:
+        APPLICATION_ID: ${{ secrets.DOCS_APPLICATION_ID }}
+        API_KEY: ${{ secrets.DOCS_API_KEY }}
+      run: |
+        cd docsearch
+        pipenv run python -m src.index config ${DOCS_TYPE} ${VERSION}