Initialize repository #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Initialize repository | |
on: | |
workflow_dispatch: | |
jobs: | |
initialize_repo: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
with: | |
ref: main | |
fetch-depth: 0 | |
- name: Setup git | |
run: | | |
git config --global user.name "github-actions" | |
git config --global user.email "[email protected]" | |
- name: Install GitHub CLI | |
run: | | |
sudo apt-get update | |
sudo apt-get install gh -y | |
- name: Extract repository and username | |
id: extract | |
run: | | |
REPO_NAME="${{ github.repository }}" | |
USERNAME=$(echo $REPO_NAME | cut -d'/' -f1) | |
REPO_NAME_ONLY=$(echo $REPO_NAME | cut -d'/' -f2) | |
echo "::set-output name=username::$USERNAME" | |
echo "::set-output name=reponame::$REPO_NAME_ONLY" | |
- name: Set branch name | |
id: vars | |
run: echo "::set-output name=branch::initialize-repo-$(date +%Y%m%d%H%M%S)" | |
- name: Replace {reponame} and {username} with actual values | |
run: | | |
REPO_NAME_ONLY="${{ steps.extract.outputs.reponame }}" | |
USERNAME="${{ steps.extract.outputs.username }}" | |
REPO_NAME_ESCAPED=$(echo $REPO_NAME_ONLY | sed 's/\//\\\//g') | |
USERNAME_ESCAPED=$(echo $USERNAME | sed 's/\//\\\//g') | |
find . -type f -exec sed -i "s/{reponame}/$REPO_NAME_ESCAPED/g" {} + | |
find . -type f -exec sed -i "s/{username}/$USERNAME_ESCAPED/g" {} + | |
- name: Remove initializer workflow | |
run: | | |
rm -f .github/workflows/initializer.yml | |
- name: Commit changes | |
run: | | |
BRANCH_NAME="${{ steps.vars.outputs.branch }}" | |
git checkout -b $BRANCH_NAME | |
git add . | |
git commit -m "Initialize repository with repo name $REPO_NAME_ONLY and username $USERNAME" | |
git push origin HEAD:$BRANCH_NAME | |
- name: Create pull request | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
BRANCH_NAME="${{ steps.vars.outputs.branch }}" | |
PR_URL=$(gh pr create --base main --head $BRANCH_NAME --title "Initialize repository" --body "This PR initializes the repository with the actual repository name, with the actual username and removing the initializer workflow.") | |
# Extract PR number from URL | |
PR_NUMBER=$(basename $PR_URL) | |
# Add any desired labels to the pull request | |
# gh pr edit $PR_NUMBER --add-label "initialization" |