Skip to content

Commit b684943

Browse files
authored
Add Ref and Commit outputs (#1180)
Signed-off-by: Luca Comellini <luca.com@gmail.com>
1 parent 2d7d9f7 commit b684943

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

.github/workflows/test.yml

+34
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,37 @@ jobs:
295295
uses: actions/checkout@v4.1.6
296296
with:
297297
path: localClone
298+
299+
test-output:
300+
runs-on: ubuntu-latest
301+
steps:
302+
# Clone this repo
303+
- name: Checkout
304+
uses: actions/checkout@v4.1.6
305+
306+
# Basic checkout using git
307+
- name: Checkout basic
308+
id: checkout
309+
uses: ./
310+
with:
311+
ref: test-data/v2/basic
312+
313+
# Verify output
314+
- name: Verify output
315+
run: |
316+
echo "Commit: ${{ steps.checkout.outputs.commit }}"
317+
echo "Ref: ${{ steps.checkout.outputs.ref }}"
318+
319+
if [ "${{ steps.checkout.outputs.ref }}" != "test-data/v2/basic" ]; then
320+
echo "Expected ref to be test-data/v2/basic"
321+
exit 1
322+
fi
323+
324+
if [ "${{ steps.checkout.outputs.commit }}" != "82f71901cf8c021332310dcc8cdba84c4193ff5d" ]; then
325+
echo "Expected commit to be 82f71901cf8c021332310dcc8cdba84c4193ff5d"
326+
exit 1
327+
fi
328+
329+
# needed to make checkout post cleanup succeed
330+
- name: Fix Checkout
331+
uses: actions/checkout@v4.1.6

action.yml

+5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ inputs:
9898
github-server-url:
9999
description: The base URL for the GitHub instance that you are trying to clone from, will use environment defaults to fetch from the same instance that the workflow is running from unless specified. Example URLs are https://github.com or https://my-ghes-server.example.com
100100
required: false
101+
outputs:
102+
ref:
103+
description: 'The branch, tag or SHA that was checked out'
104+
commit:
105+
description: 'The commit SHA that was checked out'
101106
runs:
102107
using: node20
103108
main: dist/index.js

dist/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,8 @@ function getSource(settings) {
13551355
// Get commit information
13561356
const commitInfo = yield git.log1();
13571357
// Log commit sha
1358-
yield git.log1("--format='%H'");
1358+
const commitSHA = yield git.log1('--format=%H');
1359+
core.setOutput('commit', commitSHA.trim());
13591360
// Check for incorrect pull request merge commit
13601361
yield refHelper.checkCommitInfo(settings.authToken, commitInfo, settings.repositoryOwner, settings.repositoryName, settings.ref, settings.commit, settings.githubServerUrl);
13611362
}
@@ -1897,6 +1898,7 @@ function run() {
18971898
coreCommand.issueCommand('add-matcher', {}, path.join(__dirname, 'problem-matcher.json'));
18981899
// Get sources
18991900
yield gitSourceProvider.getSource(sourceSettings);
1901+
core.setOutput('ref', sourceSettings.ref);
19001902
}
19011903
finally {
19021904
// Unregister problem matcher

src/git-source-provider.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
261261
const commitInfo = await git.log1()
262262

263263
// Log commit sha
264-
await git.log1("--format='%H'")
264+
const commitSHA = await git.log1('--format=%H')
265+
core.setOutput('commit', commitSHA.trim())
265266

266267
// Check for incorrect pull request merge commit
267268
await refHelper.checkCommitInfo(

src/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ async function run(): Promise<void> {
1919

2020
// Get sources
2121
await gitSourceProvider.getSource(sourceSettings)
22+
core.setOutput('ref', sourceSettings.ref)
2223
} finally {
2324
// Unregister problem matcher
2425
coreCommand.issueCommand('remove-matcher', {owner: 'checkout-git'}, '')

0 commit comments

Comments
 (0)