Init New Repo with Template #3
This file contains hidden or 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: Init New Repo with Template | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| repo_name: | |
| description: "Name of the new GitHub repo" | |
| required: true | |
| brief_description: | |
| description: "Short description of the repository" | |
| required: true | |
| select_template: | |
| description: "Select template" | |
| type: choice | |
| options: | |
| - node | |
| select_category_topic: | |
| description: "Select category topic" | |
| type: choice | |
| options: | |
| - specifications | |
| - reference-apps | |
| - automation-testing | |
| - mock-sandbox | |
| - adaptors | |
| - sdks | |
| - infrastructure | |
| - documentation | |
| - logs | |
| select_subcategory_topic: | |
| description: "Select subcategory topic" | |
| type: choice | |
| options: | |
| - buyer-apps | |
| - seller-apps | |
| - logistics-apps | |
| - ecommerce | |
| - core-framework | |
| - services | |
| - mock-servers | |
| select_domain_topic: | |
| description: "Select domain topic" | |
| type: choice | |
| options: | |
| - retail | |
| - logistics | |
| - agriculture | |
| - healthcare | |
| - financial | |
| - mobility | |
| - education | |
| select_type_topic: | |
| description: "Select type topic" | |
| type: choice | |
| options: | |
| - frontend | |
| - backend | |
| - mobile | |
| - api | |
| - library | |
| - tool | |
| - documentation | |
| jobs: | |
| create-repo: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Set Organization Variable | |
| run: echo "ORG=nmonga26" >> $GITHUB_ENV | |
| - name: Check if organization exists and create repo | |
| run: | | |
| ORG="${ORG}" | |
| REPO="${{ github.event.inputs.repo_name }}" | |
| echo "Checking org..." | |
| ORG_CHECK=$(curl -s -o /dev/null -w "%{http_code}" \ | |
| -H "Authorization: token ${{ secrets.PAT_TOKEN }}" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| https://api.github.com/orgs/$ORG) | |
| if [ "$ORG_CHECK" -ne 200 ]; then | |
| echo " Org '$ORG' not found or no access." | |
| exit 1 | |
| fi | |
| echo " Org exists." | |
| echo "Checking repo..." | |
| REPO_CHECK=$(curl -s -o /dev/null -w "%{http_code}" \ | |
| -H "Authorization: token ${{ secrets.PAT_TOKEN }}" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| https://api.github.com/repos/$ORG/$REPO) | |
| if [ "$REPO_CHECK" -eq 200 ]; then | |
| echo " Repo '$REPO' already exists." | |
| exit 1 | |
| fi | |
| DESC="${REPO}: [${{ github.event.inputs.select_category_topic }}/${{ github.event.inputs.select_subcategory_topic }}] ${{ github.event.inputs.brief_description }} | domain: ${{ github.event.inputs.select_domain_topic }}" | |
| CREATE_RESP=$(curl -s -o /dev/null -w "%{http_code}" -X POST \ | |
| -H "Authorization: token ${{ secrets.PAT_TOKEN }}" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| https://api.github.com/orgs/$ORG/repos \ | |
| -d "{\"name\":\"$REPO\",\"private\":false,\"description\":\"$DESC\"}") | |
| if [ "$CREATE_RESP" -eq 201 ]; then | |
| echo " Repo created: $ORG/$REPO" | |
| else | |
| echo " Failed with status $CREATE_RESP" | |
| exit 1 | |
| fi | |
| - name: Add Topics to Repository | |
| run: | | |
| ORG="${ORG}" | |
| REPO="${{ github.event.inputs.repo_name }}" | |
| TOPICS="[\"category-${{ github.event.inputs.select_category_topic }}\",\"subcategory-${{ github.event.inputs.select_subcategory_topic }}\",\"type-${{ github.event.inputs.select_type_topic }}\",\"domain-${{ github.event.inputs.select_domain_topic }}\"]" | |
| curl -X PUT \ | |
| -H "Authorization: token ${{ secrets.PAT_TOKEN }}" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| https://api.github.com/repos/$ORG/$REPO/topics \ | |
| -d "{\"names\": $TOPICS}" | |
| echo " Topics added: $TOPICS" | |
| - name: Install Yeoman | |
| run: npm install -g yo | |
| - name: Clone Template Repo | |
| run: | | |
| ORG="${ORG}" | |
| git clone https://x-access-token:${{ secrets.PAT_TOKEN }}@github.com/$ORG/node-template.git template-repo | |
| - name: Prepare Yeoman Generator | |
| run: | | |
| mkdir -p generator-ondc-nodets/generators/app/templates | |
| cp -r template-repo/* generator-ondc-nodets/generators/app/templates/ | |
| cp -r template-repo/.[!.]* generator-ondc-nodets/generators/app/templates/ || true | |
| # package.json for generator | |
| cat > generator-ondc-nodets/package.json <<'EOF' | |
| { | |
| "name": "generator-ondc-nodets", | |
| "version": "1.0.0", | |
| "description": "Yeoman generator wrapping ONDC Node TS template", | |
| "main": "generators/app/index.js", | |
| "dependencies": { | |
| "yeoman-generator": "^5.9.0" | |
| } | |
| } | |
| EOF | |
| # index.js for generator | |
| mkdir -p generator-ondc-nodets/generators/app | |
| cat > generator-ondc-nodets/generators/app/index.js <<'EOF' | |
| const Generator = require('yeoman-generator'); | |
| module.exports = class extends Generator { | |
| writing() { | |
| this.fs.copyTpl( | |
| this.templatePath('**/*'), | |
| this.destinationPath('.'), | |
| {}, | |
| {}, | |
| { globOptions: { dot: true } } | |
| ); | |
| } | |
| } | |
| EOF | |
| cd generator-ondc-nodets | |
| npm install | |
| npm link | |
| - name: Generate Project with Yeoman | |
| run: | | |
| mkdir -p ${{ github.event.inputs.repo_name }} | |
| cd ${{ github.event.inputs.repo_name }} | |
| yo ondc-nodets --skip-install --force --no-insight | |
| - name: Update Project Files with Repo Name | |
| run: | | |
| cd ${{ github.event.inputs.repo_name }} | |
| REPO_NAME="${{ github.event.inputs.repo_name }}" | |
| sed -i "s/test55-node-backend/$REPO_NAME/g" package.json || true | |
| sed -i "s/test55-node-backend/$REPO_NAME/g" package-lock.json || true | |
| sed -i "s/test55-node-backend/$REPO_NAME/g" README.md || true | |
| sed -i "s/test55-node-backend/$REPO_NAME/g" docker-compose.yml || true | |
| sed -i "s/test55-node-backend/$REPO_NAME/g" docker-compose.production.yml || true | |
| - name: Push Initial Code | |
| run: | | |
| cd ${{ github.event.inputs.repo_name }} | |
| rm -rf .git | |
| git init | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git branch -M main | |
| git remote add origin https://x-access-token:${{ secrets.PAT_TOKEN }}@github.com/${ORG}/${{ github.event.inputs.repo_name }}.git | |
| git add . | |
| git commit -m "[skip ci]" | |
| git push origin main | |
| - name: Setup Husky and Commit Hooks | |
| run: | | |
| cd ${{ github.event.inputs.repo_name }} | |
| npm install husky@9 --save-dev | |
| npx husky install | |
| # create commit-msg hook | |
| echo "npx commitlint --edit \$1" > .husky/commit-msg | |
| chmod +x .husky/commit-msg | |
| # create pre-commit hook | |
| echo "npx lint-staged" > .husky/pre-commit | |
| chmod +x .husky/pre-commit | |
| git add . | |
| git commit -m "[skip ci]" || true | |
| git push origin main | |
| - name: Create Develop Branch | |
| run: | | |
| cd ${{ github.event.inputs.repo_name }} | |
| git checkout -b develop | |
| git push origin develop |