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

bug: the github action outputs the wrong ref when it contains a / #11

Open
laurentsenta opened this issue Aug 17, 2023 · 2 comments
Open

Comments

@laurentsenta
Copy link
Member

https://github.com/singulargarden/kubo/actions/runs/5893416836/job/15984909672

I called an action using a branch named feat/with-job-url:

The action's context:

  "action_path": "/home/runner/work/_actions/ipfs/gateway-conformance/feat/with-job-url/.github/actions/extract-fixtures",

Which breaks the ref computation:

echo "action_ref=$(echo ${ACTION_PATH#*/_actions/} | cut -d/ -f3)" >> $GITHUB_OUTPUT
# action_ref = "feat" instead of feat/with-job-url
@laurentsenta laurentsenta changed the title bug: invalid ref when I use a branch with /, like feat/my-feat bug: the github action outputs the wrong ref when it contains a / Aug 17, 2023
@laurentsenta
Copy link
Member Author

@galargh I can fix this by replacing the match with something like:

action_ref=$(echo "$ACTION_PATH" | sed -n 's#_actions/[a-zA-Z0-9]*/[a-zA-Z0-9]*/\([a-zA-Z0-9/]*\)/.github/.*#\1#p')

But since we're doing pretty much the same match three time, would it be cool if I moved this to a js script and used capture groups, regexp, etc?

@galargh
Copy link
Member

galargh commented Aug 21, 2023

This one's a little tricky because action directories are not well-defined. The action path follows the pattern of GITHUB_WORK_DIR/_actions/GITHUB_REPOSITORY/GITHUB_ACTION_REFERENCE/GITHUB_ACTION_DIRECTORY. Both GITHUB_ACTION_REFERENCE and GITHUB_ACTION_DIRECTORY can have an arbitrary number of / in them.

However, there's one thing we could do. After action directory is downloaded, GITHUB_WORK_DIR/_actions/GITHUB_REPOSITORY/GITHUB_ACTION_REFERENCE.completed file is created. We could try finding this file. Then, to extract the ref out of it, we could take everything after GITHUB_REPOSITORY.

It could come together to something like this.

# The relative path to the action directory, without /home/runner/work/_actions/ prefix
action_rel_path=${ACTION_PATH#*/_actions/}
# The root directory where actions are stored, e.g. /home/runner/work/_actions
action_root_path=${ACTION_PATH%/$action_rel_path}

# The first two parts of the relative path represent the action repository
action_repository=$(echo $action_rel_path | cut -d/ -f1-2)
action_sha=''

action_completed_path=$ACTION_PATH

# Looking $action_root_path/$action_repository/$action_ref.completed file recursively
while [[ ! -f $action_completed_path.completed && $action_completed_path != $action_root_path/$action_repository ]]; do
  action_completed_path=$(dirname $action_completed_path);
done

if [[ $action_completed_path == $action_root_path/$action_repository ]]; then
  # The completed file doesn't exist, we fall back to assuming the third part of the relative path is the action reference (assumes no slashes in the reference)
  action_ref=$(echo $action_rel_path | cut -d/ -f3)
else
  # The completed file exists, everything from the third part until the end of the relative path to the completed file is the exact action reference
  action_ref=$(echo ${action_completed_path#*/_actions/} | cut -d/ -f3-)
fi

echo "action_repository=$action_repository" | tee -a $GITHUB_OUTPUT
echo "action_ref=$action_ref" | tee -a $GITHUB_OUTPUT
echo "action_sha=$action_sha" | tee -a $GITHUB_OUTPUT

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

No branches or pull requests

2 participants