|
| 1 | +name: CD - Build and Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + version_type: |
| 7 | + description: 'Version bump type' |
| 8 | + required: true |
| 9 | + default: 'patch' |
| 10 | + type: choice |
| 11 | + options: |
| 12 | + - patch |
| 13 | + - minor |
| 14 | + - major |
| 15 | + |
| 16 | +env: |
| 17 | + REGISTRY: ghcr.io |
| 18 | + IMAGE_NAME: ${{ github.repository }} |
| 19 | + |
| 20 | +jobs: |
| 21 | + version: |
| 22 | + name: Calculate Version |
| 23 | + runs-on: ubuntu-latest |
| 24 | + outputs: |
| 25 | + version: ${{ steps.version.outputs.version }} |
| 26 | + previous_version: ${{ steps.version.outputs.previous_version }} |
| 27 | + steps: |
| 28 | + - name: Checkout code |
| 29 | + uses: actions/checkout@v4 |
| 30 | + with: |
| 31 | + fetch-depth: 0 # Need full history for tags |
| 32 | + |
| 33 | + - name: Get latest version tag |
| 34 | + id: get_version |
| 35 | + run: | |
| 36 | + # Get the latest semantic version tag |
| 37 | + LATEST_TAG=$(git tag --sort=-version:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -1 || echo "v0.0.0") |
| 38 | + echo "Latest tag: $LATEST_TAG" |
| 39 | + echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT |
| 40 | +
|
| 41 | + - name: Calculate new version |
| 42 | + id: version |
| 43 | + run: | |
| 44 | + LATEST_TAG="${{ steps.get_version.outputs.latest_tag }}" |
| 45 | + echo "previous_version=$LATEST_TAG" >> $GITHUB_OUTPUT |
| 46 | + |
| 47 | + # Extract version components |
| 48 | + VERSION_WITHOUT_V="${LATEST_TAG#v}" |
| 49 | + IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION_WITHOUT_V" |
| 50 | + |
| 51 | + # Default to 0.0.0 if no valid version found |
| 52 | + MAJOR=${MAJOR:-0} |
| 53 | + MINOR=${MINOR:-0} |
| 54 | + PATCH=${PATCH:-0} |
| 55 | + |
| 56 | + # Increment based on input |
| 57 | + case "${{ github.event.inputs.version_type }}" in |
| 58 | + major) |
| 59 | + MAJOR=$((MAJOR + 1)) |
| 60 | + MINOR=0 |
| 61 | + PATCH=0 |
| 62 | + ;; |
| 63 | + minor) |
| 64 | + MINOR=$((MINOR + 1)) |
| 65 | + PATCH=0 |
| 66 | + ;; |
| 67 | + patch) |
| 68 | + PATCH=$((PATCH + 1)) |
| 69 | + ;; |
| 70 | + esac |
| 71 | + |
| 72 | + NEW_VERSION="v${MAJOR}.${MINOR}.${PATCH}" |
| 73 | + echo "New version: $NEW_VERSION" |
| 74 | + echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 75 | +
|
| 76 | + build-and-push: |
| 77 | + name: Build and Push Docker Image |
| 78 | + runs-on: ubuntu-latest |
| 79 | + needs: version |
| 80 | + permissions: |
| 81 | + contents: read |
| 82 | + packages: write |
| 83 | + steps: |
| 84 | + - name: Checkout code |
| 85 | + uses: actions/checkout@v4 |
| 86 | + |
| 87 | + - name: Set up Docker Buildx |
| 88 | + uses: docker/setup-buildx-action@v3 |
| 89 | + |
| 90 | + - name: Log in to GitHub Container Registry |
| 91 | + uses: docker/login-action@v3 |
| 92 | + with: |
| 93 | + registry: ${{ env.REGISTRY }} |
| 94 | + username: ${{ github.actor }} |
| 95 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 96 | + |
| 97 | + - name: Extract metadata |
| 98 | + id: meta |
| 99 | + uses: docker/metadata-action@v5 |
| 100 | + with: |
| 101 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 102 | + tags: | |
| 103 | + type=raw,value=${{ needs.version.outputs.version }} |
| 104 | + type=raw,value=latest |
| 105 | +
|
| 106 | + - name: Build and push Docker image |
| 107 | + uses: docker/build-push-action@v5 |
| 108 | + with: |
| 109 | + context: . |
| 110 | + platforms: linux/amd64,linux/arm64 |
| 111 | + push: true |
| 112 | + tags: ${{ steps.meta.outputs.tags }} |
| 113 | + labels: ${{ steps.meta.outputs.labels }} |
| 114 | + build-args: | |
| 115 | + VERSION=${{ needs.version.outputs.version }} |
| 116 | + cache-from: type=gha |
| 117 | + cache-to: type=gha,mode=max |
| 118 | + |
| 119 | + create-release: |
| 120 | + name: Create GitHub Release |
| 121 | + runs-on: ubuntu-latest |
| 122 | + needs: [version, build-and-push] |
| 123 | + permissions: |
| 124 | + contents: write |
| 125 | + steps: |
| 126 | + - name: Checkout code |
| 127 | + uses: actions/checkout@v4 |
| 128 | + with: |
| 129 | + fetch-depth: 0 |
| 130 | + |
| 131 | + - name: Create tag |
| 132 | + run: | |
| 133 | + git config user.name "github-actions[bot]" |
| 134 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 135 | + git tag ${{ needs.version.outputs.version }} |
| 136 | + git push origin ${{ needs.version.outputs.version }} |
| 137 | +
|
| 138 | + - name: Generate release notes |
| 139 | + id: changelog |
| 140 | + run: | |
| 141 | + # Generate changelog between versions |
| 142 | + PREV_TAG="${{ needs.version.outputs.previous_version }}" |
| 143 | + NEW_TAG="${{ needs.version.outputs.version }}" |
| 144 | + |
| 145 | + { |
| 146 | + echo "## What's Changed" |
| 147 | + echo "" |
| 148 | + |
| 149 | + if [ "$PREV_TAG" = "v0.0.0" ]; then |
| 150 | + echo "Initial release 🎉" |
| 151 | + else |
| 152 | + # Get PR merge commits |
| 153 | + git log ${PREV_TAG}..HEAD --merges --pretty=format:"* %s" | grep -E "Merge pull request #[0-9]+" || true |
| 154 | + |
| 155 | + # Get direct commits (non-merge) |
| 156 | + echo "" |
| 157 | + echo "### Direct commits" |
| 158 | + git log ${PREV_TAG}..HEAD --no-merges --pretty=format:"* %s (%an)" || true |
| 159 | + fi |
| 160 | + |
| 161 | + echo "" |
| 162 | + echo "## Docker Image" |
| 163 | + echo "" |
| 164 | + echo "Pull the latest image:" |
| 165 | + echo '```bash' |
| 166 | + echo "docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.version.outputs.version }}" |
| 167 | + echo '```' |
| 168 | + echo "" |
| 169 | + echo "Or use docker-compose:" |
| 170 | + echo '```yaml' |
| 171 | + echo "services:" |
| 172 | + echo " service-quality-oracle:" |
| 173 | + echo " image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.version.outputs.version }}" |
| 174 | + echo '```' |
| 175 | + } > release_notes.md |
| 176 | +
|
| 177 | + - name: Create Release |
| 178 | + uses: softprops/action-gh-release@v1 |
| 179 | + with: |
| 180 | + tag_name: ${{ needs.version.outputs.version }} |
| 181 | + name: Release ${{ needs.version.outputs.version }} |
| 182 | + body_path: release_notes.md |
| 183 | + draft: false |
| 184 | + prerelease: false |
| 185 | + |
0 commit comments