Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup outdated git tags #2

Merged
merged 3 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,42 @@ jobs:
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}`);
}
}
}
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# sample-git-tag
# sample-git-tag

Sample application how to cleanup outdated Git tags.

See my [article](https://medium.com/@kinneko-de/7d17fa85c175) how to create this by yourself.
25 changes: 25 additions & 0 deletions manual.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

git fetch --tags --prune --prune-tags

existingfeaturebranches=$(git branch -r | sed 's/^ *//;s/ *$//' | egrep -v "^main")
echo "Existing feature branches: $existingfeaturebranches"

for tag in $(git tag --list 'v[0-9]*\.[0-9]*\.[0-9]*-*')
do
echo "Tag: $tag"
# regex to match the tag name: pattern is v1.2.3-featurename.buildnumber
[[ $tag =~ ^v[0-9]*\.[0-9]*\.[0-9]*-(.*)\.([0-9]*)$ ]]
featurebranchname=${BASH_REMATCH[1]}
echo "Feature branch name: $featurebranchname"
if [[ $existingfeaturebranches =~ "feature/$featurebranchname" ]]
then
echo "Branch $featurebranchname exists, so not deleting tag $tag"
else
echo "Branch $featurebranchname does not exist, so deleting tag $tag"
git push origin --delete $tag
git tag -d $tag
fi
done

read -p "Press any key to end ..."