From bf4aad6ad296822cb0352e21ce3867284eaf1eff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Martins?= Date: Wed, 8 Jun 2022 17:25:51 +0200 Subject: [PATCH] pkg/runner: add support to replace GitHub's env (#1197) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There might be use cases where users want to use GitHub's variables in the environment variables, which is a valid use case. This commits adds support for replacement of GitHub's env with GitHub's values. Signed-off-by: André Martins --- pkg/runner/runner.go | 9 +++++++++ pkg/runner/runner_test.go | 1 + pkg/runner/testdata/issue-1195/push.yml | 13 +++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 pkg/runner/testdata/issue-1195/push.yml diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index dd71f2b..b089097 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -14,6 +14,7 @@ import ( "github.com/nektos/act/pkg/common" "github.com/nektos/act/pkg/container" + "github.com/nektos/act/pkg/exprparser" "github.com/nektos/act/pkg/model" ) @@ -159,6 +160,14 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor { if len(matrixes) > 1 { rc.Name = fmt.Sprintf("%s-%d", rc.Name, i+1) } + // evaluate environment variables since they can contain + // GitHub's special environment variables. + for k, v := range rc.GetEnv() { + valueEval, err := rc.ExprEval.evaluate(v, exprparser.DefaultStatusCheckNone) + if err == nil { + rc.Env[k] = fmt.Sprintf("%v", valueEval) + } + } if len(rc.String()) > maxJobNameLen { maxJobNameLen = len(rc.String()) } diff --git a/pkg/runner/runner_test.go b/pkg/runner/runner_test.go index 4e88fed..d183da6 100644 --- a/pkg/runner/runner_test.go +++ b/pkg/runner/runner_test.go @@ -147,6 +147,7 @@ func TestRunEvent(t *testing.T) { {workdir, "evalmatrixneeds2", "push", "", platforms}, {workdir, "evalmatrix-merge-map", "push", "", platforms}, {workdir, "evalmatrix-merge-array", "push", "", platforms}, + {workdir, "issue-1195", "push", "", platforms}, {workdir, "basic", "push", "", platforms}, {workdir, "fail", "push", "exit with `FAILURE`: 1", platforms}, diff --git a/pkg/runner/testdata/issue-1195/push.yml b/pkg/runner/testdata/issue-1195/push.yml new file mode 100644 index 0000000..c211ad2 --- /dev/null +++ b/pkg/runner/testdata/issue-1195/push.yml @@ -0,0 +1,13 @@ +on: push + +env: + variable: "${{ github.repository_owner }}" + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: print env.variable + run: | + echo ${{ env.variable }} + exit ${{ (env.variable == 'nektos') && '0' || '1'}}