nektos / act

Run your GitHub Actions locally 🚀
https://nektosact.com
MIT License
54.38k stars 1.36k forks source link

Allow to work with local actions outside of the repository we run the workflows from #1579

Open Porkepix opened 1 year ago

Porkepix commented 1 year ago

Act version

act version 0.2.34

Feature description

It would be a nice feature, when developing a new action or more, several actions (for example to migrate from CircleCI's orbs to GitHub's actions) to be able to have like a "path" (I mean, the same way you'd add a location to your $PATH for your shell) act would be looking at/mounting, so that when fetching actions it could also get them from here, to allow for actions development offline and not only the workflows themselves.

Note that I might have missed the way to do so, but the best I could find was to copy and paste actions within the repo itself and change to call from the org/repo syntax to a local one

trash-anger commented 1 year ago

I was about to create quite a similar feature request!

I'm working in a company which have quite a flexible remote work policy leading developers to travel a lot by flight. (which is currently my case)

I'm a new user of act and when I tried to use act during my previous flight I just got this message: FATA[0000] dial udp 8.8.8.8:80: connect: network is unreachable

So landing I've been wondering how we could work offline an easy way, it could be nice to implement a caching solution allowing to fetch pipeline dependencies while running it. Then running it again would first try to reach online first (and update the cache as a result) and offline, would rely on the cache.

I'm currently adapting the pipelines of my company to get a custom behaviour depending on the GITHUB_ACTIONS var to allow anyone to build locally, and deploy the current component to a local k8s cluster.

I'm thinking out loud:

Another interesting thing would be to implement a similar feature than the ".github" repository. I haven't check if it's possible to make it work with a simlink added to the gitignore tho.

@Porkepix , maybe you could use the workflow reuse function if it works and if you can refactor your pipeline.

Porkepix commented 1 year ago

I was about to create quite a similar feature request!

I'm working in a company which have quite a flexible remote work policy leading developers to travel a lot by flight. (which is currently my case)

I'm a new user of act and when I tried to use act during my previous flight I just got this message: FATA[0000] dial udp 8.8.8.8:80: connect: network is unreachable

So landing I've been wondering how we could work offline an easy way, it could be nice to implement a caching solution allowing to fetch pipeline dependencies while running it. Then running it again would first try to reach online first (and update the cache as a result) and offline, would rely on the cache.

Act actually already cache actions, see ~/.cache/act. The issue here is rather that it tries to reach out Google's Public DNS which is curious to say the least. I noticed it too but didn't opened an issue, would definitely be interesting to know why it does that. And if every resources are available locally then, yes, I believe it should work 100% offline.

What I had in mind was rather to have a clone of the actions repos and to provide them to act like you would with libs given to a compiler, or bineries in your $PATH: a local place to look for those actions.

@Porkepix , maybe you could use the workflow reuse function if it works and if you can refactor your pipeline.

I don't understand what you had in mind here.

ChristopherHX commented 1 year ago

@trash-anger Please create a new issue about FATA[0000] dial udp 8.8.8.8:80: connect: network is unreachable

This function should just return 127.0.0.1 on a network error and the artifact server might not work correctly. https://github.com/nektos/act/blob/c378a7d28b5c20de9fbb4456544329d04ffb62a4/pkg/common/outbound_ip.go#L14

trash-anger commented 1 year ago

I'm boarding, I'll do that tonight!

cardinalby commented 1 year ago

Act is great for testing Workflows. Developing individual actions I also tried to use act, but found that it's better to have integration tests. Especially for JavaScript actions where you can run your action locally offline, pass intputs, env, etc. and debug it, test its outputs, examine all commands issued by the action, etc.

I use github-action-ts-run-api for that.

mcascone commented 1 year ago

All I want is to be able to develop my actions locally, separate from the repo with the workflow file that calls them.

Specifically, I want to be able to use a local path in the uses: part of a workflow:

Existing behavior

jobs:
  Deploy:
    steps:
      - uses: 'myOrg/my-action@my-branch'

Desired functionality

jobs:
  Deploy:
    steps:
      - uses: '/path/to/local/my-action@my-branch'

Going by the output, there would need to be a trigger or flag to not tack the https://github.com onto the path:

 git clone 'https://github.com/~/myOrg' # ref=my-branch
[My-Action/Deploy] Unable to clone https://github.com/~/myOrg refs/heads/my-branch: repository not found
mverkerk-godaddy commented 1 year ago

I'm struggling with this too - I'm building a library of actions that can be shared across repos within our github org and when I'm testing this library locally with ACT, the uses: pieces should point to .[/path] where otherwise, they should point to {org}/{repo}[/path]@ref ...

Currently, I have to (temporarily) update everything from {org}/{repo}[/path]@ref to .[/path], run ACT, and change everything back before I commit/push (easy to forget). I have written a bash script that takes care of this but that's not ideal. I'll include the script at the bottom of this message...

In my testing, I have tried setting an ENV variable and/or using env.ACT but unfortunately uses: does not support variables (as far as I know)

This example:

- uses: ${{ env.ACT && './.github/actions/my-action' || 'org/repo/.github/actions/my-action@main' }}
  with:
    some-input: input

... failed with this error:

Error: Expected format {org}/{repo}[/path]@ref. Actual '${{ env.ACT && './.github/actions/my-action' || 'my-org/my-repo/.github/actions/my-action@main' }}' Input string was not in a correct format

would be great if there was a flag like --replace-ghe-path-with-local {org}/{repo} that would make this switch for us!

btw: thanks for all the great work! really enjoying this tool! 👍🏼

Here's my act_path_replace.sh script:

#!/usr/bin/env bash
target=${1}

if [ "$target" == "--to-remote" ]; then
  echo "Updating [action.yml] files to point to [org/repo/.github/actions]"
  FILES=$(find . -name "action.yml" -type f -exec grep -l "uses:.*\.\/\.github\/actions\/" {} \;)
  for file in $FILES; do
    sed -i 's|./.github/actions|org/repo/.github/actions|g' "$file"
    sed -i '/uses:.*\.github\/actions\//!b;/@v2/b;s/$/@v2/' "$file"
  done
elif [ "$target" == "--to-local" ]; then
  echo "Updating [action.yml] files to point to [./.github/actions]"
  FILES=$(find . -name "action.yml" -type f -exec grep -l "uses:.*org\/repo\/\.github\/actions\/" {} \;)
  for file in $FILES; do
    sed -i 's|org/repo|.|g' "$file"
    sed -i 's|@v2$||g' "$file"
  done
else
  echo "Please specify a target: local or remote"
  exit 1
fi
ChristopherHX commented 1 year ago

Please test this version of act https://github.com/ChristopherHX/act/pull/34.

Usage: ./act --local-repository "https://github.com/test/test@v0=$PWD" -W test.yml -P self-hosted=-self-hosted -v

on: push
jobs:
  _:
    runs-on: self-hosted
    steps:
    - uses: test/test@v0

action.yml in cwd

runs:
  using: composite
  steps:
  - run: echo Test

I need reviewer for https://github.com/nektos/act/pull/1954, because --local-repository is based on that code.

reneleonhardt commented 5 months ago

Please test this version of act ChristopherHX#34.

Why was it closed after #1954 has been merged? 🤔

ChristopherHX commented 5 months ago

Why was it closed after https://github.com/nektos/act/pull/1954 has been merged? 🤔

Summary It was the wrong target repository and was used to keep it via GitHub UI uptodate.

Actually this has been merged by now, see act --help.

Since nobody has ever commented between our comments, I forget this issue exists. Please evaluate if this can be closed

mcascone commented 5 months ago

is there any documentation on this other than the help text?

 --local-repository stringArray                      

Replaces the specified repository and ref with a local folder 

e.g. https://github.com/test/test@v0=/home/act/test or test/test@v0=/home/act/test, 
the latter matches any hosts or protocols
ChristopherHX commented 5 months ago

Some more details here: https://github.com/nektos/act/pull/2155#issue-2088646390

Usage: ./act --local-repository "https://github.com/test/test@v0=$PWD" -W test.yml -P self-hosted=-self-hosted -v

Now also possible ./act --local-repository "test/test@v0=$PWD" -W test.yml -P self-hosted=-self-hosted -v

on: push
jobs:
  _:
    runs-on: self-hosted
    steps:
    - uses: test/test@v0

action.yml in cwd

runs:
  using: composite
  steps:
  - run: echo Test
reneleonhardt commented 5 months ago

@Porkepix Is your use case now possible?

I can't see from this --local-repository example if mcascone's intuitive feature request https://github.com/nektos/act/issues/1579#issuecomment-1574290406 works when replacing $PWD by the actual absolute path to the local test/test repo... if it's outside of $PWD 😅

ChristopherHX commented 5 months ago

mcascone's intuitive feature request

That won't work and I don't agree with accepting yaml that GitHub Actions doesn't like to see. You would need to find someone else supporting such an idea, I'm not an owner of this project.

You should make shure it's an absolute path. If it's relative, I guarantee nothing and it could just fail. E.g. $PWD is really just a bash variable for an absolute path, there are no sanity checks

This is what you should be able to get working based on the referenced comment

jobs:
  Deploy:
    steps:
      - uses: 'myOrg/my-action@my-branch'

act --local-repository myOrg/my-action@my-branch=/path/to/local/my-action and /path/to/local/my-action is created by git clone myOrg/my-action -b my-branch my-action within /path/to/local/. So /path/to/local/my-action contains the .git dir and it's action.yml file.

Feel free to come back to me with any error, --local-repository is an feature flag it implies changing how actions are cloned

If you want to apply this automatically without adding the arg everytime create a ./.actrc file like (don't use any ", as this just do a split on the first space)

--local-repository myOrg/my-action@my-branch=/path/to/local/my-action

You might be using this Software more often than I, even if I contributed more code

ChristopherHX commented 5 months ago

The error messages if the folder/file doesn't exist are bad and only the myOrg/my-action@my-branch part shows up together with the tried file names

This is what I mean, maybe it makes sense to log more about the hidden source of the action, the default implied by --local-repository is also to use bare git clones of actions.

Error: failed to read 'action.yml' from action 'myOrg/my-action@my-branch' with path '' of step file does not exist
failed to read 'action.yaml' from action 'myOrg/my-action@my-branch' with path '' of step file does not exist
failed to read 'Dockerfile' from action 'myOrg/my-action@my-branch' with path '' of step file does not exist

To be honest using act might be really complicated by now with all it's cli flags and weird error messages