Skip to content
Closed
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
93 changes: 93 additions & 0 deletions .github/workflows/cleanup-temp-releases.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
name: Cleanup Temporary Branch Releases when Pull Request is Merged

on:
pull_request:
types:
- closed
branches:
- main

permissions:
contents: write
pull-requests: read

jobs:
cleanup-temp-releases:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest

env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

steps:
- name: Normalize merged branch name
run: |
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"

SAFE_BRANCH="${BRANCH_NAME//\//-}"
SAFE_BRANCH="${SAFE_BRANCH//_/-}"
SAFE_BRANCH="$(echo "$SAFE_BRANCH" | tr '[:upper:]' '[:lower:]')"

echo "BRANCH_NAME=$BRANCH_NAME" >> "$GITHUB_ENV"
echo "SAFE_BRANCH=$SAFE_BRANCH" >> "$GITHUB_ENV"

echo "Merged branch: $BRANCH_NAME"
echo "Safe branch: $SAFE_BRANCH"

- name: Delete all temporary releases and tags for merged branch
run: |
set -euo pipefail

PREFIX="temp-${SAFE_BRANCH}-v"

echo "Repository: ${GITHUB_REPOSITORY}"
echo "Looking for temp releases with prefix: ${PREFIX}"

gh api \
--paginate \
"repos/${GITHUB_REPOSITORY}/releases" \
--jq '.[] | [.id, .tag_name] | @tsv' \
> releases.tsv

echo "All release tags currently visible:"
cut -f2 releases.tsv || true

MATCHES="$(
awk -v prefix="$PREFIX" '
BEGIN { prefix_len = length(prefix) }
index($2, prefix) == 1 {
suffix = substr($2, prefix_len + 1)
if (suffix ~ /^[0-9]+$/) {
print $1 "\t" $2
}
}
' releases.tsv
)"

if [ -z "$MATCHES" ]; then
echo "No matching temporary releases found for branch: ${BRANCH_NAME}"
echo "Expected tags like: ${PREFIX}1, ${PREFIX}2, ${PREFIX}3"
exit 0
fi

echo "Matching releases to delete:"
echo "$MATCHES"

echo "$MATCHES" | while IFS="$(printf '\t')" read -r RELEASE_ID TAG; do
if [ -z "$RELEASE_ID" ] || [ -z "$TAG" ]; then
continue
fi

echo "Deleting release id=${RELEASE_ID}, tag=${TAG}"

gh api \
--method DELETE \
"repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}"

echo "Deleting git tag ref: ${TAG}"

gh api \
--method DELETE \
"repos/${GITHUB_REPOSITORY}/git/refs/tags/${TAG}" \
|| echo "Tag ${TAG} was already deleted or did not exist"
done
123 changes: 123 additions & 0 deletions .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
name: Create Release

on:
push:
branches:
- "**"

jobs:
build:
uses: iit-DLSLab/cicd_tools/.github/workflows/dls2_build_test.yml@main
with:
test-command: "ls"
output_workflow_artifact_name: build
secrets:
GHCR_USERNAME: ${{ secrets.GHCR_USERNAME }}
CICD_PAT: ${{ secrets.CICD_PAT }}

release-on-push:
runs-on: ubuntu-latest
needs: build

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
set-safe-directory: true

- name: Normalize branch name
run: |
SAFE_BRANCH="${GITHUB_REF_NAME//\//-}"
SAFE_BRANCH="${SAFE_BRANCH//_/-}"
SAFE_BRANCH="$(echo "$SAFE_BRANCH" | tr '[:upper:]' '[:lower:]')"

echo "SAFE_BRANCH=$SAFE_BRANCH" >> "$GITHUB_ENV"

- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: build
path: build

- name: Compute release tag
run: |
git fetch --tags --force

if [ "$GITHUB_REF_NAME" = "main" ]; then
LAST_VERSION="$(
git tag --list 'v[0-9]*.[0-9]*.[0-9]*' \
| sed 's/^v//' \
| grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' \
| sort -V \
| tail -n 1
)"

if [ -z "$LAST_VERSION" ]; then
NEXT_VERSION="0.1.0"
else
MAJOR="$(echo "$LAST_VERSION" | cut -d. -f1)"
MINOR="$(echo "$LAST_VERSION" | cut -d. -f2)"
PATCH="$(echo "$LAST_VERSION" | cut -d. -f3)"

MINOR=$((MINOR + 1))
PATCH=0

NEXT_VERSION="${MAJOR}.${MINOR}.${PATCH}"
fi

RELEASE_TAG="v${NEXT_VERSION}"
else
PREFIX="temp-${SAFE_BRANCH}-v"

LAST_NUMBER="$(
git tag --list "${PREFIX}*" \
| sed "s/^${PREFIX}//" \
| grep -E '^[0-9]+$' \
| sort -n \
| tail -n 1
)"

if [ -z "$LAST_NUMBER" ]; then
NEXT_NUMBER=1
else
NEXT_NUMBER=$((LAST_NUMBER + 1))
fi

RELEASE_TAG="${PREFIX}${NEXT_NUMBER}"
fi

echo "RELEASE_TAG=$RELEASE_TAG" >> "$GITHUB_ENV"
echo "Computed release tag: $RELEASE_TAG"

- name: Package build artifact
run: |
tar -czf build.tar.gz -C build .
echo "Created build.tar.gz ($(du -sh build.tar.gz | cut -f1))"

- name: Create main GitHub release and upload assets
if: github.ref_name == 'main'
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ env.RELEASE_TAG }}
name: ${{ env.RELEASE_TAG }}
target_commitish: ${{ github.sha }}
files: build.tar.gz
make_latest: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Create branch GitHub release and upload assets
if: github.ref_name != 'main'
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ env.RELEASE_TAG }}
name: ${{ env.RELEASE_TAG }}
target_commitish: ${{ github.sha }}
files: build.tar.gz
make_latest: false
prerelease: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}