|
| 1 | +name: Build and Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + create: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - master |
| 8 | + - main |
| 9 | + - develop |
| 10 | + - dev |
| 11 | + - feature/* |
| 12 | + - feat/* |
| 13 | + - release/* |
| 14 | + - hotfix/* |
| 15 | + paths: |
| 16 | + - '**.js' |
| 17 | + - '**.jsx' |
| 18 | + - '**.ts' |
| 19 | + - '**.tsx' |
| 20 | + - '**.json' |
| 21 | + release: |
| 22 | + types: [published] |
| 23 | + |
| 24 | +jobs: |
| 25 | + setup: |
| 26 | + if: github.run_number == 1 && github.event_name == 'create' |
| 27 | + runs-on: ubuntu-latest |
| 28 | + steps: |
| 29 | + - uses: actions/checkout@v3 |
| 30 | + |
| 31 | + - name: Initialize |
| 32 | + run: | |
| 33 | + tmp=$(mktemp) |
| 34 | + jq --arg author "${{ github.repository_owner }}" '.author = $author' package.json > $tmp |
| 35 | + jq --arg url "git+${{ github.event.repository.clone_url }}" '.repository.url = $url' $tmp > package.json |
| 36 | + jq --arg name "${{ github.event.repository.name }}" '.name = $name' package.json > $tmp |
| 37 | + jq --arg description "${{ github.event.description }}" '.description = $description' $tmp > package.json |
| 38 | + jq --arg homepage "${{ github.event.repository.html_url }}#readme" '.homepage = $homepage' package.json > $tmp |
| 39 | + jq --arg bugs "${{ github.event.repository.html_url }}/issues" '.bugs.url = $bugs' $tmp > package.json |
| 40 | + echo "# ${{ github.event.repository.name }}" > README.md |
| 41 | + rm -rf LICENSE |
| 42 | +
|
| 43 | + - uses: stefanzweifel/git-auto-commit-action@v4 |
| 44 | + with: |
| 45 | + commit_message: "Setup project for ${{ github.event.repository.name }} [skip ci]" |
| 46 | + |
| 47 | + ci: |
| 48 | + if: github.run_number != 1 && github.event_name != 'create' && !contains(github.event.head_commit.message, '[skip ci]') |
| 49 | + runs-on: ubuntu-latest |
| 50 | + steps: |
| 51 | + - uses: actions/checkout@v3 |
| 52 | + |
| 53 | + # extract `engines.node` from package.json and save it to output |
| 54 | + - name: Get Node.JS version from package.json |
| 55 | + id: get-versions |
| 56 | + run: | |
| 57 | + echo node=$(jq -r '.engines.node // "lts/*"' ./package.json) >> $GITHUB_OUTPUT |
| 58 | + echo pkg=$(jq -r '(.engines | select(.pnpm != null) | "pnpm") // (.engines | select(.yarn != null) | "yarn") // "npm"' ./package.json) >> $GITHUB_OUTPUT |
| 59 | +
|
| 60 | + - name: Setup Node.JS |
| 61 | + uses: actions/setup-node@v3 |
| 62 | + with: |
| 63 | + node-version: ${{steps.get-versions.outputs.node}} |
| 64 | + |
| 65 | + - name: Setup pnpm |
| 66 | + if: steps.get-versions.outputs.pkg == 'pnpm' |
| 67 | + uses: pnpm/action-setup@v2 |
| 68 | + with: |
| 69 | + version: latest |
| 70 | + |
| 71 | + - name: Install dependencies |
| 72 | + run: ${{steps.get-versions.outputs.pkg}} install |
| 73 | + |
| 74 | + - name: Test |
| 75 | + run: ${{steps.get-versions.outputs.pkg}} run test |
| 76 | + |
| 77 | + - name: Build |
| 78 | + id: build |
| 79 | + env: |
| 80 | + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 81 | + if: github.event_name == 'release' && env.NPM_TOKEN != null |
| 82 | + run: | |
| 83 | + ${{steps.get-versions.outputs.pkg}} run build |
| 84 | + tmp=$(mktemp) |
| 85 | + jq --arg version "${{ github.event.release.tag_name }}" '.version = $version' package.json > $tmp |
| 86 | + mv $tmp package.json |
| 87 | + echo version=$(jq -r '.version' package.json) >> $GITHUB_OUTPUT |
| 88 | + |
| 89 | + - name: Publish |
| 90 | + uses: JS-DevTools/npm-publish@v1 |
| 91 | + if: steps.build.outputs.version != null |
| 92 | + with: |
| 93 | + token: ${{ secrets.NPM_TOKEN }} |
0 commit comments