From c3555d7c6e0496441edaaa6bdafb4141bb34945c Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Fri, 26 Jan 2024 13:09:39 +0100 Subject: [PATCH] Add the ability to specify the working directory --- .github/workflows/ci.yml | 35 +++++++++++++++++++++++++++++++++++ action.yml | 4 ++++ main.js | 5 +++-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e22b721..61a33d1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,6 +5,41 @@ 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: git@github.com:WyriHaximus/php-fake-php-version.git + 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/action.yml b/action.yml index afd33da..e16fabc 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);