Skip to content

Commit

Permalink
Merge pull request #51 from WyriHaximus/set-working-directory
Browse files Browse the repository at this point in the history
Add the ability to specify the working directory
  • Loading branch information
WyriHaximus authored Jan 28, 2024
2 parents 5e8388f + 752030f commit 487f53a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,42 @@ on:
- 'master'
pull_request:
jobs:
get-previous-tag-with-working-directory:
name: Test Get Previous Tag on ${{ matrix.os }} with Working Directory
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- windows-latest
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
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
- uses: actions/checkout@v4
with:
repository: WyriHaximus/php-fake-php-version
ref: master
path: tmp/some/other/path
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: ./
with:
workingDirecvtory: tmp/some/other/path
- run: |
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: Assert we got the tag
uses: therussiankid92/gat@v1
with:
assertion: should.equal
expected: v1
actual: ${{ steps.previoustag.outputs.tag }}
get-previous-tag:
name: Test Get Previous Tag on ${{ matrix.os }}
strategy:
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ GitHub Action that gets the latest tag from Git

By default, this action will fail if no tag can be found, however, it accepts a `fallback` tag that will be used when no
tag can be found. Keep in mind that when this action is used in a workflow that has no `.git` directory, it will still
fail, and the fallback tag isn't used.

It is also accepts a `prefix` string to query the tags based on it.
fail, and the fallback tag isn't used. It is also accepts a `prefix` string to query the tags based on it. And finally
it takes a `workingDirectory` if you need to look for a tag in an alternative path.

* `fallback`: `1.0.0`
* `prefix`: `tag-prefix`
* `workingDirectory`: `another/path/where/a/git/repo/is/checked/out`

## Output

Expand Down Expand Up @@ -50,6 +50,7 @@ jobs:
uses: "WyriHaximus/github-action-get-previous-tag@v1"
with:
fallback: 1.0.0 # Optional fallback tag to use when no tag can be found
#workingDirectory: another/path/where/a/git/repo/is/checked/out # Optional alternative working directory
- name: 'Get next minor version'
id: semvers
uses: "WyriHaximus/github-action-next-semvers@v1"
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ inputs:
prefix:
description: 'Prefix to query the tag by'
required: false
workingDirectory:
description: The directory to run this workflow in
default: ""
required: false
outputs:
tag:
description: 'Latest tag'
Expand Down
5 changes: 3 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { exec } = require('child_process');
const fs = require('fs');
const tagPrefix = `${process.env.INPUT_PREFIX || ''}*`;
const workingDirectory = process.env.INPUT_WORKINGDIRECTORY || null;

exec(`git for-each-ref --sort=-creatordate --count 1 --format="%(refname:short)" "refs/tags/${tagPrefix}"`, (err, tag, stderr) => {
exec(`git for-each-ref --sort=-creatordate --count 1 --format="%(refname:short)" "refs/tags/${tagPrefix}"`, {cwd: workingDirectory}, (err, tag, stderr) => {
tag = tag.trim();

if (err) {
Expand All @@ -19,7 +20,7 @@ exec(`git for-each-ref --sort=-creatordate --count 1 --format="%(refname:short)"
process.exit(0);
}

exec(`git log -1 --format=%at ${tag}`, (err, timestamp, stderr) => {
exec(`git log -1 --format=%at ${tag}`, {cwd: workingDirectory}, (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);
Expand Down

0 comments on commit 487f53a

Please sign in to comment.