Skip to content

Commit

Permalink
feat: add working-directory option
Browse files Browse the repository at this point in the history
Add option `working-directory` to execute the script inside the specified working directory instead of the repository root.
  • Loading branch information
workflow committed Jan 4, 2023
1 parent 848fbbd commit c98e2f8
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 11 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,22 @@ jobs:
script: |
hello
command -v docker
test-local-flake-in-working-directory:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Nix
uses: cachix/install-nix-action@v18
with:
extra_nix_config: |
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- name: Test Bash Shell with Local and External Flakes
uses: ./
with:
flakes: .#cowsay,nixpkgs#docker
working-directory: test/working-directory
script: |
pwd
cowsay hello
command -v docker
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ jobs:

- `interpreter`: Interpreter to use in the nix shell shebang, defaults to `bash`. (This is passed to `nix run -c`, used to be `-i` in a nix shell shebang)

- `packages`: Comma-separated list of packages to pre-install in your shell. Defaults to just `bash`
- `packages`: Comma-separated list of packages to pre-install in your shell. Defaults to just `bash`. Cannot be used together with the `flakes` option.

- `flakes`: Comma-separated list of fully qualified flakes to pre-install in your shell. Use either `packages` or `flakes`. Cannot be used together with the `packages` option.

- `script`: The actual script to execute in your shell. Will be passed to the `interpreter`, which defaults to `bash`

- `working-directory`: Execute the script inside the specified working directory instead of the repository root. Example: `path/to/dir`

## FAQ: Passing a Github Token against Rate Limits

```yaml
Expand Down
20 changes: 15 additions & 5 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ function run(): void {
const packages: string = core.getInput('packages')
const flakes: string = core.getInput('flakes')
const script: string = core.getInput('script')
const workingDirectory: string = core.getInput('working-directory')

const nixWrapperPath = `${__dirname}/wrapper.sh`
const scriptPath = `${__dirname}/script.sh`
const nixWrapperPath = workingDirectory
? `./wrapper.sh`
: `${__dirname}/wrapper.sh`
const scriptPath = workingDirectory
? `./script.sh`
: `${__dirname}/script.sh`

const wrappedPackages = packages
.split(',')
Expand Down Expand Up @@ -53,10 +58,15 @@ set -euo pipefail
${script}
`

writeFileSync(nixWrapperPath, nixWrapper, {mode: 0o755})
writeFileSync(scriptPath, wrappedScript, {mode: 0o755})
writeFileSync(`${workingDirectory}/${nixWrapperPath}`, nixWrapper, {
mode: 0o755
})
writeFileSync(`${workingDirectory}/${scriptPath}`, wrappedScript, {
mode: 0o755
})

execFileSync(nixWrapperPath, {
cwd: workingDirectory || undefined,
stdio: 'inherit',
shell: 'bash'
})
Expand Down
11 changes: 11 additions & 0 deletions test/working-directory/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
description = "A very basic flake";

outputs = { self, nixpkgs }: {

packages.x86_64-linux.cowsay = nixpkgs.legacyPackages.x86_64-linux.cowsay;

packages.x86_64-linux.default = self.packages.x86_64-linux.cowsay;

};
}
1 change: 1 addition & 0 deletions test/working-directory/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo hello

0 comments on commit c98e2f8

Please sign in to comment.