CD - Build and Release #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CD - Build and Release | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| version_type: | |
| description: 'Version bump type' | |
| required: true | |
| default: 'patch' | |
| type: choice | |
| options: | |
| - patch | |
| - minor | |
| - major | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| version: | |
| name: Calculate Version | |
| runs-on: ubuntu-latest | |
| outputs: | |
| version: ${{ steps.version.outputs.version }} | |
| previous_version: ${{ steps.version.outputs.previous_version }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # Need full history for tags | |
| - name: Get latest version tag | |
| id: get_version | |
| run: | | |
| # Get the latest semantic version tag | |
| LATEST_TAG=$(git tag --sort=-version:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -1 || echo "v0.0.0") | |
| echo "Latest tag: $LATEST_TAG" | |
| echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT | |
| - name: Calculate new version | |
| id: version | |
| run: | | |
| LATEST_TAG="${{ steps.get_version.outputs.latest_tag }}" | |
| echo "previous_version=$LATEST_TAG" >> $GITHUB_OUTPUT | |
| # Extract version components | |
| VERSION_WITHOUT_V="${LATEST_TAG#v}" | |
| IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION_WITHOUT_V" | |
| # Default to 0.0.0 if no valid version found | |
| MAJOR=${MAJOR:-0} | |
| MINOR=${MINOR:-0} | |
| PATCH=${PATCH:-0} | |
| # Increment based on input | |
| case "${{ github.event.inputs.version_type }}" in | |
| major) | |
| MAJOR=$((MAJOR + 1)) | |
| MINOR=0 | |
| PATCH=0 | |
| ;; | |
| minor) | |
| MINOR=$((MINOR + 1)) | |
| PATCH=0 | |
| ;; | |
| patch) | |
| PATCH=$((PATCH + 1)) | |
| ;; | |
| esac | |
| NEW_VERSION="v${MAJOR}.${MINOR}.${PATCH}" | |
| echo "New version: $NEW_VERSION" | |
| echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT | |
| build-and-push: | |
| name: Build and Push Docker Image | |
| runs-on: ubuntu-latest | |
| needs: version | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| tags: | | |
| type=raw,value=${{ needs.version.outputs.version }} | |
| type=raw,value=latest | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| platforms: linux/amd64,linux/arm64 | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| build-args: | | |
| VERSION=${{ needs.version.outputs.version }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| create-release: | |
| name: Create GitHub Release | |
| runs-on: ubuntu-latest | |
| needs: [version, build-and-push] | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Create tag | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git tag ${{ needs.version.outputs.version }} | |
| git push origin ${{ needs.version.outputs.version }} | |
| - name: Generate release notes | |
| id: changelog | |
| run: | | |
| # Generate changelog between versions | |
| PREV_TAG="${{ needs.version.outputs.previous_version }}" | |
| NEW_TAG="${{ needs.version.outputs.version }}" | |
| { | |
| echo "## What's Changed" | |
| echo "" | |
| if [ "$PREV_TAG" = "v0.0.0" ]; then | |
| echo "Initial release 🎉" | |
| else | |
| # Get PR merge commits | |
| git log ${PREV_TAG}..HEAD --merges --pretty=format:"* %s" | grep -E "Merge pull request #[0-9]+" || true | |
| # Get direct commits (non-merge) | |
| echo "" | |
| echo "### Direct commits" | |
| git log ${PREV_TAG}..HEAD --no-merges --pretty=format:"* %s (%an)" || true | |
| fi | |
| echo "" | |
| echo "## Docker Image" | |
| echo "" | |
| echo "Pull the latest image:" | |
| echo '```bash' | |
| echo "docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.version.outputs.version }}" | |
| echo '```' | |
| echo "" | |
| echo "Or use docker-compose:" | |
| echo '```yaml' | |
| echo "services:" | |
| echo " service-quality-oracle:" | |
| echo " image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.version.outputs.version }}" | |
| echo '```' | |
| } > release_notes.md | |
| - name: Create Release | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| tag_name: ${{ needs.version.outputs.version }} | |
| name: Release ${{ needs.version.outputs.version }} | |
| body_path: release_notes.md | |
| draft: false | |
| prerelease: false | |