Skip to content

Commit

Permalink
Merge pull request #70 from Cysharp/feature/validate_tag
Browse files Browse the repository at this point in the history
feat: add validate-tag action to check release tag is reverting to old or not
  • Loading branch information
guitarrapc authored Aug 20, 2024
2 parents 292589b + 34a9f6b commit 82ac041
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/create-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ on:
type: boolean

jobs:
validate:
uses: Cysharp/Actions/.github/workflows/validate-tag@main
with:
tag: ${{ inputs.tag }}
fail-on-invalid: true
secrets: inherit

create-release:
name: Create Release
env:
Expand Down
46 changes: 46 additions & 0 deletions .github/workflows/test-validate-tag.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Test validate-tag

on:
workflow_dispatch:
inputs:
tag:
description: "tag: GitHub Tag to release. ex) 1.1.0"
required: true
default: ""
pull_request:
branches: ["main"]
push:
branches: ["main"]

jobs:
validate:
if: ${{ github.actor != 'dependabot[bot]' }}
uses: ./.github/workflows/validate-tag.yaml
with:
tag: "1.10.0"
fail-on-invalid: true
secrets: inherit

validate2:
if: ${{ github.actor != 'dependabot[bot]' }}
uses: ./.github/workflows/validate-tag.yaml
with:
tag: "1.0.0"
fail-on-invalid: false
secrets: inherit

test:
needs: [validate, validate2]
runs-on: ubuntu-latest
timeout-minutes: 3
steps:
- name: Test
run: |
echo "DEBUG: validate: ${{ needs.validate.outputs.validated }} (${{ needs.validate.outputs.tag }})"
echo "DEBUG: validate2: ${{ needs.validate2.outputs.validated }} (${{ needs.validate2.outputs.tag }})"
echo -n "FACT: tag is newer then current release. "
if [[ "${{ needs.validate.outputs.validated }}" == "true" ]]; then echo "[O PASS]"; else echo "[X FAIL]" && exit 1; fi
echo -n "FACT: tag is old then current release. "
if [[ "${{ needs.validate2.outputs.validated }}" == "false" ]]; then echo "[O PASS]"; else echo "[X FAIL]" && exit 1; fi
8 changes: 8 additions & 0 deletions .github/workflows/update-packagejson.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ on:
value: ${{ jobs.update-packagejson.outputs.is-branch-created }}

jobs:
validate:
uses: Cysharp/Actions/.github/workflows/validate-tag@main
with:
tag: ${{ inputs.tag }}
fail-on-invalid: true
secrets: inherit

update-packagejson:
needs: [validate]
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
Expand Down
71 changes: 71 additions & 0 deletions .github/workflows/validate-tag.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: (R) Validate Tag

on:
workflow_call:
inputs:
fail-on-invalid:
description: "fail-on-invalid: allow old tag or not"
required: false
default: false
type: boolean
tag:
description: "tag: tag to validate"
required: true
type: string
outputs:
tag:
description: input tag
value: ${{ jobs.validate.outputs.tag }}
validated:
description: result of the validation is validated or not
value: ${{ jobs.validate.outputs.validated }}
workflow_dispatch:
inputs:
fail-on-invalid:
description: "fail-on-invalid: allow old tag or not"
required: false
default: false
type: boolean
tag:
description: "tag: tag to validate"
required: true
type: string

permissions:
contents: read
actions: read

jobs:
validate:
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
tag: ${{ steps.validate.outputs.tag }}
validated: ${{ steps.validate.outputs.validated }}
steps:
# Only check released tag, allow override draft and pre-release. Old to new sort by version number.
- name: Validate tag is not reverting
shell: bash
id: validate
run: |
echo "tag=${{ inputs.tag }}" | tee -a "$GITHUB_OUTPUT"
release_latest=$(gh release list --exclude-drafts --exclude-pre-releases --json tagName,isLatest | jq -c -r ".[] | select(.isLatest == true) | .tagName")
sorted_latest=$(echo -e "${release_latest}\n${{ inputs.tag }}" | sort -V | tail -n 1)
if [[ "$release_latest" == "" ]]; then
echo "There is not release tag."
echo "validated=true" | tee -a "$GITHUB_OUTPUT"
elif [[ "$sorted_latest" == "$release_latest" ]]; then
echo "Tag is reverting to old version. Please bump the version. tag: ${{ inputs.tag }}, latest: $release_latest"
echo "validated=false" | tee -a "$GITHUB_OUTPUT"
if [[ "${{ inputs.fail-on-invalid }}" == "true" ]]; then
exit 1
fi
else
echo "Great, tag is latest. tag: ${{ inputs.tag }}, latest: $release_latest"
echo "validated=true" | tee -a "$GITHUB_OUTPUT"
fi
env:
GH_REPO: ${{ github.repository }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,38 @@ jobs:
push-tag: false # recommend push tag on create-release job.
```

## validate-tag

> [See workflow](https://github.com/Cysharp/Actions/blob/main/.github/workflows/validate-tag.yaml)

Validate tag is newer than latest release tag.

**Sample usage**

```yaml
name: "Validate release tag"
on:
workflow_dispatch:
inputs:
tag:
description: "tag: git tag you want create. (sample 1.0.0)"
required: true
jobs:
validate:
uses: Cysharp/Actions/.github/workflows/validate-tag.yaml@main
with:
tag: ${{ inputs.tag }}
fail-on-invalid: true # exit 1 if tag is older than current release
test:
needs: [validate]
runs-on: ubuntu-latest
steps:
- run: echo "${{ needs.validate.outputs.validated }}" # true or false
```

# Actions

Expand Down
2 changes: 1 addition & 1 deletion Sandbox/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<VersionPrefix>1.0.188</VersionPrefix>
<VersionPrefix>1.0.194</VersionPrefix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Sandbox/Sandbox.Godot/addons/Foo/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
name="Sandbox.Godot"
description="Sample."
author="Cysharp"
version="1.0.188"
version="1.0.194"
language="C-sharp"
script="GodotPlugin.cs"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.unity.plugin.example",
"version": "1.0.188",
"version": "1.0.194",
"displayName": "Package Example Plugin",
"description": "This is an example package",
"unity": "2019.1",
Expand Down
2 changes: 1 addition & 1 deletion Sandbox/Sandbox.Unity/Assets/Plugins/Foo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.unity.example",
"version": "1.0.188",
"version": "1.0.194",
"displayName": "Package Example",
"description": "This is an example package",
"unity": "2019.1",
Expand Down
2 changes: 1 addition & 1 deletion Sandbox/VersionOutput/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.188
1.0.194

0 comments on commit 82ac041

Please sign in to comment.