44 pull_request :
55 paths :
66 - .github/workflows/setting.yml
7+ - .github/environments.json
78 - .github/protection.json
89 schedule :
910 - cron : " 0 0 * * *"
2324
2425 - name : Enable auto-delete head branches
2526 run : |
26- gh repo edit ${{ github.repository }} --delete-branch-on-merge
27+ gh repo edit ${{ github.repository }} \
28+ --default-branch develop \
29+ --delete-branch-on-merge
2730 env :
2831 GH_TOKEN : ${{ steps.generate-token.outputs.token }}
2932
@@ -40,14 +43,15 @@ jobs:
4043
4144 - name : Set GitHub Pages Source
4245 run : |
43- gh api -X POST "repos/ ${{ github.repository }}/pages" \
46+ gh api -X POST ${{ env.ENDPOINT }} \
4447 -f "source[branch]=${{ env.BRANCH }}" \
4548 -f "source[path]=${{ env.TARGET_PATH }}" --silent \
4649 || \
47- gh api -X PUT "repos/ ${{ github.repository }}/pages" \
50+ gh api -X PUT ${{ env.ENDPOINT }} \
4851 -f "source[branch]=${{ env.BRANCH }}" \
4952 -f "source[path]=${{ env.TARGET_PATH }}"
5053 env :
54+ ENDPOINT : repos/${{ github.repository }}/pages
5155 BRANCH : gh-pages
5256 TARGET_PATH : /
5357 GH_TOKEN : ${{ steps.generate-token.outputs.token }}
@@ -68,21 +72,75 @@ jobs:
6872
6973 - name : Apply Branch Protection Rules
7074 run : |
71- if [ ! -f "$ CONFIG_FILE" ]; then
72- echo "Error: $CONFIG_FILE not found!"
75+ if [ ! -f ${{ env. CONFIG_FILE }} ]; then
76+ echo "Error: ${{ env. CONFIG_FILE }} not found!"
7377 exit 1
7478 fi
7579
76- BRANCHES=$(jq -r 'keys[]' "$ CONFIG_FILE" )
80+ BRANCHES=$(jq -r 'keys[]' ${{ env. CONFIG_FILE }} )
7781
7882 for BRANCH in $BRANCHES; do
79- if ! gh api "repos/ ${{ github.repository }}/branches /$BRANCH" --silent >/dev/null 2>&1; then
80- echo "Warning: Branch ' $BRANCH' does not exist in this repository. Skipping..."
83+ if ! gh api "${{ env.ENDPOINT }}/$BRANCH" --silent >/dev/null 2>&1; then
84+ echo "Warning: Branch $BRANCH does not exist in this repository. Skipping..."
8185 continue
8286 fi
8387
84- jq -c ".\"$BRANCH\"" "$ CONFIG_FILE" | gh api -X PUT "repos/ ${{ github.repository }}/branches /$BRANCH/protection" --input -
88+ jq -c ".\"$BRANCH\"" ${{ env. CONFIG_FILE }} | gh api -X PUT "${{ env.ENDPOINT }}/$BRANCH/protection" --input -
8589 done
8690 env :
8791 CONFIG_FILE : .github/protection.json
92+ ENDPOINT : repos/${{ github.repository }}/branches
93+ GH_TOKEN : ${{ steps.generate-token.outputs.token }}
94+
95+ environments :
96+ runs-on : ubuntu-latest
97+
98+ strategy :
99+ matrix :
100+ include :
101+ - environment : Develop
102+ branch : develop
103+ - environment : Production
104+ branch : main
105+ - environment : github-pages
106+ branch : gh-pages
107+
108+ steps :
109+ - name : Checkout
110+ uses : actions/checkout@v6
111+
112+ - name : Generate a token
113+ id : generate-token
114+ uses : actions/create-github-app-token@v2
115+ with :
116+ app-id : ${{ vars.APP_ID }}
117+ private-key : ${{ secrets.APP_KEY }}
118+
119+ - name : Configure Environment
120+ run : |
121+ if [ ! -f ${{ env.CONFIG_FILE }} ]; then
122+ echo "Error: ${{ env.CONFIG_FILE }} not found!"
123+ exit 1
124+ fi
125+
126+ jq -c ".\"${{ env.ENVIRONMENT_NAME }}\"" ${{ env.CONFIG_FILE }} | gh api -X PUT "${{ env.ENDPOINT }}/${{ env.ENVIRONMENT_NAME }}" --input -
127+
128+ CUSTOM_BRANCH_POLICIES=$(jq -r ".\"${{ env.ENVIRONMENT_NAME }}\".deployment_branch_policy.custom_branch_policies" ${{ env.CONFIG_FILE }})
129+
130+ if [ "$CUSTOM_BRANCH_POLICIES" != true ]; then
131+ IDS=$(gh api "${{ env.ENDPOINT }}/${{ env.ENVIRONMENT_NAME }}/deployment-branch-policies" --jq '.branch_policies[].id' || true)
132+ for ID in $IDS; do
133+ gh api -X DELETE "${{ env.ENDPOINT }}/${{ env.ENVIRONMENT_NAME }}/deployment-branch-policies/$ID" --silent || true
134+ done
135+ exit 0
136+ fi
137+
138+ gh api -X POST "${{ env.ENDPOINT }}/${{ env.ENVIRONMENT_NAME }}/deployment-branch-policies" \
139+ -f "name=${{ env.BRANCH_NAME }}" \
140+ -f "type=branch"
141+ env :
142+ CONFIG_FILE : .github/environments.json
143+ BRANCH_NAME : ${{ matrix.branch }}
144+ ENDPOINT : repos/${{ github.repository }}/environments
145+ ENVIRONMENT_NAME : ${{ matrix.environment }}
88146 GH_TOKEN : ${{ steps.generate-token.outputs.token }}
0 commit comments