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

[FR]: Providing working-directory as an input param #98

Merged
13 changes: 12 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ inputs:
description: "The package you want to scan, by default will be ./..."
required: false
default: "./..."
working-directory:
description: "The working directory, from where the scan should start, by default will be github.workspace"
required: false
default: ${{ github.workspace }}
go-version:
description: "Can be any Tag for the golang docker image, but should ideally match your runtime go version. By default 1.21.4 is assumed"
required: false
Expand All @@ -30,15 +34,22 @@ inputs:
runs:
using: "composite"
steps:
- id: determine-working-directory
run: |
source ${{ github.action_path }}/determine-wd.sh ${{ github.workspace }} ${{ inputs.working-directory }}
echo "DOCKER_WD=${DOCKER_WD}" >> "$GITHUB_ENV"
echo "GITHUBH_WD=${GITHUBH_WD}" >> "$GITHUB_ENV"
shell: bash
- id: config
run: echo "GOLANG_VERSION=${{ inputs.go-version }} VULNCHECK_VERSION=${{ inputs.vulncheck-version }}"
shell: bash
- id: build
run: docker build --build-arg GOLANG_VERSION="${{ inputs.go-version }}" --build-arg GH_PAT_TOKEN=$GH_PAT_TOKEN --build-arg GOPRIVATE=$GOPRIVATE --build-arg VULNCHECK_VERSION="${{ inputs.vulncheck-version }}" -q -f $GITHUB_ACTION_PATH/Dockerfile -t templum/govulncheck-action:local $GITHUB_ACTION_PATH
shell: bash
- id: run
run: docker run --rm -v $(pwd):/github/workspace --workdir /github/workspace -e GITHUB_TOKEN=${{ inputs.github-token }} -e STRICT=${{ inputs.fail-on-vuln }} -e PACKAGE=${{ inputs.package }} -e SKIP_UPLOAD=${{ inputs.skip-upload }} -e DEBUG=${DEBUG} -e GITHUB_REPOSITORY=${{ github.repository }} -e GITHUB_REF=${{ github.ref }} -e GITHUB_SHA=${{ github.sha }} templum/govulncheck-action:local
run: docker run --rm -v $(pwd):${{ env.DOCKER_WD }} --workdir ${{ env.DOCKER_WD }} -e GITHUB_TOKEN=${{ inputs.github-token }} -e STRICT=${{ inputs.fail-on-vuln }} -e PACKAGE=${{ inputs.package }} -e SKIP_UPLOAD=${{ inputs.skip-upload }} -e DEBUG=${DEBUG} -e GITHUB_REPOSITORY=${{ github.repository }} -e GITHUB_REF=${{ github.ref }} -e GITHUB_SHA=${{ github.sha }} templum/govulncheck-action:local
shell: bash
working-directory: ${{ env.GITHUBH_WD }}

branding:
icon: "alert-octagon"
Expand Down
21 changes: 21 additions & 0 deletions determine-wd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

github_workspace=$1
input_working_directory=$2

if [ -z $github_workspace ]; then
echo "The first argument (github.workspace) is required.";
exit 1;
elif [[ $github_workspace = $input_working_directory || -z $input_working_directory ]]; then
export DOCKER_WD=$github_workspace;
export GITHUBH_WD=$github_workspace;
elif [[ $input_working_directory =~ ^/ ]]; then
export DOCKER_WD=$input_working_directory;
export GITHUBH_WD=.$input_working_directory;
else
export DOCKER_WD=$(echo $input_working_directory | sed 's/\.//');
export GITHUBH_WD=$input_working_directory
fi
Comment on lines +6 to +18
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be so kind to walk me through your code here ?

I see that DOCKER_WD & GITHUBH_WD in the most cases have the same content

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sure!

The content is not exactly the same. Let me provide more details, which could simply the understanding:

  • if the provided github.worspace is empty --> the execution is not possible. The reason for that is to be sure that the first argument is github.worspace as we use it as a default case.
  • if the github_workspace == input_working_directory OR the input_working_directory == "" (as a second arg) we could use github_workspace as a default use case.

if 2 args are provided, we could forget about github_workspace and use input_working_directory for our processing:

  • if input_working_directory starts with / then for the docker we would use it as it is (docker accepts only absolute paths), but for the github workdir we would need to add a dot as a prefix: ./ (GitHub working-directory should be relative path)
  • input_working_directory starts with ./ then for the docker we would remove a dot from the beginning with a help of sed command (docker accepts only absolute paths) and use input_working_directory as it is for the github actions (GitHub working-directory should be relative path)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This morning I tried to implement it in Go with tests (you could see commits below), but I gave up this idea because it's not possible to set the env variable from the go program to use it outside of it. I guess it could be a workaround, but probably with sh script is cleaner

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, probably having a shell script is fine for this case. I also don't think any extra test needs to be done. The E2E Flow that we have in place, should be sufficient enough.


echo "export DOCKER_WD=$DOCKER_WD"
echo "export GITHUBH_WD=$GITHUBH_WD"
Loading