Skip to content

Commit 5a48e35

Browse files
ci: handle refs that are not valid semver (#78)
1 parent a70112e commit 5a48e35

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

build-client/action.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ runs:
2020
- uses: actions/checkout@v4
2121
with:
2222
path: "./client"
23-
# We need Node 16
2423
- uses: actions/setup-node@v4
2524
with:
26-
node-version: 18
25+
node-version: 20
2726
# Get the openapi.yaml file
2827
- run: |
2928
curl -H "Private-Token: ${GITLAB_API_TOKEN}" ${GITLAB_API}/${GITLAB_PROJECT}/repository/files/${OPENAPI_PATH}%2F${OPENAPI_FILE}/raw?ref=${CI_COMMIT_TAG} --output ${OPENAPI_FILE}

publish-client/action.yaml

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ runs:
1010
env:
1111
NPM_TOKEN: ${{ env.NODE_AUTH_TOKEN }}
1212
shell: bash
13+
- name: Install Semver CLI
14+
run: npm install -g semver
15+
shell: bash
1316
- name: Prepare package.json
1417
run: |
1518
jq -s '.[0] * .[1]' package.json package.release.json | tee dist/package.json
@@ -24,9 +27,52 @@ runs:
2427
- name: Publish
2528
run: |
2629
cd client/dist
27-
jq --arg tag "$CI_COMMIT_TAG" '.version = $tag' package.json > temp.json
30+
INPUT_TAG="$CI_COMMIT_TAG"
31+
PIPELINE_ID="$CI_PIPELINE_ID"
32+
OPENAPI_PATH="../../openapi.yaml" # Assumes openapi.yaml is at the root
33+
34+
echo "Input Tag: $INPUT_TAG"
35+
echo "Pipeline ID: $PIPELINE_ID"
36+
37+
# Check if openapi.yaml exists
38+
if [ ! -f "$OPENAPI_PATH" ]; then
39+
echo "Error: OpenAPI spec file not found at $OPENAPI_PATH!"
40+
exit 1
41+
fi
42+
43+
# Extract base version (e.g., '4.0') from openapi.yaml info.version
44+
OPENAPI_VERSION_LINE=$(grep -E "^\\s*version:\\s*'[0-9]+\\.[0-9]+'" "$OPENAPI_PATH")
45+
OPENAPI_VERSION=$(echo "$OPENAPI_VERSION_LINE" | sed -n "s/.*version:\\s*'\\(.*\\)'/\\1/p")
46+
47+
if [ -z "$OPENAPI_VERSION" ]; then
48+
echo "Error: Could not extract 'info.version' (e.g., 'X.Y') from $OPENAPI_PATH"
49+
exit 1
50+
fi
51+
52+
# Extract major and minor parts
53+
MAJOR=$(echo "$OPENAPI_VERSION" | cut -d. -f1)
54+
MINOR=$(echo "$OPENAPI_VERSION" | cut -d. -f2)
55+
56+
# Validate input tag using semver CLI. Exit code is 0 for valid semver.
57+
if semver "$INPUT_TAG" > /dev/null 2>&1; then
58+
# Input tag is SemVer compliant, use it directly
59+
VERSION="$INPUT_TAG"
60+
echo "Input tag '$INPUT_TAG' is valid SemVer. Using version '$VERSION'."
61+
else
62+
# Input tag is not SemVer, treat as branch/tag name
63+
echo "Input tag '$INPUT_TAG' is not valid SemVer. Generating pre-release version."
64+
# Sanitize the tag: replace non-alphanumeric/hyphen chars with hyphen
65+
SANITIZED_TAG=$(echo "$INPUT_TAG" | sed 's/[^a-zA-Z0-9-]/-/g')
66+
# Construct the version string: major.minor.0-tag-pipeline
67+
VERSION="${MAJOR}.${MINOR}.0-${SANITIZED_TAG}-${PIPELINE_ID}"
68+
echo "Generated version: $VERSION"
69+
fi
70+
71+
# Update package.json version
72+
jq --arg version "$VERSION" '.version = $version' package.json > temp.json
2873
mv temp.json package.json
29-
head package.json
74+
echo "Updated package.json:"
75+
head package.json # Keep for debugging output
3076
git config user.name "${GITHUB_ACTOR}"
3177
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
3278
npm publish

0 commit comments

Comments
 (0)