cloudflare / wrangler-action

🧙‍♀️ easily deploy cloudflare workers applications using wrangler and github actions
Apache License 2.0
1.25k stars 159 forks source link

Unable to forward command output #206

Closed LeoColomb closed 1 year ago

LeoColomb commented 1 year ago

Here is my use case:

      - name: Retrieve Terraform State
        uses: cloudflare/wrangler-action@v3.3.2
        with:
          command: kv:namespace list | jq -r '.[] | select( .title == "dispoflare-terraform-state" ) | .id' .kv_namespace_list.json | awk '{print "tfstate_kv="$1}' >> $GITHUB_ENV
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

This use case is mostly coming from Cloudflare Wildbeest deploy workflow: https://github.com/cloudflare/wildebeest/blob/20efb7f0eb504462be869b91102307991d991c2f/.github/workflows/deploy.yml#L127-L137

The command is executed as followed, with the following wrangler error:

🚀 Running Wrangler Commands
  /usr/local/bin/npx wrangler kv:namespace list | jq -r '.[] | select( .title == dispoflare-terraform-state ) | .id' .kv_namespace_list.json | awk '{print tfstate_kv=$1}' >> $GITHUB_ENV

  ✘ [ERROR] Unknown arguments: r, |, jq, |, select(, .title, ==, dispoflare-terraform-state, ), |, .id', .kv_namespace_list.json, |, awk, '{print, tfstate_kv=$1}', >>, $GITHUB_ENV

Indeed, since #171, all arguments of command parameter are passed directly to the package manager. It is therefore impossible to forward output to something else than the default stdout.

Using pre/post-commands as a workaround is mostly impossible due to #178.

1000hz commented 1 year ago

Hey @LeoColomb, thanks for filing an issue.

Yeah, command doesn't execute within a shell, which is by design since we decorate the command you provide with extra stuff (your package manager's exec command, wrangler, extra flags, etc.) to produce the actually executed command. Supporting a shell would make that significantly more error-prone and complex.

Using pre/post-commands is mostly impossible due to https://github.com/cloudflare/wrangler-action/issues/178.

What about #178 is preventing you from using pre/post-commands here? The two issues described there boil down to needing to invoke wrangler via your package manager and having to provide your own flags. It's maybe not quite as convenient, but definitely still possible.

This should work for you (assuming you're using npm):

      - name: Retrieve Terraform State
        uses: cloudflare/wrangler-action@v3.3.2
        with:
-         command: kv:namespace list | jq -r '.[] | select( .title == "dispoflare-terraform-state" ) | .id' .kv_namespace_list.json | awk '{print "tfstate_kv="$1}' >> $GITHUB_ENV
+         preCommands: npx wrangler kv:namespace list | jq -r '.[] | select( .title == "dispoflare-terraform-state" ) | .id' .kv_namespace_list.json | awk '{print "tfstate_kv="$1}' >> $GITHUB_ENV
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

It's also worth mentioning, wrangler-action is primarily geared toward the use case of easily deploying your Workers via CI. The Wildebeest workflows probably would have been better off installing wrangler into the runner environment and just running shell commands via run: for most of its steps rather than using wrangler-action a whole bunch of times.

LeoColomb commented 1 year ago

Thanks for your reply, @1000hz. Indeed, I read #178 a bit too quick, I guess 😊 This is working, but comes with a few drawbacks: as you mentioned, the need to invoke the package manager, but also the need to pass a dummy command to avoid unexpected deployments.

Noted for the mention, I'll give a try to do in this other way instead.