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

[Question] Using direnv in GitHub Actions #903

Closed
BeyondEvil opened this issue Mar 3, 2022 · 9 comments · Fixed by #910
Closed

[Question] Using direnv in GitHub Actions #903

BeyondEvil opened this issue Mar 3, 2022 · 9 comments · Fixed by #910
Labels

Comments

@BeyondEvil
Copy link

I spent quite some time getting this to work, so wanted to share it here. Maybe (after som revisions) it can be added to the Wiki.

We wanted to use direnv in GitHub Actions, without using a third-party action.

Here's the result:

  - name: Install direnv
    run: |
      mkdir -p ~/.local/bin
      curl -sfL https://direnv.net/install.sh | bash
      direnv allow

  - name: Run Tool
    run: |
      eval "$(direnv export bash)"
     * run some tool installed by direnv *

Note that we're running this in a self-hosted Ubuntu runner. YMMV

Unfortunately you have to duplicate eval "$(direnv export bash)" for every step.

So a question is: Is there a better way of solving that? Maybe involving $GITHUB_ENV?

@BeyondEvil BeyondEvil added the Bug label Mar 3, 2022
@zimbatm
Copy link
Member

zimbatm commented Mar 3, 2022

Nice! That looks like a good addition for the wiki indeed.

Now to answer your question:

For single commands, you can use direnv exec . <command> <...args>. Eg: direnv exec . npm run build. But that's not great if your step has multiple commands in the step.

I like your idea of using the $GITHUB_ENV. It's possible that adding direnv exec . /usr/bin/env > $GITHUB_ENV the the install would be enough. You will probably encounter edge-cases if the env values contain \n or any other special characters. The best would be to add a new shell type that exports in a format that is compatible with $GITHUB_ENV, including the escaping. Then you could run direnv export gha > $GITHUB_ENV.

See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings

@BeyondEvil
Copy link
Author

Then you could run direnv export gha > $GITHUB_ENV

Ooh, I like that!

I can take a stab at a PR, if you want? I've only dabbled a little bit in GO, so might require some handholding. 🙈

@mmlb
Copy link
Contributor

mmlb commented Mar 21, 2022

After messing around with a way to get python packages to show up in my attempts to use nix-env instead of nix-shell I realized what i really wanted was something like direnv export github-actions that can handle the multi-line strings github-actions style. @BeyondEvil My go is pretty good and I've got time this week so I'll be looking into doing this, no sense in doing duplicate effort.

@BeyondEvil
Copy link
Author

After messing around with a way to get python packages to show up in my attempts to use nix-env instead of nix-shell I realized what i really wanted was something like direnv export github-actions that can handle the multi-line strings github-actions style. @BeyondEvil My go is pretty good and I've got time this week so I'll be looking into doing this, no sense in doing duplicate effort.

Oh, that's awesome @mmlb !

lmk if you need anything from me!

@mmlb
Copy link
Contributor

mmlb commented Mar 22, 2022

Hey @BeyondEvil I've got #910 up, give it a spin with your GHA setup? I've given it a go on the PR I was working on before, https://github.com/mistifyio/go-zfs/runs/5649577377 and its looking good for my use.

@BeyondEvil
Copy link
Author

Happy to test it out @mmlb !

Umm... how do I use your branch tho? 😂

Here's what I'm currently doing:

      - name: Install direnv
        run: |
          mkdir -p ~/.local/bin
          curl -sfL https://direnv.net/install.sh | bash
          direnv allow

@mmlb
Copy link
Contributor

mmlb commented Mar 23, 2022

I can put up the binary somewhere that you can fetch instead, but thats not a good idea imo. You'd have to build it and commit locally (like I did in my PR, temporarily and not going to be merged) or build my branch in your ci run. Happy to help with either case.

@BeyondEvil
Copy link
Author

Hey, sorry for the late response @mmlb

Yeah, I'll pull your branch and build it - I think that's the path of least resistance. I'l have to do it tomorrow tho.

@BeyondEvil
Copy link
Author

Tested and works like a charm @mmlb

Thank you!

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

Successfully merging a pull request may close this issue.

3 participants