Skip to content

Commit

Permalink
feat: add support for changing the working directory (#27)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kitsune7 authored Mar 23, 2022
1 parent b34af05 commit a8fc356
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ jobs:
# Default: false
error-on-no-successful-workflow: ''

# 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.
#
# Default: .
working-directory: ''

# The ID of the github action workflow to check for successful run or the name of the file name containing the workflow.
# E.g. 'ci.yml'. If not provided, current workflow id will be used
#
Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ inputs:
last-successful-event:
description: "The type of event to check for the last successful commit corresponding to that workflow-id, E.g. push, pull-request, release etc"
default: "push"
working-directory:
description: "The directory where your repository is located"
default: "."
workflow-id:
description: "The ID of the workflow to track or name of the file name. E.g. ci.yml. Defaults to current workflow"

Expand All @@ -34,7 +37,7 @@ runs:
- name: Set base and head SHAs used for nx affected
id: setSHAs
shell: bash
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 }}
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 }}

- name: Log base and head SHAs used for nx affected
shell: bash
Expand Down
17 changes: 15 additions & 2 deletions find-successful-workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@ const { Octokit } = require("@octokit/action");
const core = require("@actions/core");
const github = require('@actions/github');
const { execSync } = require('child_process');
const { existsSync } = require('fs');
const { join } = require('path');

const { runId, repo: { repo, owner }, eventName } = github.context;
process.env.GITHUB_TOKEN = process.argv[2];
const mainBranchName = process.argv[3];
const errorOnNoSuccessfulWorkflow = process.argv[4];
const lastSuccessfulEvent = process.argv[5];
const workflowId = process.argv[6];
const workingDirectory = process.argv[6];
const workflowId = process.argv[7];
const defaultWorkingDirectory = '.';

let BASE_SHA;
(async () => {
if (workingDirectory !== defaultWorkingDirectory) {
if (existsSync(workingDirectory)) {
process.chdir(join(__dirname, workingDirectory));
} else {
process.stdout.write('\n');
process.stdout.write(`WARNING: Working directory '${workingDirectory}' doesn't exist.\n`);
}
}

const HEAD_SHA = execSync(`git rev-parse HEAD`, { encoding: 'utf-8' });

if (eventName === 'pull_request') {
Expand All @@ -33,7 +46,7 @@ let BASE_SHA;
process.stdout.write(`WARNING: Unable to find a successful workflow run on 'origin/${mainBranchName}'\n`);
process.stdout.write(`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`);
process.stdout.write('\n');
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`);
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`);

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

0 comments on commit a8fc356

Please sign in to comment.