Generate MDK repository #114957
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: Generate MDK repository | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| repository: | |
| type: string | |
| description: The URL of the repository | |
| required: true | |
| minecraft_version: | |
| type: string | |
| description: The Minecraft version to generate the MDK for | |
| required: true | |
| gradle_plugin: | |
| type: string | |
| description: The Gradle plugin version to use for the MDK | |
| required: true | |
| branch: | |
| type: string | |
| description: The branch to push to | |
| required: true | |
| jobs: | |
| generate: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout latest version of the mod generator | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: neoforged/mod-generator | |
| persist-credentials: false | |
| - uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| - name: Download NPM dependencies | |
| run: npm ci | |
| - name: Generate MDK | |
| run: > | |
| npm run run -- generate | |
| --mod-name 'Example Mod' | |
| --mod-id examplemod | |
| --minecraft-version ${{ github.event.inputs.minecraft_version }} | |
| --gradle-plugin ${{ github.event.inputs.gradle_plugin }} | |
| --package-name com.example.examplemod | |
| --executable-gradlew no | |
| --output-folder out | |
| - name: Generate a token for downstream | |
| id: gen_downstream_token | |
| uses: kattecon/gh-app-access-token-gen@v1 | |
| with: | |
| app_id: 926135 | |
| private_key: ${{ secrets.MDKS_APPLICATION_KEY }} | |
| repository: ${{ github.event.inputs.repository }} | |
| - name: Update git credentials | |
| uses: OleksiyRudenko/[email protected] | |
| with: | |
| token: ${{ steps.gen_downstream_token.outputs.token }} | |
| global: true | |
| name: 'NeoForge MDK Automation' | |
| email: '173375039+neoforge-mdk-automation[bot]@users.noreply.github.com' | |
| actor: 'neoforge-mdk-automation[bot]' | |
| - name: Checkout downstream | |
| id: checkout-downstream | |
| uses: actions/checkout@v4 | |
| continue-on-error: true | |
| with: | |
| repository: ${{ github.event.inputs.repository }} | |
| path: downstream | |
| persist-credentials: false | |
| fetch-depth: 0 | |
| - name: Create downstream repo | |
| if: steps.checkout-downstream.outcome == 'failure' | |
| working-directory: ./downstream | |
| run: | | |
| git init | |
| git checkout -b main | |
| - name: Check empty | |
| id: check-empty | |
| uses: actions/github-script@v7 | |
| with: | |
| result-encoding: string | |
| script: | | |
| const { stdout } = await exec.getExecOutput('git', ['status'], {cwd: './downstream'}); | |
| const isEmpty = stdout.includes('No commits yet') | |
| console.log(`Is branch empty: ${isEmpty}`) | |
| return isEmpty | |
| - name: Switch to ${{ inputs.branch }} branch | |
| working-directory: ./downstream | |
| if: steps.check-empty.outputs.result == 'false' | |
| run: git switch ${{ inputs.branch }} || git switch -c ${{ inputs.branch }} | |
| - name: Remove tracked files from downstream repository | |
| working-directory: ./downstream | |
| run: git ls-files -z | xargs -0 rm -f | |
| - name: Copy generated files over | |
| run: cp -rT ./out ./downstream | |
| # Making gradlew executable is needed such that ./gradlew will work on unix systems. | |
| - name: Add changed files and make gradlew executable | |
| working-directory: ./downstream | |
| run: | | |
| chmod +x ./gradlew | |
| git add . | |
| - name: Check status | |
| id: check-status | |
| uses: actions/github-script@v7 | |
| with: | |
| result-encoding: string | |
| script: | | |
| const { stdout } = await exec.getExecOutput('git', ['status'], {cwd: './downstream'}); | |
| const shouldCommit = !stdout.includes('nothing to commit, working tree clean') | |
| console.log(`Should commit: ${shouldCommit}`) | |
| return shouldCommit | |
| - name: Commit and push | |
| if: steps.check-status.outputs.result == 'true' | |
| working-directory: ./downstream | |
| run: | | |
| git commit -m "Update MDK with new output from mod generator" | |
| git remote remove origin | |
| git remote add downstream https://neoforge-mdk-automation[bot]:${{ steps.gen_downstream_token.outputs.token }}@github.com/${{ github.event.inputs.repository }}.git | |
| git push -u downstream ${{ steps.check-empty.outputs.result == 'true' && 'main' || inputs.branch }} | |
| - name: Create PR | |
| if: steps.check-status.outputs.result == 'true' && steps.check-empty.outputs.result == 'false' | |
| uses: actions/github-script@v7 | |
| env: | |
| REPO: ${{ inputs.repository }} | |
| BRANCH: ${{ inputs.branch }} | |
| with: | |
| github-token: ${{ steps.gen_downstream_token.outputs.token }} | |
| script: | | |
| const repoPath = process.env.REPO.split('/') | |
| const repo = { owner: repoPath[0], repo: repoPath[1] } | |
| const prs = await github.rest.pulls.list({ | |
| ...repo, | |
| head: process.env.BRANCH, | |
| state: 'open' | |
| }).then(d => d.data) | |
| if (prs.length === 0) { | |
| await github.rest.pulls.create({ | |
| ...repo, | |
| head: process.env.BRANCH, | |
| base: 'main', | |
| title: 'Update MDK with new output from mod generator' | |
| }) | |
| } |