name: Build and Push Docker Image on: push: branches: - main tags: - 'v*' pull_request: branches: - main workflow_dispatch: env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} jobs: build-and-push: runs-on: ubuntu-latest permissions: contents: read packages: write id-token: write steps: - name: Checkout repository uses: actions/checkout@v6 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to GitHub Container Registry if: github.event_name != 'pull_request' uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata (tags, labels) id: meta uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=ref,event=branch type=ref,event=pr type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}} type=sha,prefix=sha- type=raw,value=latest,enable={{is_default_branch}} - name: Build and push Docker image uses: docker/build-push-action@v6 with: context: . push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max platforms: linux/amd64 release: if: startsWith(github.ref, 'refs/tags/v') needs: build-and-push runs-on: ubuntu-latest permissions: contents: write packages: write steps: - name: Checkout uses: actions/checkout@v6 with: fetch-depth: 0 - name: Set up Helm uses: azure/setup-helm@v4 - name: Extract version from tag id: version run: | TAG=${GITHUB_REF#refs/tags/} VERSION=${TAG#v} echo "tag=${TAG}" >> $GITHUB_OUTPUT echo "version=${VERSION}" >> $GITHUB_OUTPUT echo "🚀 Creating release for ${TAG}" - name: Package and Push Helm Chart run: | helm registry login ghcr.io \ --username ${{ github.actor }} \ --password ${{ secrets.GITHUB_TOKEN }} helm package chart/ helm push devcontainer-${{ steps.version.outputs.version }}.tgz oci://ghcr.io/cpfarhood/charts - name: Generate Release Notes id: notes run: | # Get commits since last tag PREV_TAG=$(git describe --tags --abbrev=0 ${{ steps.version.outputs.tag }}^ 2>/dev/null || echo "") if [ -z "$PREV_TAG" ]; then COMMITS=$(git log --pretty=format:"- %s (%h)" ${{ steps.version.outputs.tag }}) else COMMITS=$(git log --pretty=format:"- %s (%h)" ${PREV_TAG}..${{ steps.version.outputs.tag }}) fi cat << EOF > release-notes.md ## 🚀 Release ${{ steps.version.outputs.version }} ### Changes ${COMMITS} ### Docker Image \`\`\`bash docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.tag }} \`\`\` ### Helm Chart \`\`\`bash helm install devcontainer oci://ghcr.io/cpfarhood/charts/devcontainer --version ${{ steps.version.outputs.version }} \`\`\` EOF echo "notes<> $GITHUB_OUTPUT cat release-notes.md >> $GITHUB_OUTPUT echo "EOF" >> $GITHUB_OUTPUT - name: Create GitHub Release uses: actions/create-release@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: tag_name: ${{ steps.version.outputs.tag }} release_name: Release ${{ steps.version.outputs.tag }} body: ${{ steps.notes.outputs.notes }} draft: false prerelease: false