|
| 1 | +name: Generate Protobuf Documentation |
| 2 | + |
| 3 | +# Define a manual trigger with input for version |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - master |
| 8 | + |
| 9 | +jobs: |
| 10 | + build: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + steps: |
| 14 | + # Step 1: Checkout the repository code |
| 15 | + - name: Checkout code |
| 16 | + uses: actions/checkout@v3 |
| 17 | + |
| 18 | + # Step 1.1: Check Version Number |
| 19 | + - name: Check Version Number |
| 20 | + run: | |
| 21 | + pwd |
| 22 | + PROTO_VERSION=$(grep -oP '(?<=version\s)[0-9]+\.[0-9]+' ./idl/grpc/service.proto) |
| 23 | + THRIFT_VERSION=$(grep -oP '(?<=version\s)[0-9]+\.[0-9]+' ./idl/thrift/soccer_service.thrift) |
| 24 | + if [ "$PROTO_VERSION" != "$THRIFT_VERSION" ]; then |
| 25 | + echo "Version mismatch: Protobuf version is $PROTO_VERSION, Thrift version is $THRIFT_VERSION" |
| 26 | + exit 1 |
| 27 | + fi |
| 28 | +
|
| 29 | + # Step 2: Install dependencies for protoc |
| 30 | + - name: Install protoc dependencies |
| 31 | + run: sudo apt-get update && sudo apt-get install -y protobuf-compiler wget |
| 32 | + |
| 33 | + # Step 3: Download precompiled protoc-gen-doc binary |
| 34 | + - name: Download protoc-gen-doc binary |
| 35 | + run: | |
| 36 | + # Download the appropriate precompiled binary for Linux |
| 37 | + wget https://github.com/pseudomuto/protoc-gen-doc/releases/download/v1.5.1/protoc-gen-doc_1.5.1_linux_amd64.tar.gz -O protoc-gen-doc.tar.gz |
| 38 | +
|
| 39 | + # Extract the binary from the tarball |
| 40 | + tar -xvf protoc-gen-doc.tar.gz |
| 41 | + |
| 42 | + # Ensure it's an executable binary |
| 43 | + file protoc-gen-doc |
| 44 | + |
| 45 | + # Make it executable |
| 46 | + chmod +x protoc-gen-doc |
| 47 | + |
| 48 | + # Move the binary to /usr/local/bin |
| 49 | + sudo mv protoc-gen-doc /usr/local/bin/ |
| 50 | +
|
| 51 | + # Step 4: Generate Markdown from the Protobuf file |
| 52 | + - name: Generate Protobuf Documentation |
| 53 | + run: | |
| 54 | + # Generate markdown from .proto file |
| 55 | + protoc --doc_out=./idl --doc_opt=markdown,readme.md ./idl/grpc/service.proto |
| 56 | +
|
| 57 | + # Step 5: Extract version from the first line of the .proto file |
| 58 | + - name: Extract version from .proto file |
| 59 | + id: extract_version |
| 60 | + run: | |
| 61 | + VERSION=$(grep -oP '(?<=version\s)[0-9]+\.[0-9]+' ./idl/grpc/service.proto) |
| 62 | + echo "VERSION=$VERSION" >> $GITHUB_ENV |
| 63 | +
|
| 64 | + # Step 6: Insert version into the generated Markdown file |
| 65 | + - name: Insert version into markdown |
| 66 | + run: | |
| 67 | + sed -i '3a\\n## Version: '"${{ env.VERSION }}"'\n' ./idl/readme.md |
| 68 | +
|
| 69 | + # Step 7: Replace > and < in Mermaid diagrams with > and < |
| 70 | + - name: Fix Mermaid symbols in readme.md |
| 71 | + run: | |
| 72 | + sed -i 's/>/>/g' ./idl/readme.md |
| 73 | + sed -i 's/</</g' ./idl/readme.md |
| 74 | +
|
| 75 | + # Step 8: Configure Git and commit the updated readme.md |
| 76 | + - name: Configure Git |
| 77 | + run: | |
| 78 | + git config --global user.name "GitHub Action" |
| 79 | + git config --global user.email "[email protected]" |
| 80 | +
|
| 81 | + # Step 10: Stage and Commit Changes |
| 82 | + - name: Stage and Commit Changes |
| 83 | + run: | |
| 84 | + ls ./idl |
| 85 | + git status |
| 86 | + git add ./idl/readme.md # Ensure the correct file is added |
| 87 | + git diff --quiet || git commit -m "Update protobuf documentation with version and fixed Mermaid symbols" || echo "No changes to commit" |
| 88 | +
|
| 89 | + # Step 11: Push the changes |
| 90 | + - name: Push Changes |
| 91 | + env: |
| 92 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 93 | + run: git push |
| 94 | + |
| 95 | + # Step 12: Clone the website repository and push the updated protobuf.md |
| 96 | + - name: Generate random number for branch name |
| 97 | + id: random |
| 98 | + run: echo "::set-output name=random_number::$(shuf -i 1000-9999 -n 1)" |
| 99 | + - name: Clone the CLSFramework.github.io repository |
| 100 | + run: | |
| 101 | + git clone https://github.com/CLSFramework/CLSFramework.github.io.git cls-repo |
| 102 | + cd cls-repo |
| 103 | + |
| 104 | + # Copy updated README to target directory in the CLSFramework.github.io repository |
| 105 | + cp ../idl/readme.md docs/3-idl/protobuf.md |
| 106 | +
|
| 107 | + # Create a new branch with a random number appended |
| 108 | + git checkout -b update-proto-doc-${{ steps.random.outputs.random_number }} |
| 109 | + - name: Set up authentication using PAT for CLSFramework.github.io |
| 110 | + run: | |
| 111 | + cd cls-repo |
| 112 | + git remote set-url origin https://x-access-token:${{ secrets.WEBSITE_TOKEN }}@github.com/CLSFramework/CLSFramework.github.io.git |
| 113 | + - name: Commit and Push Changes to CLSFramework.github.io |
| 114 | + run: | |
| 115 | + cd cls-repo |
| 116 | + if git diff | grep 'protobuf.md'; then |
| 117 | + echo "protobuf.md has changed" |
| 118 | + else |
| 119 | + echo "protobuf.md has not changed" && exit 0 |
| 120 | + fi |
| 121 | + git add docs/3-idl/protobuf.md |
| 122 | + git commit -m "Update proto documentation" |
| 123 | + git push origin update-proto-doc-${{ steps.random.outputs.random_number }} |
| 124 | + - name: Create Pull Request in CLSFramework.github.io using GitHub API |
| 125 | + run: | |
| 126 | + PR_RESPONSE=$(curl -X POST -H "Authorization: token ${{ secrets.WEBSITE_TOKEN }}" \ |
| 127 | + -H "Accept: application/vnd.github.v3+json" \ |
| 128 | + https://api.github.com/repos/CLSFramework/CLSFramework.github.io/pulls \ |
| 129 | + -d '{"title":"Update proto documentation","head":"update-proto-doc-${{ steps.random.outputs.random_number }}","base":"main","body":"This PR updates the proto documentation based on changes made in grpc file."}') |
| 130 | + echo "Pull request created: $PR_RESPONSE" |
| 131 | + |
0 commit comments