|
| 1 | +# Building, testing, and publishing Python releases |
| 2 | +# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python |
| 3 | + |
| 4 | +name: Python CD |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + tags: |
| 9 | + - 'v*.*.*-*' |
| 10 | + |
| 11 | +env: |
| 12 | + PYTHON_VERSION_FILE: '.python-version' |
| 13 | + PACKAGE_NAME: nanotaboada/python-samples-fastapi-restful |
| 14 | + |
| 15 | +jobs: |
| 16 | + release: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: write |
| 20 | + packages: write |
| 21 | + steps: |
| 22 | + - name: Checkout repository |
| 23 | + uses: actions/checkout@v6.0.1 |
| 24 | + with: |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Extract version from tag |
| 28 | + id: version |
| 29 | + run: | |
| 30 | + TAG_NAME=${GITHUB_REF#refs/tags/} |
| 31 | + # Extract semver (e.g., v1.0.0-ancelotti -> 1.0.0) |
| 32 | + SEMVER=$(echo $TAG_NAME | sed -E 's/^v([0-9]+\.[0-9]+\.[0-9]+)-.*/\1/') |
| 33 | + # Extract coach name (e.g., v1.0.0-ancelotti -> ancelotti) |
| 34 | + COACH=$(echo $TAG_NAME | sed -E 's/^v[0-9]+\.[0-9]+\.[0-9]+-//') |
| 35 | +
|
| 36 | + # Validate semver format (X.Y.Z) |
| 37 | + if ! echo "$SEMVER" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$'; then |
| 38 | + echo "❌ Error: Invalid semantic version '$SEMVER' extracted from tag '$TAG_NAME'" |
| 39 | + echo "Expected format: v{MAJOR}.{MINOR}.{PATCH}-{COACH} (e.g., v1.0.0-ancelotti)" |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | +
|
| 43 | + # Valid coach names (A-Z from CHANGELOG.md) |
| 44 | + VALID_COACHES="ancelotti bielsa capello delbosque eriksson ferguson guardiola heynckes inzaghi klopp kovac low mourinho nagelsmann ottmar pochettino queiroz ranieri simeone tuchel unai vangaal wenger xavi yozhef zeman" |
| 45 | +
|
| 46 | + # Validate coach name against the list |
| 47 | + if [ -z "$COACH" ]; then |
| 48 | + echo "❌ Error: Coach name is empty in tag '$TAG_NAME'" |
| 49 | + echo "Expected format: v{MAJOR}.{MINOR}.{PATCH}-{COACH} (e.g., v1.0.0-ancelotti)" |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | +
|
| 53 | + if ! echo "$VALID_COACHES" | grep -qw "$COACH"; then |
| 54 | + echo "❌ Error: Invalid coach name '$COACH' in tag '$TAG_NAME'" |
| 55 | + echo "Valid coaches (A-Z): $VALID_COACHES" |
| 56 | + echo "See CHANGELOG.md for the complete list" |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | +
|
| 60 | + # Export validated outputs |
| 61 | + echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT |
| 62 | + echo "semver=$SEMVER" >> $GITHUB_OUTPUT |
| 63 | + echo "coach=$COACH" >> $GITHUB_OUTPUT |
| 64 | +
|
| 65 | + echo "📦 Release version: $SEMVER" |
| 66 | + echo "♟️ Coach name: $COACH" |
| 67 | +
|
| 68 | + - name: Set up Python |
| 69 | + uses: actions/setup-python@v6.1.0 |
| 70 | + with: |
| 71 | + python-version-file: ${{ env.PYTHON_VERSION_FILE }} |
| 72 | + cache: 'pip' |
| 73 | + |
| 74 | + - name: Install test dependencies |
| 75 | + run: | |
| 76 | + python -m pip install --upgrade pip |
| 77 | + pip install -r requirements-test.txt |
| 78 | +
|
| 79 | + - name: Run tests with pytest |
| 80 | + run: | |
| 81 | + pytest -v |
| 82 | +
|
| 83 | + - name: Generate coverage report |
| 84 | + run: | |
| 85 | + pytest --cov=./ --cov-report=xml --cov-report=term |
| 86 | +
|
| 87 | + - name: Log in to GitHub Container Registry |
| 88 | + uses: docker/login-action@v3.6.0 |
| 89 | + with: |
| 90 | + registry: ghcr.io |
| 91 | + username: ${{ github.actor }} |
| 92 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 93 | + |
| 94 | + - name: Set up Docker Buildx |
| 95 | + uses: docker/setup-buildx-action@v3.12.0 |
| 96 | + |
| 97 | + - name: Build and push Docker image to GitHub Container Registry |
| 98 | + uses: docker/build-push-action@v6.18.0 |
| 99 | + with: |
| 100 | + context: . |
| 101 | + push: true |
| 102 | + platforms: linux/amd64,linux/arm64 |
| 103 | + provenance: false |
| 104 | + cache-from: type=gha |
| 105 | + cache-to: type=gha,mode=max |
| 106 | + tags: | |
| 107 | + ghcr.io/${{ env.PACKAGE_NAME }}:${{ steps.version.outputs.semver }} |
| 108 | + ghcr.io/${{ env.PACKAGE_NAME }}:${{ steps.version.outputs.coach }} |
| 109 | + ghcr.io/${{ env.PACKAGE_NAME }}:latest |
| 110 | +
|
| 111 | + - name: Generate changelog |
| 112 | + id: changelog |
| 113 | + run: | |
| 114 | + # Get the previous tag (second most recent tag matching the version pattern) |
| 115 | + PREVIOUS_TAG=$(git tag -l 'v*.*.*-*' --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+-' | sed -n '2p') |
| 116 | +
|
| 117 | + if [ -z "$PREVIOUS_TAG" ]; then |
| 118 | + echo "📝 First release - no previous tag found" |
| 119 | + CHANGELOG="Initial release" |
| 120 | + else |
| 121 | + echo "📝 Generating changelog from $PREVIOUS_TAG to ${{ steps.version.outputs.tag_name }}" |
| 122 | + CHANGELOG=$(git log --pretty=format:"- %s (%h)" ${PREVIOUS_TAG}..${{ steps.version.outputs.tag_name }}) |
| 123 | + fi |
| 124 | +
|
| 125 | + # Write changelog to file |
| 126 | + echo "$CHANGELOG" > changelog.txt |
| 127 | + cat changelog.txt |
| 128 | +
|
| 129 | + # Set output for use in release body |
| 130 | + { |
| 131 | + echo "changelog<<EOF" |
| 132 | + echo "$CHANGELOG" |
| 133 | + echo "EOF" |
| 134 | + } >> $GITHUB_OUTPUT |
| 135 | +
|
| 136 | + - name: Create GitHub Release |
| 137 | + uses: softprops/action-gh-release@v2.2.0 |
| 138 | + with: |
| 139 | + name: "v${{ steps.version.outputs.semver }} - ${{ steps.version.outputs.coach }} ♟️" |
| 140 | + tag_name: ${{ steps.version.outputs.tag_name }} |
| 141 | + body: | |
| 142 | + # ♟️ Release ${{ steps.version.outputs.semver }} - ${{ steps.version.outputs.coach }} |
| 143 | +
|
| 144 | + ## Docker Images |
| 145 | +
|
| 146 | + Pull this release using any of the following tags: |
| 147 | +
|
| 148 | + ```bash |
| 149 | + # By semantic version (recommended) |
| 150 | + docker pull ghcr.io/${{ env.PACKAGE_NAME }}:${{ steps.version.outputs.semver }} |
| 151 | +
|
| 152 | + # By coach name |
| 153 | + docker pull ghcr.io/${{ env.PACKAGE_NAME }}:${{ steps.version.outputs.coach }} |
| 154 | +
|
| 155 | + # Latest |
| 156 | + docker pull ghcr.io/${{ env.PACKAGE_NAME }}:latest |
| 157 | + ``` |
| 158 | +
|
| 159 | + ## Changelog |
| 160 | +
|
| 161 | + ${{ steps.changelog.outputs.changelog }} |
| 162 | +
|
| 163 | + --- |
| 164 | +
|
| 165 | + 📦 **Package:** [ghcr.io/${{ env.PACKAGE_NAME }}](https://github.com/${{ github.repository }}/pkgs/container/python-samples-fastapi-restful) |
| 166 | + draft: false |
| 167 | + prerelease: false |
| 168 | + generate_release_notes: true |
0 commit comments