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

better way to set job level working directory? #1185

Open
geekflyer opened this issue Jul 5, 2023 · 2 comments
Open

better way to set job level working directory? #1185

geekflyer opened this issue Jul 5, 2023 · 2 comments
Labels

Comments

@geekflyer
Copy link

geekflyer commented Jul 5, 2023

Description

I'm running some tasks in a monorepo where all the relevant code for the job is in the ui subdirectory. Is there a more elegant / more DRY way to change the working directory to a subdirectory of the repo than this?

load("github.com/cirrus-modules/helpers", "cache", "container", "script", "task")

def main(ctx):
    return [
        task(
            name = "ts-checks",
            instance = container("node:18"),
            instructions = [
                script("corepack", "cd ui && corepack enable"),
                cache(
                    name = "node_modules",
                    folder = "ui/node_modules",
                    fingerprint_script = "cat ui/pnpm-lock.yaml",
                    populate_script = "cd ui && pnpm install --frozen-lockfile",
                ),
                script("lint", "cd ui && pnpm lint"),
                script("build", "cd ui && pnpm build"),
            ],
        ),
    ]
@fkorotkov
Copy link
Contributor

After thinking about it, it seems it's possible to override CIRRUS_WORKING_DIR via CIRRUS_ENV file:

task:
  container:
    image: alpine:latest
  change_dir_script:
    - mkdir -p foo
    - cd foo
    - echo "Hello, Bar!" >> bar.txt
    - echo "CIRRUS_WORKING_DIR=$CIRRUS_WORKING_DIR/foo" >> $CIRRUS_ENV
  print_script:
    - cat bar.txt

The example above will override the working directory in change_dir_script and every following instruction will respect it.

This behaviour was not intended when we added CIRRUS_ENV but it works. I've added an integration test to make sure the behaviour from the example above won't break.

@geekflyer
Copy link
Author

geekflyer commented Jul 5, 2023

cool thanks - that seems to work!
I wrote a small starlark utility function to make this less verbose if anyone comes by this:

def change_working_dir(path):
    return script("change working directory to {}".format(path), 'echo "CIRRUS_WORKING_DIR=$CIRRUS_WORKING_DIR/{}" >> $CIRRUS_ENV'.format(path))

can be used like this:

task(
   instructions = [
       change_working_dir("my-sub-dir"),
       script("run stuff in sub dir", "echo stuff"),
   ]
)

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

No branches or pull requests

2 participants