Skip to content

Commit 692aad0

Browse files
committed
Create release action
1 parent 3c61330 commit 692aad0

1 file changed

Lines changed: 139 additions & 0 deletions

File tree

.github/workflows/release.yaml

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
name: Release
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
version_type:
7+
description: 'Type of version bump'
8+
required: true
9+
default: 'patch'
10+
type: choice
11+
options:
12+
- patch
13+
- minor
14+
- major
15+
custom_version:
16+
description: 'Custom version (optional, overrides version_type)'
17+
required: false
18+
type: string
19+
prerelease:
20+
description: 'Create a prerelease'
21+
required: false
22+
default: false
23+
type: boolean
24+
25+
jobs:
26+
release:
27+
runs-on: ubuntu-latest
28+
permissions:
29+
contents: write
30+
31+
steps:
32+
- name: Checkout
33+
uses: actions/checkout@v6
34+
with:
35+
token: ${{ secrets.GITHUB_TOKEN }}
36+
37+
- name: Determine version
38+
id: version
39+
run: |
40+
if [ -n "${{ github.event.inputs.custom_version }}" ]; then
41+
BASE_VERSION="${{ github.event.inputs.custom_version }}"
42+
else
43+
# Get current version from header file
44+
if [ ! -f "include/xtensor/core/xtensor_config.hpp" ]; then
45+
echo "Error: xtensor_config.hpp not found"
46+
exit 1
47+
fi
48+
49+
CURRENT_MAJOR=$(grep "#define XTENSOR_VERSION_MAJOR" include/xtensor/core/xtensor_config.hpp | awk '{print $3}')
50+
CURRENT_MINOR=$(grep "#define XTENSOR_VERSION_MINOR" include/xtensor/core/xtensor_config.hpp | awk '{print $3}')
51+
CURRENT_PATCH=$(grep "#define XTENSOR_VERSION_PATCH" include/xtensor/core/xtensor_config.hpp | awk '{print $3}')
52+
53+
# Verify version numbers were found
54+
if [ -z "$CURRENT_MAJOR" ] || [ -z "$CURRENT_MINOR" ] || [ -z "$CURRENT_PATCH" ]; then
55+
echo "Error: Could not parse current version from header file"
56+
exit 1
57+
fi
58+
59+
echo "Current version: ${CURRENT_MAJOR}.${CURRENT_MINOR}.${CURRENT_PATCH}"
60+
61+
case "${{ github.event.inputs.version_type }}" in
62+
"major")
63+
BASE_VERSION="$((CURRENT_MAJOR + 1)).0.0"
64+
;;
65+
"minor")
66+
BASE_VERSION="${CURRENT_MAJOR}.$((CURRENT_MINOR + 1)).0"
67+
;;
68+
"patch")
69+
BASE_VERSION="${CURRENT_MAJOR}.${CURRENT_MINOR}.$((CURRENT_PATCH + 1))"
70+
;;
71+
esac
72+
fi
73+
74+
# Handle prerelease
75+
if [ "${{ github.event.inputs.prerelease }}" = "true" ]; then
76+
# Find existing prerelease tags for this version
77+
EXISTING_TAGS=$(git tag -l "${BASE_VERSION}_r*" | sort -V)
78+
79+
if [ -z "$EXISTING_TAGS" ]; then
80+
# No existing prereleases, start with _r0
81+
PRERELEASE_NUM=0
82+
else
83+
# Get the highest prerelease number and increment
84+
LAST_TAG=$(echo "$EXISTING_TAGS" | tail -n 1)
85+
PRERELEASE_NUM=$(echo "$LAST_TAG" | sed "s/${BASE_VERSION}_r//" | sed 's/^0*//')
86+
if [ -z "$PRERELEASE_NUM" ]; then
87+
PRERELEASE_NUM=0
88+
fi
89+
PRERELEASE_NUM=$((PRERELEASE_NUM + 1))
90+
fi
91+
92+
FINAL_VERSION="${BASE_VERSION}_r${PRERELEASE_NUM}"
93+
echo "is_prerelease=true" >> $GITHUB_OUTPUT
94+
else
95+
FINAL_VERSION="$BASE_VERSION"
96+
echo "is_prerelease=false" >> $GITHUB_OUTPUT
97+
fi
98+
99+
echo "Final version: $FINAL_VERSION"
100+
echo "version=$FINAL_VERSION" >> $GITHUB_OUTPUT
101+
echo "base_version=$BASE_VERSION" >> $GITHUB_OUTPUT
102+
103+
- name: Update version in header file
104+
run: |
105+
BASE_VERSION="${{ steps.version.outputs.base_version }}"
106+
IFS='.' read -r NEW_MAJOR NEW_MINOR NEW_PATCH <<< "$BASE_VERSION"
107+
108+
# Only update header file for non-prerelease versions
109+
if [ "${{ steps.version.outputs.is_prerelease }}" = "false" ]; then
110+
# Update version numbers in header file
111+
sed -i "s/^#define XTENSOR_VERSION_MAJOR [0-9]*/#define XTENSOR_VERSION_MAJOR $NEW_MAJOR/" include/xtensor/core/xtensor_config.hpp
112+
sed -i "s/^#define XTENSOR_VERSION_MINOR [0-9]*/#define XTENSOR_VERSION_MINOR $NEW_MINOR/" include/xtensor/core/xtensor_config.hpp
113+
sed -i "s/^#define XTENSOR_VERSION_PATCH [0-9]*/#define XTENSOR_VERSION_PATCH $NEW_PATCH/" include/xtensor/core/xtensor_config.hpp
114+
115+
git config --local user.name "GitHub Action"
116+
git add include/xtensor/core/xtensor_config.hpp
117+
git commit -m "Release version ${{ steps.version.outputs.version }}"
118+
else
119+
echo "Skipping header file update for prerelease"
120+
git config --local user.name "GitHub Action"
121+
fi
122+
123+
- name: Create tag
124+
run: |
125+
git tag -a "${{ steps.version.outputs.version }}" -m "Release version ${{ steps.version.outputs.version }}"
126+
127+
- name: Push changes and tag
128+
run: |
129+
git push origin master
130+
git push origin "${{ steps.version.outputs.version }}"
131+
132+
- name: Create GitHub Release
133+
uses: elgohr/Github-Release-Action@v5
134+
env:
135+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
136+
with:
137+
title: "Release ${{ steps.version.outputs.version }}"
138+
tag: "${{ steps.version.outputs.version }}"
139+
prerelease: ${{ steps.version.outputs.is_prerelease }}

0 commit comments

Comments
 (0)