Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run in a specific directory #412

Closed
kaykhan opened this issue Aug 16, 2023 · 5 comments
Closed

How to run in a specific directory #412

kaykhan opened this issue Aug 16, 2023 · 5 comments
Labels
question A question on how to use this action

Comments

@kaykhan
Copy link

kaykhan commented Aug 16, 2023

I have a workflow which makes use of the working-directory. https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#defaultsrun although this does not work with uses.

defaults:
 run:
  working-directory: applications/prod/kafka-ui
...

Below i use github-script.

    - name: Terraform Comment
      uses: actions/github-script@v6
      env:
        TFPLAN_IDENTIFIER: "${{ steps.plan.outputs.tfplan_identifier}}"
        PLAN_OUTPUT: "${{ steps.plan.outputs.stdout }}"
        PLAN_ERROR: "${{ steps.plan.outputs.stderr }}"
      with:
        script: |

How do i force github-script to run in a specific directory.

@joshmgross joshmgross added the question A question on how to use this action label Sep 22, 2023
@joshmgross
Copy link
Member

You could try using the Node process.chdir to change the current working directory within your script - https://nodejs.org/api/process.html#process_process_chdir_directory

@asilverman
Copy link

@joshmgross, thank you so much for your reply.

I would like to follow up with a thought and request. Your suggestion serves as a great stopgap. However, doing so couples the script to the specific project file-structure.

As a consequence, any changes in the project file-structure would result in a CI failure. Such a failure would be difficult to diagnose and remedy. In addition, it breaks consistency with the known pattern for handling this kind of scenario (see defaults.run).

jobs:
  job1:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
        working-directory: ./scripts # <- sets the directory from which to run the bash script

I think that given that this action is published by github.com, there is an implicit expectation that it would remain consistent with the conventions and maintain consistency with prior art.

I suggest if possible, to have the script offer an option working-directory that can be used to specify the directory from which to start the script in order to increase the usability and flexibility while maintaining consistency with prior art.

Would it be costly on your end to provide a parameter working-directory similar to the example below whose default value is sourced from defaults.run.working-directory if specified?

- name: Terraform Comment
      uses: actions/github-script@v6
      env:
        TFPLAN_IDENTIFIER: "${{ steps.plan.outputs.tfplan_identifier}}"
        PLAN_OUTPUT: "${{ steps.plan.outputs.stdout }}"
        PLAN_ERROR: "${{ steps.plan.outputs.stderr }}"
      with:
        working-directory: './some-dir' # defaults to  `defaults.run.working-directory` if unspecified.
        script: |
            ...

@kaykhan
Copy link
Author

kaykhan commented Oct 5, 2023

Just thought id leave a solution with what i did as im pretty happy with it. Since working-directory does not work with uses. I rely on an enviornment variable now. I do use working-directory wherever possible else where

name: terraform-ci

on:
  workflow_call:
    inputs:
      TF_ROOT:
        required: true
        type: string
      TF_APP_NAME:
        required: true
        type: string
        
    - name: Terraform Comment
      uses: actions/github-script@v6
      env:
        TFPLAN_IDENTIFIER: "${{ steps.plan.outputs.tfplan_identifier}}"
        PLAN_OUTPUT: "${{ steps.plan.outputs.stdout }}"
        PLAN_ERROR: "${{ steps.plan.outputs.stderr }}"
        TF_ROOT: "${{ inputs.TF_ROOT }}"
        TF_APP_NAME: "${{ inputs.TF_APP_NAME }}"
        
      with:
        script: |
          let plan;
          try {
            const fs = require('fs');
            plan = fs.readFileSync(`${process.env.TF_ROOT}/${process.env.TFPLAN_IDENTIFIER}.log`, { encoding: 'utf8', flag: 'r' });
           ...

@asilverman
Copy link

You could try using the Node process.chdir to change the current working directory within your script - https://nodejs.org/api/process.html#process_process_chdir_directory

@joshmgross I used the approach you mentioned and it does work, I appreciate the help ❤️

@joshmgross
Copy link
Member

Glad to hear it @asilverman.
I filed #426 to track adding a working-directory input to this action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A question on how to use this action
Projects
None yet
Development

No branches or pull requests

3 participants