Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slack notification after release #166

Merged
merged 17 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 87 additions & 37 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
name: "release"
# This workflow handles two different scenarios:
# This workflow manages the release process for two types of events:
# 1. Merging a pull request into main:
# - creation of a draft release
# - create a draft release [release]
# 2. Pushing a version tag:
# - building and pushing of a new Docker image to ghcr.io
# - building and uploading of the public documentation
# - creation of a published release
# - building and pushing of a new Docker image to ghcr.io [package]
# - building and uploading of the public documentation [docs]
# - create a published release [release]
# - post the release info to Slack [release]

on:
push:
Expand All @@ -14,54 +15,39 @@ on:
- "v*.*.*"

jobs:
release:
package:
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v')

permissions:
contents: "write"
id-token: "write"
packages: "write"
pages: "write"

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for tags

- name: Next tag
run: |
# Get the tag that triggered the workflow
tag=${GITHUB_REF#refs/tags/}
# Test if the tag is a version tag
if [[ "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
next_tag=$tag
else
next_tag=$(scripts/next_tag.sh)
fi

next_tag=$(scripts/version_name.sh)
echo "Next tag: $next_tag"
echo "NEXT_TAG=$next_tag" >> $GITHUB_ENV

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
if: startsWith(github.ref, 'refs/tags/v')

- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
if: startsWith(github.ref, 'refs/tags/v')
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Convert repository name to lowercase
if: startsWith(github.ref, 'refs/tags/v')
run: echo "PACKAGE_NAME=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV

- name: Build and push
uses: docker/build-push-action@v6
if: startsWith(github.ref, 'refs/tags/v')
with:
platforms: linux/amd64,linux/arm64
push: true
Expand All @@ -71,15 +57,37 @@ jobs:
ghcr.io/${{ env.PACKAGE_NAME }}:latest
ghcr.io/${{ env.PACKAGE_NAME }}:${{ env.NEXT_TAG }}

docs:
runs-on: ubuntu-latest
needs: [package]
if: startsWith(github.ref, 'refs/tags/v')

permissions:
id-token: "write"
pages: "write"

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for tags

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Next tag
run: |
next_tag=$(scripts/version_name.sh)
echo "Next tag: $next_tag"
echo "NEXT_TAG=$next_tag" >> $GITHUB_ENV

- name: Set up Python
uses: actions/setup-python@v5
if: startsWith(github.ref, 'refs/tags/v')
with:
python-version: '3.11'
cache: 'pip'

- name: Build public documentation
if: startsWith(github.ref, 'refs/tags/v')
run: |
python -m pip install --upgrade pip
pip install '.[dev]'
Expand All @@ -90,13 +98,36 @@ jobs:

- name: Upload public documentation
uses: actions/upload-pages-artifact@v3
if: startsWith(github.ref, 'refs/tags/v')
with:
path: _site/

- name: Deploy to GitHub Pages
uses: actions/deploy-pages@v4
if: startsWith(github.ref, 'refs/tags/v')

release:
runs-on: ubuntu-latest
needs: [package, docs]
if: always()

permissions:
id-token: "write"
contents: "write"

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for tags

- name: Next tag
run: |
next_tag=$(scripts/version_name.sh)
echo "Next tag: $next_tag"
echo "NEXT_TAG=$next_tag" >> $GITHUB_ENV
# If the tag ends in -rc.*, then its a draft release
draft=$([[ "${{ env.NEXT_TAG }}" =~ -rc\.[0-9]+$ ]] && echo false || echo true)
echo "Draft release: $draft"
echo "DRAFT=$draft" >> $GITHUB_ENV

- name: Optionally delete the existing draft release
run: |
Expand All @@ -118,16 +149,8 @@ jobs:

- name: Create the release
run: |
# If the tag ends in -rc.*, then its a draft release
if [[ "${{ env.NEXT_TAG }}" =~ -rc\.[0-9]+$ ]]; then
echo "Creating a draft release"
draft=true
else
echo "Creating a published release"
draft=false
fi
# Create a new release
curl --fail -s -X POST \
response=$(curl --fail -s -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github+json" \
-d @- "https://api.github.com/repos/${{ github.repository }}/releases" <<EOF
Expand All @@ -136,6 +159,33 @@ jobs:
"target_commitish": "main",
"name": "${{ env.NEXT_TAG }}",
"generate_release_notes": true,
"draft": ${draft}
"draft": ${{ env.DRAFT }}
}
EOF
)
echo "Response: $response"
# Parse the HTML URL from the array
html_url=$(echo "$response" | jq -r '.html_url')
echo "Release URL: $html_url"
echo "RELEASE_URL=$html_url" >> $GITHUB_ENV

- name: Post release to Slack
if: env.DRAFT == 'false'
uses: slackapi/slack-github-action@v1.27.0
with:
payload: |
{
"text": ":package: A new release has been created: <${{ env.RELEASE_URL }}|${{ env.NEXT_TAG }}>",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":package: A new release has been created: <${{ env.RELEASE_URL }}|${{ env.NEXT_TAG }}>"
}
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
23 changes: 0 additions & 23 deletions scripts/next_tag.sh

This file was deleted.

29 changes: 29 additions & 0 deletions scripts/version_name.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh
#
# Get the name of the version if the commit is tagged, else
# calculate the next feature version name for the project.
#
# Usage: scripts/version_name.sh
#
# The format of the tag is vYY.X.0-rc.Z, where:
# - YY is the current two digit year
# - X is the next feature version this year
# - Z is the number of commits on main since the last tag

HEAD_TAG=$(git describe --tags --exact-match HEAD 2>/dev/null)
# Check if the HEAD commit is tagged like '^v[0-9]+\.[0-9]+\.[0-9]+$'
if echo $HEAD_TAG | grep -q "^v[0-9]\+\.[0-9]\+\.[0-9]\+$"; then
echo $HEAD_TAG
else
# Get the current two digit year
year=$(date +"%y")
# Get the latest tag
latest_tag=$(git describe --tags --match "v*" --abbrev=0 $(git rev-list --tags --max-count=1) 2>/dev/null || echo "")
# Count the number of commits between the latest tag and HEAD
commits=$(git rev-list --count $latest_tag..HEAD --)
# Get latest tag for the current year, or default to v0.0.0
latest_tag_for_year=$(git describe --tags --match "v${year}.*" --abbrev=0 $(git rev-list --tags --max-count=1) 2>/dev/null || echo "v0.0.0")
# Get the next feature version
next_feature_ver=$(($(echo $latest_tag_for_year | cut -d '.' -f 2) + 1))
echo "v${year}.${next_feature_ver}.0-rc.${commits}"
fi
Loading