From 543a2b333085d945c7fb891e6cb797ffa6d303df Mon Sep 17 00:00:00 2001 From: "tao.yang" Date: Wed, 6 Sep 2023 17:13:55 +0800 Subject: [PATCH] Change the process of CI publishing charts and documents Signed-off-by: tao.yang --- .github/workflows/auto-release.yaml | 11 +- .github/workflows/call-release-chart.yaml | 123 +++++++++++++++++++++ .github/workflows/call-release-pages.yaml | 128 ++++++++++++---------- docs/mkdocs.yml | 5 + docs/overrides/main.html | 8 ++ 5 files changed, 217 insertions(+), 58 deletions(-) create mode 100644 .github/workflows/call-release-chart.yaml create mode 100644 docs/overrides/main.html diff --git a/.github/workflows/auto-release.yaml b/.github/workflows/auto-release.yaml index 7e2e0cdc..a263505e 100644 --- a/.github/workflows/auto-release.yaml +++ b/.github/workflows/auto-release.yaml @@ -111,15 +111,22 @@ jobs: ref: ${{ needs.get-tag.outputs.tag }} secrets: inherit + release-chart: + needs: [build-release-image, get-tag] + uses: ./.github/workflows/call-release-chart.yaml + with: + ref: ${{ needs.get-tag.outputs.tag }} + secrets: inherit + create-release: - needs: [ release-changelog, get-tag, release-pages] + needs: [ release-changelog, get-tag, release-chart] name: create release runs-on: ubuntu-latest steps: - name: Download Chart Artifact uses: actions/download-artifact@v3.0.2 with: - name: ${{ needs.release-pages.outputs.artifact }} + name: ${{ needs.release-chart.outputs.artifact }} path: chart-package/ - name: Download Changelog Artifact diff --git a/.github/workflows/call-release-chart.yaml b/.github/workflows/call-release-chart.yaml new file mode 100644 index 00000000..9e8884da --- /dev/null +++ b/.github/workflows/call-release-chart.yaml @@ -0,0 +1,123 @@ +name: Call Release Chart + +env: + MERGE_BRANCH: github_pages + PR_LABEL: pr/robot_update + PR_REVIWER: weizhoublue + CHART_OUTPUT_PATH: output/chart/* + +on: + workflow_call: + inputs: + ref: + required: true + type: string + outputs: + artifact: + description: "name of chart artifact" + value: chart_package_artifact + # --- call by manual + workflow_dispatch: + inputs: + ref: + description: 'branch, tag, sha' + required: true + default: main + +permissions: write-all + +jobs: + package: + name: prepare + runs-on: ubuntu-latest + outputs: + ref: ${{ env.RUN_REF }} + steps: + - name: Get Ref + id: get_ref + run: | + pwd + ls + if ${{ github.event_name == 'workflow_dispatch' }}; then + echo "call by workflow_dispatch" + echo "REF=${{ github.event.inputs.ref }}" >> $GITHUB_ENV + elif ${{ inputs.ref != '' }}; then + echo "call by workflow_call" + echo "REF=${{ inputs.ref }}" >> $GITHUB_ENV + else + echo "unexpected event: ${{ github.event_name }}" + exit 1 + fi + + - name: Checkout Code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + ref: ${{ env.REF }} + + - name: Install yq + run: | + YQ_VERSION=v4.33.1 + YQ_BINARY="yq_$(uname | tr 'A-Z' 'a-z')_amd64" + wget https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/${YQ_BINARY}.tar.gz -O /tmp/yq.tar.gz + tar -xzf /tmp/yq.tar.gz -C /tmp + sudo mv /tmp/${YQ_BINARY} /usr/bin/yq + sudo chmod +x /usr/bin/yq + yq &>/dev/null || exit 1 + + - name: Build chart + run: | + make chart_package + + - name: Upload Artifact + uses: actions/upload-artifact@v3.1.2 + with: + name: chart_package_artifact + path: ${{ env.CHART_OUTPUT_PATH }} + retention-days: 1 + if-no-files-found: error + + create_pr: + name: Create PR + needs: [package] + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@v3 + with: + ref: ${{ env.MERGE_BRANCH }} + fetch-depth: 0 + + ## chart + - name: Install Helm + uses: azure/setup-helm@v3.5 + + - name: Download Chart Artifact + uses: actions/download-artifact@v3.0.2 + with: + name: chart_package_artifact + path: charts + + - name: Update Chart Yaml + run: | + name=${{ github.repository }} + proj=${name#*/} + url=https://${{ github.repository_owner }}.github.io/${proj} + helm repo index ./charts --url ${url}/charts + mv ./charts/index.yaml ./index.yaml + + # Allow auto-merge on general + - name: Create Pull Request + id: create_pr + uses: peter-evans/create-pull-request@v5.0.2 + with: + title: "robot update chart from ${{ needs.package.outputs.REF }} to branch ${{ env.MERGE_BRANCH }} " + commit-message: "robot update chart from ${{ needs.package.outputs.REF }} to branch ${{ env.MERGE_BRANCH }} " + branch-suffix: timestamp + branch: robot/update_chart + delete-branch: true + base: ${{ env.MERGE_BRANCH }} + signoff: true + token: ${{ secrets.GITHUB_TOKEN }} + labels: ${{ env.PR_LABEL }} + reviewers: ${{ env.PR_REVIWER }} diff --git a/.github/workflows/call-release-pages.yaml b/.github/workflows/call-release-pages.yaml index a1b44fba..37628de3 100644 --- a/.github/workflows/call-release-pages.yaml +++ b/.github/workflows/call-release-pages.yaml @@ -4,7 +4,6 @@ env: MERGE_BRANCH: github_pages PR_LABEL: pr/robot_update PR_REVIWER: weizhoublue - CHART_OUTPUT_PATH: output/chart/* on: workflow_call: @@ -12,10 +11,6 @@ on: ref: required: true type: string - outputs: - artifact: - description: "name of chart artifact" - value: chart_package_artifact # --- call by manual workflow_dispatch: inputs: @@ -32,18 +27,19 @@ jobs: runs-on: ubuntu-latest outputs: ref: ${{ env.RUN_REF }} + skip_all_job: ${{ env.SKIP_ALL_JOB }} steps: - name: Get Ref id: get_ref run: | pwd ls - if ${{ inputs.ref != '' }}; then - echo "call by workflow_call" - echo "RUN_REF=${{ inputs.ref }}" >> $GITHUB_ENV - elif ${{ github.event_name == 'workflow_dispatch' }} ; then + if ${{ github.event_name == 'workflow_dispatch' }}; then echo "call by workflow_dispatch" - echo "RUN_REF=${{ github.event.inputs.ref }}" >> $GITHUB_ENV + echo "REF=${{ github.event.inputs.ref }}" >> $GITHUB_ENV + elif ${{ inputs.ref != '' }}; then + echo "call by workflow_call" + echo "REF=${{ inputs.ref }}" >> $GITHUB_ENV else echo "unexpected event: ${{ github.event_name }}" exit 1 @@ -52,90 +48,110 @@ jobs: - name: Checkout Code uses: actions/checkout@v3 with: - ref: ${{ steps.get_ref.outputs.ref }} + fetch-depth: 0 + ref: ${{ env.REF }} + + - name: Extract Version + id: extract + run: | + if ! grep -E "^[[:space:]]*v[0-9]+.[0-9]+.[0-9]+[[:space:]]*$" VERSION &>/dev/null ; then + echo "not a release version, skip generating doc." + cat VERSION + echo "SKIP_ALL_JOB=true" >> $GITHUB_ENV + exit 0 + fi + # for example v0.6.1, the build's documentation version is v0.6 + docVersion=` cat VERSION | tr -d ' ' | tr -d '\n' | grep -Eo "v[0-9]+\.[0-9]+" ` + if [ -n "${docVersion}" ]; then + echo "the version intercepted from the branch is: ${docVersion}" + else + echo "error, failed to get version." && exit 1 + fi + git checkout -f ${{ env.MERGE_BRANCH }} + echo "Switch to the branch:${{ env.MERGE_BRANCH }} where the document is located" + ls + if [ -e "${docVersion}" ]; then + echo "doc version:${docVersion} already exists, just update it." + echo "SET_LATEST=false" >> $GITHUB_ENV + else + echo "The doc version:${docVersion} does not exist yet, while generating the doc and set it to latest" + echo "SET_LATEST=true" >> $GITHUB_ENV + fi + echo "the doc version is: ${docVersion}" + echo "DOCS_TAG=${docVersion}" >> $GITHUB_ENV - - name: Build Site and chart + - name: build doc site + id: build_doc + if: ${{ env.SKIP_ALL_JOB != 'true' }} run: | - make build_doc - make chart_package + git checkout ${{ env.REF }} + ls + echo "switch to the release version branch ${{ env.REF }}" + pip install mkdocs==1.5.2 mike==1.1.2 mkdocs-material==9.2.8 + git config user.email "robot@example.com" + git config user.name "robot" + cp ./docs/mkdocs.yml ./ + if ${{ env.SET_LATEST == 'true' }} ;then + echo "generate doc version:${{ env.DOCS_TAG }} and set to latest." + mike deploy --rebase -b ${{ env.MERGE_BRANCH }} --update-aliases ${{env.DOCS_TAG }} latest + mike set-default -b ${{ env.MERGE_BRANCH }} latest + else + echo "the version:${{ env.DOCS_TAG }} of the doc does not need to be set to the latest." + mike deploy --rebase -b ${{ env.MERGE_BRANCH }} ${{ env.DOCS_TAG }} + fi + rm -rf ./site + rm -rf ./mkdocs.yml + git checkout -f ${{ env.MERGE_BRANCH }} + rm -rf ./charts && rm -rf ./index.yaml && rm -rf ./changelogs + tar -czvf ./site.tar.gz * + ls + echo "Automatic release, offline doc site package ready" + echo "Push a doc version: ${{ env.DOCS_TAG }} from branch: ${{ env.REF }}, update it to latest: ${{ env.SET_LATEST }} " - name: Upload Artifact uses: actions/upload-artifact@v3.1.2 + if: ${{ env.SKIP_ALL_JOB != 'true' }} with: name: site_artifact - path: output/docs/site.tar.gz + path: site.tar.gz retention-days: 0 if-no-files-found: error - - name: Upload Artifact - uses: actions/upload-artifact@v3.1.2 - with: - name: chart_package_artifact - path: ${{ env.CHART_OUTPUT_PATH }} - retention-days: 1 - if-no-files-found: error - create_pr: name: Create PR needs: [package] + if: ${{ needs.package.outputs.skip_all_job != 'true' }} runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v3 with: ref: ${{ env.MERGE_BRANCH }} + fetch-depth: 0 ## doc - - name: Prepare - run: | - # prepare directory - mv docs/charts charts - rm -rf docs - mkdir docs - - name: Download Artifact uses: actions/download-artifact@v3.0.2 with: name: site_artifact - path: docs - name: Untar Doc run: | - cd docs tar -xzvf site.tar.gz rm -f site.tar.gz - ## chart - - name: Install Helm - uses: azure/setup-helm@v3.5 - - - name: Download Chart Artifact - uses: actions/download-artifact@v3.0.2 - with: - name: chart_package_artifact - path: charts/ - - - name: Update Chart Yaml - run: | - mv charts docs/charts - name=${{ github.repository }} - proj=${name#*/} - url=https://${{ github.repository_owner }}.github.io/${proj} - cd docs - helm repo index ./charts --url ${url}/charts - mv ./charts/index.yaml ./index.yaml - - # https://github.com/peter-evans/create-pull-request + # Allow auto-merge on general - name: Create Pull Request id: create_pr uses: peter-evans/create-pull-request@v5.0.2 with: - title: "robot Update doc from ${{ needs.prepare_doc.outputs.ref }} to branch ${{ env.MERGE_BRANCH }} " - commit-message: "robot Update chart from ${{ needs.prepare_doc.outputs.ref }} to branch ${{ env.MERGE_BRANCH }} " + title: "robot update website from ${{ needs.package.outputs.REF }} to branch ${{ env.MERGE_BRANCH }} " + commit-message: "robot update website from ${{ needs.package.outputs.REF }} to branch ${{ env.MERGE_BRANCH }} " branch-suffix: timestamp branch: robot/update_doc delete-branch: true base: ${{ env.MERGE_BRANCH }} signoff: true - token: ${{ secrets.WELAN_PAT }} + token: ${{ secrets.GITHUB_TOKEN }} labels: ${{ env.PR_LABEL }} + reviewers: ${{ env.PR_REVIWER }} diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index d4f757b1..acabda75 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -7,6 +7,7 @@ repo_url: https://github.com/spidernet-io/rocktemplate remote_branch: github_pages theme: name: material + custom_dir: docs/overrides # The maximum depth of the navigation tree in the sidebar navigation_depth: 2 palette: @@ -28,6 +29,10 @@ plugins: - tags - search +extra: + version: + provider: mike + markdown_extensions: - meta - toc diff --git a/docs/overrides/main.html b/docs/overrides/main.html new file mode 100644 index 00000000..35591d61 --- /dev/null +++ b/docs/overrides/main.html @@ -0,0 +1,8 @@ +{% extends "base.html" %} + +{% block outdated %} + You're not viewing the latest version. + + Click here to go to latest. + +{% endblock %} \ No newline at end of file