diff --git a/README.md b/README.md index 482d63e..51a1b8d 100644 --- a/README.md +++ b/README.md @@ -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 # diff --git a/action.yml b/action.yml index 319b43c..2b28796 100644 --- a/action.yml +++ b/action.yml @@ -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" @@ -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 diff --git a/find-successful-workflow.js b/find-successful-workflow.js index 7bccaac..f2597bb 100644 --- a/find-successful-workflow.js +++ b/find-successful-workflow.js @@ -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') { @@ -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');