Skip to content
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
137 changes: 96 additions & 41 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,53 +1,108 @@
name: Create Release
name: Release

on:
push:
on:
workflow_dispatch:
inputs:
version:
description: Release version without the leading v, for example 2.0.0
required: true
type: string

# Publish `master` as Docker `latest` image.
branches:
- main
permissions:
contents: read

# Publish `v1.2.3` tags as releases.
tags:
- v*
concurrency:
group: release
cancel-in-progress: false

# Create a button to trigger the workflow manually.
workflow_dispatch:
jobs:
verify:
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.version.outputs.tag }}
major_tag: ${{ steps.version.outputs.major_tag }}
steps:
- name: Require the main branch
if: github.ref != 'refs/heads/main'
run: |
echo '::error::Releases must be created from the main branch.'
exit 1

env:
# the target name of the image.
IMAGE_NAME: deploy-from-github
- uses: actions/checkout@v6
with:
persist-credentials: false

jobs:
# Push image to GitHub Packages Container Registry.
push:
- uses: actions/setup-node@v6
with:
node-version: 24
cache: npm

- name: Validate version
id: version
env:
RELEASE_VERSION: ${{ inputs.version }}
run: |
set -euo pipefail

if [[ ! "$RELEASE_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo '::error::The version must use the format 2.0.0 without a leading v.'
exit 1
fi

PACKAGE_VERSION="$(node -p "require('./package.json').version")"
if [[ "$RELEASE_VERSION" != "$PACKAGE_VERSION" ]]; then
echo "::error::Requested version $RELEASE_VERSION does not match package.json version $PACKAGE_VERSION."
exit 1
fi

echo "tag=v$RELEASE_VERSION" >> "$GITHUB_OUTPUT"
echo "major_tag=v${RELEASE_VERSION%%.*}" >> "$GITHUB_OUTPUT"

- run: npm ci
- run: npm run verify

release:
needs: verify
runs-on: ubuntu-latest
permissions:
packages: write
contents: read

contents: write
steps:
- uses: actions/checkout@v3
- name: Create release and update major tag
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ needs.verify.outputs.tag }}
MAJOR_TAG: ${{ needs.verify.outputs.major_tag }}
run: |
set -euo pipefail

- name: Build image
run: docker build . --file Dockerfile --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}"
if gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$TAG" >/dev/null 2>&1; then
EXISTING_SHA="$(gh api "repos/$GITHUB_REPOSITORY/commits/$TAG" --jq .sha)"
if [[ "$EXISTING_SHA" != "$GITHUB_SHA" ]]; then
echo "::error::Tag $TAG already points to $EXISTING_SHA."
exit 1
fi
else
gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" \
-f ref="refs/tags/$TAG" \
-f sha="$GITHUB_SHA" >/dev/null
fi

- name: Log in to registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin
if gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$MAJOR_TAG" >/dev/null 2>&1; then
gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/tags/$MAJOR_TAG" \
-f sha="$GITHUB_SHA" \
-F force=true >/dev/null
else
gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" \
-f ref="refs/tags/$MAJOR_TAG" \
-f sha="$GITHUB_SHA" >/dev/null
fi

- name: Push image
run: |
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME

# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention
[ "$VERSION" == "main" ] && VERSION=latest
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
if gh release view "$TAG" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1; then
echo "Release $TAG already exists."
else
gh release create "$TAG" \
--repo "$GITHUB_REPOSITORY" \
--verify-tag \
--title "$TAG" \
--generate-notes
fi
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test

on:
pull_request:
push:
branches:
- main

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
Comment thread
coderabbitai[bot] marked this conversation as resolved.
with:
persist-credentials: false
- uses: actions/setup-node@v6
with:
node-version: 24
cache: npm
- run: npm ci
- run: npm run verify
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
coverage/
dist-types/
10 changes: 0 additions & 10 deletions Dockerfile

This file was deleted.

Loading