Skip to content

Commit a8fc356

Browse files
authored
feat: add support for changing the working directory (#27)
* Add working-directory to input parameters (issue #13) * Add error handling for working directory parameter * Update input order and add documentation for the `working-directory` option
1 parent b34af05 commit a8fc356

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ jobs:
8080
# Default: false
8181
error-on-no-successful-workflow: ''
8282

83+
# The path where your repository is. This is only required for cases where the repository code is checked out or moved to a specific path.
84+
#
85+
# Default: .
86+
working-directory: ''
87+
8388
# The ID of the github action workflow to check for successful run or the name of the file name containing the workflow.
8489
# E.g. 'ci.yml'. If not provided, current workflow id will be used
8590
#

action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ inputs:
1414
last-successful-event:
1515
description: "The type of event to check for the last successful commit corresponding to that workflow-id, E.g. push, pull-request, release etc"
1616
default: "push"
17+
working-directory:
18+
description: "The directory where your repository is located"
19+
default: "."
1720
workflow-id:
1821
description: "The ID of the workflow to track or name of the file name. E.g. ci.yml. Defaults to current workflow"
1922

@@ -34,7 +37,7 @@ runs:
3437
- name: Set base and head SHAs used for nx affected
3538
id: setSHAs
3639
shell: bash
37-
run: node $GITHUB_ACTION_PATH/dist/index.js ${{ github.token }} ${{ inputs.main-branch-name }} ${{ inputs.error-on-no-successful-workflow }} ${{ inputs.last-successful-event }} ${{ inputs.workflow-id }}
40+
run: node $GITHUB_ACTION_PATH/dist/index.js ${{ github.token }} ${{ inputs.main-branch-name }} ${{ inputs.error-on-no-successful-workflow }} ${{ inputs.last-successful-event }} ${{ inputs.working-directory }} ${{ inputs.workflow-id }}
3841

3942
- name: Log base and head SHAs used for nx affected
4043
shell: bash

find-successful-workflow.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@ const { Octokit } = require("@octokit/action");
22
const core = require("@actions/core");
33
const github = require('@actions/github');
44
const { execSync } = require('child_process');
5+
const { existsSync } = require('fs');
6+
const { join } = require('path');
57

68
const { runId, repo: { repo, owner }, eventName } = github.context;
79
process.env.GITHUB_TOKEN = process.argv[2];
810
const mainBranchName = process.argv[3];
911
const errorOnNoSuccessfulWorkflow = process.argv[4];
1012
const lastSuccessfulEvent = process.argv[5];
11-
const workflowId = process.argv[6];
13+
const workingDirectory = process.argv[6];
14+
const workflowId = process.argv[7];
15+
const defaultWorkingDirectory = '.';
1216

1317
let BASE_SHA;
1418
(async () => {
19+
if (workingDirectory !== defaultWorkingDirectory) {
20+
if (existsSync(workingDirectory)) {
21+
process.chdir(join(__dirname, workingDirectory));
22+
} else {
23+
process.stdout.write('\n');
24+
process.stdout.write(`WARNING: Working directory '${workingDirectory}' doesn't exist.\n`);
25+
}
26+
}
27+
1528
const HEAD_SHA = execSync(`git rev-parse HEAD`, { encoding: 'utf-8' });
1629

1730
if (eventName === 'pull_request') {
@@ -33,7 +46,7 @@ let BASE_SHA;
3346
process.stdout.write(`WARNING: Unable to find a successful workflow run on 'origin/${mainBranchName}'\n`);
3447
process.stdout.write(`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`);
3548
process.stdout.write('\n');
36-
process.stdout.write(`NOTE: You can instead make this a hard error by settting 'error-on-no-successful-workflow' on the action in your workflow.\n`);
49+
process.stdout.write(`NOTE: You can instead make this a hard error by setting 'error-on-no-successful-workflow' on the action in your workflow.\n`);
3750

3851
BASE_SHA = execSync(`git rev-parse HEAD~1`, { encoding: 'utf-8' });
3952
core.setOutput('noPreviousBuild', 'true');

0 commit comments

Comments
 (0)