|
| 1 | +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json |
| 2 | + |
| 3 | +name: publish |
| 4 | +on: |
| 5 | + workflow_dispatch: # Allow running the workflow manually from the GitHub UI |
| 6 | + push: |
| 7 | + branches: |
| 8 | + - 'main' # Run the workflow when pushing to the main branch (test only) |
| 9 | + tags: |
| 10 | + - '*' # Run the workflow with a tag |
| 11 | + |
| 12 | +env: |
| 13 | + DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 |
| 14 | + DOTNET_NOLOGO: true |
| 15 | + NuGetDirectory: ${{ github.workspace }}/nuget |
| 16 | + |
| 17 | +defaults: |
| 18 | + run: |
| 19 | + shell: pwsh |
| 20 | + |
| 21 | +jobs: |
| 22 | + set_version: |
| 23 | + if: startsWith(github.ref, 'refs/tags/') |
| 24 | + runs-on: ubuntu-latest |
| 25 | + steps: |
| 26 | + - name: Checkout |
| 27 | + uses: actions/checkout@v4 |
| 28 | + with: |
| 29 | + fetch-depth: 0 |
| 30 | + |
| 31 | + - name: Set Version |
| 32 | + id: package_version |
| 33 | + uses: KageKirin/set-csproj-version@v0 |
| 34 | + with: |
| 35 | + file: ./ModShardUnpacker.csproj |
| 36 | + version: ${{ github.ref_name }} |
| 37 | + |
| 38 | + - name: Set user info |
| 39 | + run: | |
| 40 | + git config user.name github-actions |
| 41 | + git config user.email [email protected] |
| 42 | +
|
| 43 | + - name: Commit Files & Pull |
| 44 | + run: | |
| 45 | + git commit -a -m "Updated version with CI." |
| 46 | + git pull origin main --rebase |
| 47 | +
|
| 48 | + - name: Push Changes |
| 49 | + uses: ad-m/github-push-action@master |
| 50 | + with: |
| 51 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 52 | + branch: main |
| 53 | + |
| 54 | + create_nuget: |
| 55 | + runs-on: ubuntu-latest |
| 56 | + needs: [ set_version ] |
| 57 | + steps: |
| 58 | + - uses: actions/checkout@v4 |
| 59 | + with: |
| 60 | + fetch-depth: 0 # Get all history to allow automatic versioning using MinVer |
| 61 | + |
| 62 | + # Install the .NET SDK indicated in the global.json file |
| 63 | + - name: Setup .NET |
| 64 | + uses: actions/setup-dotnet@v4 |
| 65 | + |
| 66 | + # Build the project |
| 67 | + - run: dotnet build --configuration Release |
| 68 | + |
| 69 | + # Create the NuGet package in the folder from the environment variable NuGetDirectory |
| 70 | + - run: dotnet pack --configuration Release --output ${{ env.NuGetDirectory }} |
| 71 | + |
| 72 | + # Publish the NuGet package as an artifact, so they can be used in the following jobs |
| 73 | + - uses: actions/upload-artifact@v3 |
| 74 | + with: |
| 75 | + name: nuget |
| 76 | + if-no-files-found: error |
| 77 | + retention-days: 7 |
| 78 | + path: ${{ env.NuGetDirectory }}/*.nupkg |
| 79 | + |
| 80 | + validate_nuget: |
| 81 | + runs-on: ubuntu-latest |
| 82 | + needs: [ create_nuget ] |
| 83 | + steps: |
| 84 | + # Install the .NET SDK indicated in the global.json file |
| 85 | + - name: Setup .NET |
| 86 | + uses: actions/setup-dotnet@v4 |
| 87 | + |
| 88 | + # Download the NuGet package created in the previous job |
| 89 | + - uses: actions/download-artifact@v3 |
| 90 | + with: |
| 91 | + name: nuget |
| 92 | + path: ${{ env.NuGetDirectory }} |
| 93 | + |
| 94 | + - name: Install nuget validator |
| 95 | + run: dotnet tool update Meziantou.Framework.NuGetPackageValidation.Tool --global |
| 96 | + |
| 97 | + # Validate metadata and content of the NuGet package |
| 98 | + # https://www.nuget.org/packages/Meziantou.Framework.NuGetPackageValidation.Tool#readme-body-tab |
| 99 | + # If some rules are not applicable, you can disable them |
| 100 | + # using the --excluded-rules or --excluded-rule-ids option |
| 101 | + - name: Validate package |
| 102 | + run: meziantou.validate-nuget-package (Get-ChildItem "${{ env.NuGetDirectory }}/*.nupkg") |
| 103 | + |
| 104 | + run_test: |
| 105 | + runs-on: ubuntu-latest |
| 106 | + steps: |
| 107 | + - uses: actions/checkout@v3 |
| 108 | + - name: Setup .NET |
| 109 | + uses: actions/setup-dotnet@v4 |
| 110 | + - name: Run tests |
| 111 | + run: dotnet test --configuration Release |
| 112 | + |
| 113 | + create_release: |
| 114 | + runs-on: ubuntu-latest |
| 115 | + needs: [ validate_nuget, run_test ] |
| 116 | + steps: |
| 117 | + - uses: actions/checkout@v4 |
| 118 | + |
| 119 | + - name: Create Release |
| 120 | + run: gh release create ${{ github.ref_name }} --generate-notes |
| 121 | + env: |
| 122 | + GITHUB_TOKEN: ${{ github.TOKEN }} |
| 123 | + |
| 124 | + deploy: |
| 125 | + # Publish only when creating a GitHub Release |
| 126 | + # https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository |
| 127 | + # You can update this logic if you want to manage releases differently |
| 128 | + runs-on: ubuntu-latest |
| 129 | + needs: [ create_release ] |
| 130 | + steps: |
| 131 | + # Download the NuGet package created in the previous job |
| 132 | + - uses: actions/download-artifact@v3 |
| 133 | + with: |
| 134 | + name: nuget |
| 135 | + path: ${{ env.NuGetDirectory }} |
| 136 | + |
| 137 | + # Install the .NET SDK indicated in the global.json file |
| 138 | + - name: Setup .NET Core |
| 139 | + uses: actions/setup-dotnet@v4 |
| 140 | + |
| 141 | + # Publish all NuGet packages to NuGet.org |
| 142 | + # Use --skip-duplicate to prevent errors if a package with the same version already exists. |
| 143 | + # If you retry a failed workflow, already published packages will be skipped without error. |
| 144 | + - name: Publish NuGet package |
| 145 | + run: | |
| 146 | + foreach($file in (Get-ChildItem "${{ env.NuGetDirectory }}" -Recurse -Include *.nupkg)) { |
| 147 | + dotnet nuget push $file --api-key "${{ secrets.NUGET_APIKEY }}" --source https://api.nuget.org/v3/index.json --skip-duplicate |
| 148 | + } |
0 commit comments