From 718cbcec10ec529d59813c82b80a3dac38abf6b4 Mon Sep 17 00:00:00 2001 From: medcl Date: Wed, 13 Nov 2024 10:43:33 +0800 Subject: [PATCH] build: adding build workflow --- .github/workflows/build-docs.yml | 73 ++++++++++++++++++++++++++++++++ Makefile | 25 ++++++----- 2 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/build-docs.yml diff --git a/.github/workflows/build-docs.yml b/.github/workflows/build-docs.yml new file mode 100644 index 00000000..47a762dd --- /dev/null +++ b/.github/workflows/build-docs.yml @@ -0,0 +1,73 @@ +name: Build and Deploy Docs + +on: + push: + branches: + - main + - 'v*' + +jobs: + build-deploy-docs: + runs-on: ubuntu-latest + + steps: + - name: Checkout Product Repo + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Set Variables Based on Branch + id: vars + run: | + CURRENT_BRANCH=${GITHUB_REF##*/} + if [[ "$CURRENT_BRANCH" == "main" ]]; then + echo "VERSION=latest" >> $GITHUB_ENV + echo "BRANCH=main" >> $GITHUB_ENV + echo "VERSIONS=latest,`git branch -r --list "origin/v*" | sed 's|origin/||' | sort -Vr | tr '\n' ',' | sed 's/,$//'`" >> $GITHUB_ENV + elif [[ "$CURRENT_BRANCH" =~ ^v.* ]]; then + echo "VERSION=$CURRENT_BRANCH" >> $GITHUB_ENV + echo "BRANCH=$CURRENT_BRANCH" >> $GITHUB_ENV + # Create VERSIONS with "latest" and current VERSION, separated by commas + #echo "VERSIONS=latest,$CURRENT_BRANCH" >> $GITHUB_ENV + echo "VERSIONS=latest,`git branch -r --list "origin/v*" | sed 's|origin/||' | sort -Vr | tr '\n' ',' | sed 's/,$//'`" >> $GITHUB_ENV + else + echo "Skipping docs build, branch doesn't match versioning pattern." + exit 0 + fi + + - name: Install Hugo + run: | + wget https://github.com/gohugoio/hugo/releases/download/v0.79.1/hugo_extended_0.79.1_Linux-64bit.tar.gz + tar -xzvf hugo_extended_0.79.1_Linux-64bit.tar.gz + sudo mv hugo /usr/local/bin/ + + - name: Prepare Config File + run: make config + + - name: Checkout Docs Repo + uses: actions/checkout@v2 + with: + repository: infinilabs/docs + path: docs-output + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Build Documentation + run: | + OUTPUT=$(pwd)/docs-output make build + + - name: Commit and Push Changes + working-directory: docs-output + run: | + git config user.name "GitHub Actions" + git config user.email "actions@github.com" + if [[ -n $(git status --porcelain) ]]; then + git add . + git commit -m "$(git log -1 --pretty=%B)" + git push origin main + else + echo "No changes to commit." + fi + + - name: Clean Up Config File + if: success() + run: make restore-generated-file \ No newline at end of file diff --git a/Makefile b/Makefile index dac140e8..70a60e48 100644 --- a/Makefile +++ b/Makefile @@ -2,25 +2,28 @@ SHELL=/bin/bash # Basic info PRODUCT?= gateway -VERSION?= "latest" -VERSIONS?= "latest,v1.3.0" -BRANCH?= "main" +BRANCH?= main +VERSION?= $(shell [[ "$(BRANCH)" == "main" ]] && echo "latest" || echo "$(BRANCH)") +VERSIONS?= "latest" OUTPUT?= "/tmp/gateway-docs" +# Temporary file path for branches +BRANCH_FILE := $(OUTPUT)/branch_list.txt + .PHONY: build default: build config: - cp docs/config.yaml config.bak - # Replace placeholder (e.g., "BRANCH") in config.toml with the VERSION environment variable - sed -i '' "s/BRANCH/$(BRANCH)/g" docs/config.yaml + cp docs/config.yaml config.bak + # Replace placeholder (e.g., "BRANCH") in config.toml with the VERSION environment variable + sed -i '' "s/BRANCH/$(BRANCH)/g" docs/config.yaml build: config - cd docs && hugo.old --minify --theme book --destination="$(OUTPUT)"/"$(PRODUCT)"/"$(VERSION)"\ - --baseURL="/$(PRODUCT)"/"$(VERSION)" 1> /dev/null - @$(MAKE) restore-generated-file + echo $(VERSIONS) + cd docs && hugo.old --minify --theme book --destination="$(OUTPUT)/$(PRODUCT)/$(VERSION)" \ + --baseURL="/$(PRODUCT)/$(VERSION)" 1> /dev/null + @$(MAKE) restore-generated-file restore-generated-file: - # Restore the original config.toml - mv config.bak docs/config.yaml \ No newline at end of file + mv config.bak docs/config.yaml \ No newline at end of file