fix: release draft variable (#181) #86
This file contains 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: "release" | |
# This workflow manages the release process for two types of events: | |
# 1. Merging a pull request into main: | |
# - create a draft release [release] | |
# 2. Pushing a version tag: | |
# - 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: | |
branches: [main] | |
tags: | |
- "v*.*.*" | |
jobs: | |
package: | |
runs-on: ubuntu-latest | |
if: startsWith(github.ref, 'refs/tags/v') | |
permissions: | |
id-token: "write" | |
packages: "write" | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- 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 Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to GitHub Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Convert repository name to lowercase | |
run: echo "PACKAGE_NAME=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV | |
- name: Build and push | |
uses: docker/build-push-action@v6 | |
with: | |
platforms: linux/amd64,linux/arm64 | |
push: true | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
tags: | | |
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 | |
with: | |
python-version: '3.11' | |
cache: 'pip' | |
- name: Build public documentation | |
run: | | |
python -m pip install --upgrade pip | |
pip install '.[dev]' | |
export DB_URI="sqlite:///:memory:" | |
export INITIAL_ALGORITHMS="" | |
export VERSION=${{ env.NEXT_TAG }} | |
./scripts/build_docs.sh _site | |
- name: Upload public documentation | |
uses: actions/upload-pages-artifact@v3 | |
with: | |
path: _site/ | |
- name: Deploy to GitHub Pages | |
uses: actions/deploy-pages@v4 | |
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=$([[ "$next_tag" =~ -rc\.[0-9]+$ ]] && echo true || echo false) | |
echo "Draft release: $draft" | |
echo "DRAFT=$draft" >> $GITHUB_ENV | |
- name: Optionally delete the existing draft release | |
run: | | |
# Get existing draft release (if any) | |
response=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
"https://api.github.com/repos/${{ github.repository }}/releases") | |
# Check if there is a draft release | |
draft_release=$(echo "$response" | jq '.[] | select(.draft == true)') | |
if [[ -n "$draft_release" ]]; then | |
# Delete the existing draft release | |
draft_release_id=$(echo "$draft_release" | jq -r '.id') | |
echo "Deleting draft release: ${draft_release_id}" | |
curl --fail -s -X DELETE \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github+json" \ | |
"https://api.github.com/repos/${{ github.repository }}/releases/${draft_release_id}" | |
fi | |
- name: Create the release | |
run: | | |
# Create a new release | |
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 | |
{ | |
"tag_name": "${{ env.NEXT_TAG }}", | |
"target_commitish": "main", | |
"name": "${{ env.NEXT_TAG }}", | |
"generate_release_notes": true, | |
"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/[email protected] | |
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 |