diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e22b721..3bd273b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/README.md b/README.md index fad4d90..0c9fc66 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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" diff --git a/action.yml b/action.yml index a46be9a..7b0b2b9 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/main.js b/main.js index f06f28e..2a2913a 100644 --- a/main.js +++ b/main.js @@ -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) { @@ -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);