|
| 1 | +# Building, testing, and publishing .NET releases |
| 2 | +# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net |
| 3 | + |
| 4 | +name: .NET CD |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + tags: |
| 9 | + - 'v*.*.*-*' |
| 10 | + |
| 11 | +env: |
| 12 | + DOTNET_VERSION: 8.0.x |
| 13 | + PACKAGE_NAME: nanotaboada/dotnet-samples-aspnetcore-webapi |
| 14 | + |
| 15 | +jobs: |
| 16 | + release: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: write |
| 20 | + packages: write |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Checkout repository |
| 24 | + uses: actions/checkout@v6.0.1 |
| 25 | + with: |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: Extract version from tag |
| 29 | + id: version |
| 30 | + run: | |
| 31 | + TAG_NAME=${GITHUB_REF#refs/tags/} |
| 32 | +
|
| 33 | + # Extract semver (e.g., v1.0.0-azteca -> 1.0.0) |
| 34 | + SEMVER=$(echo $TAG_NAME | sed -E 's/^v([0-9]+\.[0-9]+\.[0-9]+)-.*/\1/') |
| 35 | +
|
| 36 | + # Extract stadium name (e.g., v1.0.0-azteca -> azteca) |
| 37 | + STADIUM=$(echo $TAG_NAME | sed -E 's/^v[0-9]+\.[0-9]+\.[0-9]+-//') |
| 38 | +
|
| 39 | + # Validate semver format (X.Y.Z) |
| 40 | + if ! echo "$SEMVER" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$'; then |
| 41 | + echo "❌ Error: Invalid semantic version '$SEMVER' extracted from tag '$TAG_NAME'" |
| 42 | + echo "Expected format: v{MAJOR}.{MINOR}.{PATCH}-{STADIUM} (e.g., v1.0.0-azteca)" |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | +
|
| 46 | + # Valid stadium names (A-Z from CHANGELOG.md) |
| 47 | + VALID_STADIUMS="azteca bernabeu centenario dusseldorf ekaterinburg frankfurt gelsenkirchen hardrock ibnbatouta johannesburg kazan lusail maracana nantes olympiastadion parcdesprinces qatar974 rosebowl sansiro toronto ullevi volgograd wembley xiamen yokohama zentralstadion" |
| 48 | +
|
| 49 | + # Validate stadium name against the list |
| 50 | + if [ -z "$STADIUM" ]; then |
| 51 | + echo "❌ Error: Stadium name is empty in tag '$TAG_NAME'" |
| 52 | + echo "Expected format: v{MAJOR}.{MINOR}.{PATCH}-{STADIUM} (e.g., v1.0.0-azteca)" |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | +
|
| 56 | + if ! echo "$VALID_STADIUMS" | grep -qw "$STADIUM"; then |
| 57 | + echo "❌ Error: Invalid stadium name '$STADIUM' in tag '$TAG_NAME'" |
| 58 | + echo "Valid stadiums (A-Z): $VALID_STADIUMS" |
| 59 | + echo "See CHANGELOG.md for the complete list" |
| 60 | + exit 1 |
| 61 | + fi |
| 62 | +
|
| 63 | + # Export validated outputs |
| 64 | + echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT |
| 65 | + echo "semver=$SEMVER" >> $GITHUB_OUTPUT |
| 66 | + echo "stadium=$STADIUM" >> $GITHUB_OUTPUT |
| 67 | +
|
| 68 | + echo "📦 Release version: $SEMVER" |
| 69 | + echo "🏟️ Stadium name: $STADIUM" |
| 70 | +
|
| 71 | + - name: Set up .NET ${{ env.DOTNET_VERSION }} |
| 72 | + uses: actions/setup-dotnet@v5.1.0 |
| 73 | + with: |
| 74 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 75 | + cache: true |
| 76 | + cache-dependency-path: | |
| 77 | + src/Dotnet.Samples.AspNetCore.WebApi/packages.lock.json |
| 78 | + test/Dotnet.Samples.AspNetCore.WebApi.Tests/packages.lock.json |
| 79 | +
|
| 80 | + - name: Restore dependencies |
| 81 | + run: dotnet restore |
| 82 | + |
| 83 | + - name: Build projects (Release configuration) |
| 84 | + run: dotnet build --configuration Release --no-restore |
| 85 | + |
| 86 | + - name: Run tests |
| 87 | + run: dotnet test --configuration Release --no-build --verbosity normal |
| 88 | + |
| 89 | + - name: Log in to GitHub Container Registry |
| 90 | + uses: docker/login-action@v3.6.0 |
| 91 | + with: |
| 92 | + registry: ghcr.io |
| 93 | + username: ${{ github.actor }} |
| 94 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 95 | + |
| 96 | + - name: Set up Docker Buildx |
| 97 | + uses: docker/setup-buildx-action@v3.12.0 |
| 98 | + |
| 99 | + - name: Build and push Docker image to GitHub Container Registry |
| 100 | + uses: docker/build-push-action@v6.18.0 |
| 101 | + with: |
| 102 | + context: . |
| 103 | + push: true |
| 104 | + platforms: linux/amd64 |
| 105 | + provenance: false |
| 106 | + cache-from: type=gha |
| 107 | + cache-to: type=gha,mode=max |
| 108 | + tags: | |
| 109 | + ghcr.io/${{ env.PACKAGE_NAME }}:latest |
| 110 | + ghcr.io/${{ env.PACKAGE_NAME }}:${{ steps.version.outputs.semver }} |
| 111 | + ghcr.io/${{ env.PACKAGE_NAME }}:${{ steps.version.outputs.stadium }} |
| 112 | +
|
| 113 | + - name: Generate changelog |
| 114 | + id: changelog |
| 115 | + run: | |
| 116 | + # Get previous tag |
| 117 | + PREVIOUS_TAG=$(git tag --sort=-version:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+-' | sed -n '2p') |
| 118 | +
|
| 119 | + if [ -z "$PREVIOUS_TAG" ]; then |
| 120 | + echo "📝 First release - no previous tag found" |
| 121 | + CHANGELOG="Initial release" |
| 122 | + else |
| 123 | + echo "📝 Generating changelog from $PREVIOUS_TAG to ${{ steps.version.outputs.tag_name }}" |
| 124 | + CHANGELOG=$(git log --pretty=format:"- %s (%h)" ${PREVIOUS_TAG}..${{ steps.version.outputs.tag_name }}) |
| 125 | + fi |
| 126 | +
|
| 127 | + # Write changelog to file |
| 128 | + echo "$CHANGELOG" > changelog.txt |
| 129 | + cat changelog.txt |
| 130 | +
|
| 131 | + # Set output for use in release body |
| 132 | + { |
| 133 | + echo "changelog<<EOF" |
| 134 | + echo "$CHANGELOG" |
| 135 | + echo "EOF" |
| 136 | + } >> $GITHUB_OUTPUT |
| 137 | +
|
| 138 | + - name: Create GitHub Release |
| 139 | + uses: softprops/action-gh-release@v2.2.0 |
| 140 | + with: |
| 141 | + name: "v${{ steps.version.outputs.semver }} - ${{ steps.version.outputs.stadium }}" |
| 142 | + tag_name: ${{ steps.version.outputs.tag_name }} |
| 143 | + body: | |
| 144 | + # 🏟️ Release ${{ steps.version.outputs.semver }} - ${{ steps.version.outputs.stadium }} |
| 145 | +
|
| 146 | + ## Docker Images |
| 147 | +
|
| 148 | + Pull this release using any of the following tags: |
| 149 | +
|
| 150 | + ```bash |
| 151 | + # By semantic version (recommended) |
| 152 | + docker pull ghcr.io/${{ env.PACKAGE_NAME }}:${{ steps.version.outputs.semver }} |
| 153 | +
|
| 154 | + # By stadium name |
| 155 | + docker pull ghcr.io/${{ env.PACKAGE_NAME }}:${{ steps.version.outputs.stadium }} |
| 156 | +
|
| 157 | + # Latest |
| 158 | + docker pull ghcr.io/${{ env.PACKAGE_NAME }}:latest |
| 159 | + ``` |
| 160 | +
|
| 161 | + --- |
| 162 | +
|
| 163 | + 📦 **Package:** [ghcr.io/${{ env.PACKAGE_NAME }}](https://github.com/${{ github.repository }}/pkgs/container/dotnet-samples-aspnetcore-webapi) |
| 164 | + draft: false |
| 165 | + prerelease: false |
| 166 | + generate_release_notes: true |
0 commit comments