Skip to content

Create new repository: NEWrepo-1 #68

Create new repository: NEWrepo-1

Create new repository: NEWrepo-1 #68

Workflow file for this run

name: Create Repository After PR Merge
on:
pull_request:
types: [closed]
jobs:
create_repo:
if: |
github.event.pull_request.merged == true &&
contains(github.event.pull_request.labels.*.name, 'repo-creation')
runs-on: ubuntu-latest
steps:
- name: Extract PR Details
id: extract
run: |
# Get PR body and extract details
PR_BODY="${{ github.event.pull_request.body }}"
REPO_NAME=$(echo "$PR_BODY" | grep -oP '(?<=- \*\*Name\*\*: ).*')
BRANCH=$(echo "$PR_BODY" | grep -oP '(?<=- \*\*Default Branch\*\*: ).*')
INCLUDE_README=$(echo "$PR_BODY" | grep -oP '(?<=- \*\*Include README\*\*: ).*')
VISIBILITY=$(echo "$PR_BODY" | grep -oP '(?<=- \*\*Visibility\*\*: ).*')
echo "REPO_NAME=$REPO_NAME" >> $GITHUB_ENV
echo "BRANCH=$BRANCH" >> $GITHUB_ENV
echo "INCLUDE_README=$INCLUDE_README" >> $GITHUB_ENV
echo "VISIBILITY=$VISIBILITY" >> $GITHUB_ENV
- name: Set Repository Visibility
id: visibility
run: |
# Translate visibility to private: true/false
if [[ "$VISIBILITY" == "public" ]]; then
echo "REPO_VISIBILITY=false" >> $GITHUB_ENV
elif [[ "$VISIBILITY" == "private" || "$VISIBILITY" == "internal" ]]; then
echo "REPO_VISIBILITY=true" >> $GITHUB_ENV
else
echo "Error: Invalid visibility option" && exit 1
fi
- name: Create Repository
uses: actions/github-script@v6
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
with:
github-token: ${{ secrets.GH_PAT }}
script: |
try {
await github.rest.repos.createInOrg({
org: context.repo.owner,
name: process.env.REPO_NAME,
private: process.env.REPO_VISIBILITY === 'true', // If true, it's private; if false, it's public
auto_init: true,
license_template: 'mit'
});
console.log(`Created repository: ${process.env.REPO_NAME}`);
} catch (error) {
core.setFailed(`Repository creation failed: ${error}`);
}
- name: Add README if requested
if: env.INCLUDE_README == 'Yes'
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
run: |
git clone https://${{ github.actor }}:${{ secrets.GH_PAT }}@github.com/${{ github.repository_owner }}/${{ env.REPO_NAME }}.git
cd ${{ env.REPO_NAME }}
echo "# ${{ env.REPO_NAME }}" > README.md
git add README.md
git config user.name "github-actions"
git config user.email "actions@github.com"
git commit -m "Add initial README"
git push