publish site docs (#115) #39
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 handles two different scenarios: | |
# 1. Merging a pull request into main: | |
# - creation of a draft 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 | |
on: | |
push: | |
branches: [main] | |
tags: | |
- "v*.*.*" | |
jobs: | |
release: | |
runs-on: ubuntu-latest | |
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 | |
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 | |
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 }} | |
- 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]' | |
export INITIAL_ALGORITHMS="" | |
export VERSION=${{ env.NEXT_TAG }} | |
./scripts/build_docs.sh _site | |
- 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') | |
- 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: | | |
# 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 \ | |
-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": ${draft} | |
} | |
EOF |