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

Get latest tag (not tag associated with latest commit) #32

Merged
merged 17 commits into from
Oct 24, 2022
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
14 changes: 11 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ jobs:
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Required due to the way Git works, without it this action won't be able to find any or the correct tags
- name: 'Get Previous tag'
id: previoustag
uses: ./
- run: |
echo "${{ steps.previoustag.outputs.tag }}"
echo "Tag: ${{ steps.previoustag.outputs.tag }}"
echo "Timestamp: ${{ steps.previoustag.outputs.timestamp }}"
test -n "${{ steps.previoustag.outputs.tag }}"
test -n "${{ steps.previoustag.outputs.timestamp }}"
- name: Remove tags
uses: JesseTG/rm@v1.0.2
with:
Expand All @@ -32,4 +37,7 @@ jobs:
with:
fallback: v1.0.0
- run: |
echo "${{ steps.previoustagwithfallback.outputs.tag }}"
echo "Tag: ${{ steps.previoustagwithfallback.outputs.tag }}"
echo "Timestamp: ${{ steps.previoustagwithfallback.outputs.timestamp }}"
test -n "${{ steps.previoustagwithfallback.outputs.tag }}"
test -n "${{ steps.previoustagwithfallback.outputs.timestamp }}"
5 changes: 3 additions & 2 deletions .github/workflows/craft-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ jobs:
name: Wait for status checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: sleep 13
- name: 'Wait for status checks'
id: waitforstatuschecks
Expand Down Expand Up @@ -49,7 +48,9 @@ jobs:
- generate-changelog
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Required due to the way Git works, without it this action won't be able to find any or the correct tags
- name: Create release/${{ env.MILESTONE }} branch
run: git checkout -b release/${{ env.MILESTONE }} ${GITHUB_SHA}
- run: echo -e "${CHANGELOG}" > release-${{ env.MILESTONE }}-changelog.md
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/set-milestone-on-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ jobs:
set-milestone:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Required due to the way Git works, without it this action won't be able to find any or the correct tags
if: github.event.pull_request.milestone == null
- name: 'Get Previous tag'
if: github.event.pull_request.milestone == null
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Get previous tag


[![Continuous Integration](https://github.com/WyriHaximus/github-action-get-previous-tag/actions/workflows/ci.yml/badge.svg)](https://github.com/WyriHaximus/github-action-get-previous-tag/actions/workflows/ci.yml)
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/WyriHaximus/github-action-get-previous-tag?logo=github&sort=semver)](https://github.com/WyriHaximus/github-action-get-previous-tag/releases)

Expand Down Expand Up @@ -38,7 +39,7 @@ name: Generate
jobs:
generate:
steps:
- uses: actions/checkout@v2.2.0
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Required due to the way Git works, without it this action won't be able to find any or the correct tags
- name: 'Get Previous tag'
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ inputs:
outputs:
tag:
description: 'Latest tag'
timestamp:
description: 'Latest tag timestamp'
runs:
using: 'node12'
main: 'main.js'
49 changes: 19 additions & 30 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
const { exec } = require('child_process');
exec('git rev-list --tags --max-count=1', (err, rev, stderr) => {
exec(`git for-each-ref --sort=-taggerdate --count 1 --format="%(refname:short)" "refs/tags/*"`, (err, tag, stderr) => {
if (err) {
console.log('\x1b[33m%s\x1b[0m', 'Could not find any revisions because: ');
console.log('\x1b[33m%s\x1b[0m', 'Could not find any tags because: ');
console.log('\x1b[31m%s\x1b[0m', stderr);
process.exit(1);
} else if(tag === "") {
let timestamp = Math.floor(new Date().getTime() / 1000);
console.log('\x1b[33m%s\x1b[0m', 'Falling back to default tag');
console.log('\x1b[32m%s\x1b[0m', `Found tag: ${process.env.INPUT_FALLBACK}`);
console.log('\x1b[32m%s\x1b[0m', `Found timestamp: ${timestamp}`);
console.log(`::set-output name=tag::${process.env.INPUT_FALLBACK}`);
console.log(`::set-output name=timestamp::${timestamp}`);
process.exit(0);
}

rev = rev.trim()
tag = tag.trim()

exec(`git describe --tags ${rev}`, (err, tag, stderr) => {
exec(`git log -1 --format=%at ${tag}`, (err, timestamp, stderr) => {
if (err) {
console.log('\x1b[33m%s\x1b[0m', 'Could not find any tags because: ');
console.log('\x1b[33m%s\x1b[0m', 'Could not find any timestamp because: ');
console.log('\x1b[31m%s\x1b[0m', stderr);
if (process.env.INPUT_FALLBACK) {
let timestamp = Math.floor(new Date().getTime() / 1000);
console.log('\x1b[33m%s\x1b[0m', 'Falling back to default tag');
console.log('\x1b[32m%s\x1b[0m', `Found tag: ${process.env.INPUT_FALLBACK}`);
console.log('\x1b[32m%s\x1b[0m', `Found timestamp: ${timestamp}`);
console.log(`::set-output name=tag::${process.env.INPUT_FALLBACK}`);
console.log(`::set-output name=timestamp::${timestamp}`);
process.exit(0);
}
process.exit(1);
}

tag = tag.trim()
timestamp = timestamp.trim()

exec(`git log -1 --format=%at ${tag}`, (err, timestamp, stderr) => {
if (err) {
console.log('\x1b[33m%s\x1b[0m', 'Could not find any timestamp because: ');
console.log('\x1b[31m%s\x1b[0m', stderr);
process.exit(1);
}

timestamp = timestamp.trim()

console.log('\x1b[32m%s\x1b[0m', `Found tag: ${tag}`);
console.log('\x1b[32m%s\x1b[0m', `Found timestamp: ${timestamp}`);
console.log(`::set-output name=tag::${tag}`);
console.log(`::set-output name=timestamp::${timestamp}`);
process.exit(0);
});
console.log('\x1b[32m%s\x1b[0m', `Found tag: ${tag}`);
console.log('\x1b[32m%s\x1b[0m', `Found timestamp: ${timestamp}`);
console.log(`::set-output name=tag::${tag}`);
console.log(`::set-output name=timestamp::${timestamp}`);
process.exit(0);
});
});