Skip to content

Commit 7638c13

Browse files
committed
ci: release version check
1 parent 6d86370 commit 7638c13

1 file changed

Lines changed: 80 additions & 2 deletions

File tree

.github/workflows/release.yml

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,85 @@ env:
1616
IMG: ghcr.io/openvirtualcluster/openvirtualcluster-operator:${{ github.event_name == 'workflow_dispatch' && format('v{0}', github.event.inputs.version) || github.ref_name }}
1717

1818
jobs:
19+
validate-version:
20+
if: github.event_name == 'workflow_dispatch'
21+
runs-on: ubuntu-latest
22+
outputs:
23+
is_valid: ${{ steps.validate.outputs.is_valid }}
24+
steps:
25+
- name: Checkout code
26+
uses: actions/checkout@v3
27+
with:
28+
fetch-depth: 0
29+
30+
- name: Validate version
31+
id: validate
32+
run: |
33+
INPUT_VERSION="${{ github.event.inputs.version }}"
34+
35+
# Check if version is a valid semver (X.Y.Z format)
36+
if ! [[ $INPUT_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
37+
echo "Error: Version must be in format X.Y.Z (e.g. 1.2.3)"
38+
echo "is_valid=false" >> $GITHUB_OUTPUT
39+
exit 1
40+
fi
41+
42+
# Get all existing tags
43+
git fetch --tags
44+
45+
# Check if tag already exists
46+
if git tag | grep -q "^v$INPUT_VERSION$"; then
47+
echo "Error: Version v$INPUT_VERSION already exists"
48+
echo "is_valid=false" >> $GITHUB_OUTPUT
49+
exit 1
50+
fi
51+
52+
# Find latest version
53+
LATEST_TAG=$(git tag -l "v*.*.*" | sort -V | tail -n 1)
54+
55+
if [ -z "$LATEST_TAG" ]; then
56+
echo "No previous versions found. This will be the first release."
57+
echo "is_valid=true" >> $GITHUB_OUTPUT
58+
exit 0
59+
fi
60+
61+
LATEST_VERSION=${LATEST_TAG#v}
62+
63+
# Split versions into components
64+
IFS='.' read -r LATEST_MAJOR LATEST_MINOR LATEST_PATCH <<< "$LATEST_VERSION"
65+
IFS='.' read -r INPUT_MAJOR INPUT_MINOR INPUT_PATCH <<< "$INPUT_VERSION"
66+
67+
# Convert to integers for comparison
68+
LATEST_MAJOR=$((10#$LATEST_MAJOR))
69+
LATEST_MINOR=$((10#$LATEST_MINOR))
70+
LATEST_PATCH=$((10#$LATEST_PATCH))
71+
INPUT_MAJOR=$((10#$INPUT_MAJOR))
72+
INPUT_MINOR=$((10#$INPUT_MINOR))
73+
INPUT_PATCH=$((10#$INPUT_PATCH))
74+
75+
# Validate version is newer
76+
if [ $INPUT_MAJOR -gt $LATEST_MAJOR ]; then
77+
# Major version bump is valid
78+
echo "Valid major version bump: $LATEST_VERSION -> $INPUT_VERSION"
79+
echo "is_valid=true" >> $GITHUB_OUTPUT
80+
elif [ $INPUT_MAJOR -eq $LATEST_MAJOR ] && [ $INPUT_MINOR -gt $LATEST_MINOR ]; then
81+
# Minor version bump is valid
82+
echo "Valid minor version bump: $LATEST_VERSION -> $INPUT_VERSION"
83+
echo "is_valid=true" >> $GITHUB_OUTPUT
84+
elif [ $INPUT_MAJOR -eq $LATEST_MAJOR ] && [ $INPUT_MINOR -eq $LATEST_MINOR ] && [ $INPUT_PATCH -gt $LATEST_PATCH ]; then
85+
# Patch version bump is valid
86+
echo "Valid patch version bump: $LATEST_VERSION -> $INPUT_VERSION"
87+
echo "is_valid=true" >> $GITHUB_OUTPUT
88+
else
89+
echo "Error: Version $INPUT_VERSION is not newer than the latest version $LATEST_VERSION"
90+
echo "New version must increment major, minor, or patch number"
91+
echo "is_valid=false" >> $GITHUB_OUTPUT
92+
exit 1
93+
fi
94+
1995
build-and-push:
96+
needs: [validate-version]
97+
if: github.event_name != 'workflow_dispatch' || needs.validate-version.outputs.is_valid == 'true'
2098
runs-on: ubuntu-latest
2199
permissions:
22100
contents: read
@@ -64,7 +142,7 @@ jobs:
64142
org.opencontainers.image.revision=${{ github.sha }}
65143
66144
release:
67-
needs: build-and-push
145+
needs: [build-and-push]
68146
runs-on: ubuntu-latest
69147
permissions:
70148
contents: write
@@ -123,7 +201,7 @@ jobs:
123201
generate_release_notes: true
124202

125203
build-installer:
126-
needs: build-and-push
204+
needs: [build-and-push]
127205
runs-on: ubuntu-latest
128206
steps:
129207
- name: Checkout

0 commit comments

Comments
 (0)