| 1 | # GitBook external customer documentation: This action creates a branch for each release, which can optionally be used in GitBook to maintain service-versioned documentation streamlined with service-releases. |
| 2 | # If an existing release is deleted and re-released, the original release-branch is deleted and recreated with the re-release. |
| 3 | # Simply deleting a release without re-releasing does not delete the release-branch, as it could already be used by GitBook. |
| 4 | |
| 5 | name: Create Release Branch for GitBook |
| 6 | |
| 7 | on: |
| 8 | release: |
| 9 | types: |
| 10 | - created |
| 11 | |
| 12 | jobs: |
| 13 | create-release-branch: |
| 14 | runs-on: ubuntu-latest |
| 15 | |
| 16 | steps: |
| 17 | - name: Check out the repository |
| 18 | uses: actions/checkout@v3 |
| 19 | |
| 20 | - name: Set up Git |
| 21 | run: | |
| 22 | git config user.name "${{ github.actor }}" |
| 23 | git config user.email "${{ github.actor }}@users.noreply.github.com" |
| 24 | |
| 25 | - name: Check if branch already exists |
| 26 | id: check-branch |
| 27 | run: | |
| 28 | # Normalize the release tag to ensure it is a valid branch name |
| 29 | SAFE_TAG_NAME=$(echo "${{ github.event.release.tag_name }}" | sed 's/[^a-zA-Z0-9._-]/-/g') |
| 30 | BRANCH_NAME="release/$SAFE_TAG_NAME" |
| 31 | |
| 32 | if git ls-remote --heads origin $BRANCH_NAME | grep -q $BRANCH_NAME; then |
| 33 | echo "Branch $BRANCH_NAME already exists." |
| 34 | echo "branch-exists=true" >> $GITHUB_ENV |
| 35 | else |
| 36 | echo "Branch $BRANCH_NAME does not exist." |
| 37 | echo "branch-exists=false" >> $GITHUB_ENV |
| 38 | fi |
| 39 | |
| 40 | - name: Delete existing branch if it exists (re-release?) |
| 41 | if: env.branch-exists == 'true' |
| 42 | run: | |
| 43 | # Normalize the release tag to ensure it is a valid branch name |
| 44 | SAFE_TAG_NAME=$(echo "${{ github.event.release.tag_name }}" | sed 's/[^a-zA-Z0-9._-]/-/g') |
| 45 | BRANCH_NAME="release/$SAFE_TAG_NAME" |
| 46 | |
| 47 | echo "Deleting existing branch $BRANCH_NAME." |
| 48 | git push origin --delete $BRANCH_NAME || echo "Branch $BRANCH_NAME could not be deleted (might not exist on remote)." |
| 49 | |
| 50 | - name: Create and push the new branch |
| 51 | run: | |
| 52 | # Normalize the release tag to ensure it is a valid branch name |
| 53 | SAFE_TAG_NAME=$(echo "${{ github.event.release.tag_name }}" | sed 's/[^a-zA-Z0-9._-]/-/g') |
| 54 | BRANCH_NAME="release/$SAFE_TAG_NAME" |
| 55 | echo "Creating branch $BRANCH_NAME" |
| 56 | |
| 57 | git branch $BRANCH_NAME |
| 58 | git push origin $BRANCH_NAME |