Release version (#3) #12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: | |
push: | |
workflow_dispatch: | |
env: | |
MAJOR_MINOR_PATCH: 1.0.0 | |
name: sample-git-tag-ci | |
jobs: | |
ci: | |
name: ci | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- name: Version suffix | |
id: version_suffix | |
run: | | |
if [[ ${{ github.ref }} == "refs/heads/${{ github.event.repository.default_branch }}" ]]; then | |
echo 'for default branch pipeline' | |
USE=false | |
SUFFIX='' | |
EXTENSION='' | |
else | |
echo 'for feature branch pipeline' | |
USE=true | |
SUFFIX=${GITHUB_REF##*/}.${{github.run_number}} | |
EXTENSION="-${SUFFIX}" | |
fi | |
echo 'use_version_suffix' $USE | |
echo 'version_suffix: ' $SUFFIX | |
echo "use_version_suffix=$USE" >> $GITHUB_OUTPUT | |
echo "version_suffix=$SUFFIX" >> $GITHUB_OUTPUT | |
echo "extension=$EXTENSION" >> $GITHUB_OUTPUT | |
- name: Semantic version | |
id: semantic_version | |
run: | | |
SEMANTIC_VERSION="${{ env.MAJOR_MINOR_PATCH }}" | |
SEMANTIC_VERSION="${SEMANTIC_VERSION}${{ steps.version_suffix.outputs.extension }}" | |
echo 'MAJOR_MINOR_PATCH: ' $MAJOR_MINOR_PATCH | |
echo 'SEMANTIC_VERSION: ' $SEMANTIC_VERSION | |
echo "semantic_version=$SEMANTIC_VERSION" >> $GITHUB_OUTPUT | |
echo "major_minor_patch=$MAJOR_MINOR_PATCH" >> $GITHUB_OUTPUT | |
- name: Create semantic versioning git tag | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
github.rest.git.createRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: "refs/tags/v${{ steps.semantic_version.outputs.semantic_version }}", | |
sha: context.sha | |
}) | |
- name: Delete Git Tags | |
uses: actions/github-script@v7 | |
if: github.ref == 'refs/heads/main' | |
with: | |
script: | | |
const existingFeatureBranches = (await github.rest.repos.listBranches({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
})).data.map(branch => branch.name); | |
console.log("Existing feature branches:", existingFeatureBranches); | |
const tags = await github.rest.git.listMatchingRefs({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: 'tags/v', | |
}); | |
for (const tag of tags.data) { | |
const tagName = tag.ref.replace('refs/tags/', ''); | |
console.log("Tag:", tagName); | |
const tagParts = /^v[0-9]*\.[0-9]*\.[0-9]*-(.*)\.([0-9]*)$/.exec(tagName); | |
if (tagParts) { | |
const featureBranchName = tagParts[1]; | |
console.log("Feature branch name:", featureBranchName); | |
if (!existingFeatureBranches.includes(`feature/${featureBranchName}`)) { | |
console.log(`Branch ${featureBranchName} does not exist, so deleting tag ${tagName}`); | |
await github.rest.git.deleteRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: `tags/${tagName}`, | |
}); | |
} else { | |
console.log(`Branch ${featureBranchName} exists, so not deleting tag ${tagName}`); | |
} | |
} | |
} |