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

Running commands after publish #25

Closed
tillkruss opened this issue May 6, 2020 · 14 comments · Fixed by #35
Closed

Running commands after publish #25

tillkruss opened this issue May 6, 2020 · 14 comments · Fixed by #35

Comments

@tillkruss
Copy link

Is it already possible to run wrangler commands after publishing?

I'd like to delete a couple of KV keys, either via several wrangler kv:key delete or via wrangler kv:bulk delete.

@kristianfreeman
Copy link
Contributor

hm, good question! you may be able to put in some shell commands after the uses: wrangler-action step and just call wrangler directly, but i haven't tested it!

@tillkruss
Copy link
Author

I'm getting this error:

/home/runner/work/_temp/...673e2ea27105.sh: line 1: wrangler: command not found

Step:

- name: Clean KV cache
    run: wrangler kv:key delete --binding=app cache-foo

@till
Copy link

till commented Jun 22, 2020

The action doesn't do anything besides wrangler publish. So I have a similar request, I want to run wrangler build only (and then deploy with Terraform). Can you extend your action to support this?

@tillkruss
Copy link
Author

@signalnerve: Two Tills requesting this feature, must be a sign.

@bradyjoslin
Copy link
Contributor

One option for addressing would be similar to how the action was extended for secrets.

Example action yaml of implementation idea:

jobs:
  deploy:
    steps:
      uses: cloudflare/wrangler-action@1.2.0
      with:
        apiToken: ${{ secrets.CF_API_TOKEN }}
        wrangler-commands: |
            kv:namespace create "MY_KV"
            kv:key put --binding=MY_KV "key" "value"

entrypoint.sh could be updated as:

# If an environment is detected as input, for each secret specified get the value of
# the matching named environment variable then configure using wrangler secret put.
if [ -z "$INPUT_ENVIRONMENT" ]
then
  wrangler publish

  for SECRET in $INPUT_SECRETS; do
    VALUE=$(printenv "$SECRET") || secret_not_found "$SECRET"
    echo "$VALUE" | wrangler secret put "$SECRET"
  done

  # ** New feature for handling arbitrary wrangler commands **
  for COMMAND in $INPUT_WRANGLER_COMMANDS; do
    wrangler "$COMMAND"
  done
else
  wrangler publish -e "$INPUT_ENVIRONMENT"

  # ** New feature for handling arbitrary wrangler commands **
  for COMMAND in $INPUT_WRANGLER_COMMANDS; do
    wrangler "$COMMAND" --env "$INPUT_ENVIRONMENT"
  done
fi

@tillkruss
Copy link
Author

@bradyjoslin, nice! Can you submit a PR?

@bradyjoslin
Copy link
Contributor

That's sort of pseudocode above just to express an idea, if implemented as written a few things wouldn't work right. But, if I get some time I'll try and prototype something out.

@bradyjoslin
Copy link
Contributor

Have a POC working with these changes.

Update to action.yaml:

...
  wranglerCommands:
    description: "(Advanced) A new line deliminated string of wrangler commands.  Target environment must be explicitly specified."
    required: false

With this update to entrypoint.sh.

...
# ** New feature for handling arbitrary wrangler commands **
printf '%s' "$INPUT_WRANGLERCOMMANDS" | while IFS= read -r COMMAND; do
  OPTS=()

  for OPT in $COMMAND; do
    OPTS+=("$OPT")
  done

  wrangler "${OPTS[@]}"

  OPTS=()
done

Example deployment.yml that updates two kv entries using the action:

...
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          environment: "production"
          workingDirectory: "test"
          secrets: SECRET1
          wranglerCommands: |
            kv:key put --binding=STATIC_TEST_MY_KV key value
            kv:key put --binding=STATIC_TEST_MY_KV key2 value2
        env:
          SECRET1: ${{ secrets.SECRET1 }}

@till
Copy link

till commented Aug 7, 2020

@bradyjoslin please open a pull-request? Other people can take it on if it goes stale and you don't have the time.

@kristianfreeman
Copy link
Contributor

kristianfreeman commented Aug 7, 2020

@bradyjoslin this is awesome! I'm wondering if it makes more sense to have it not be wrangler subcommands, and just have a pre/post-publish inputs that you can use to execute commands inside of the wrangler-action context. @till would be curious on your thoughts around this too

@till
Copy link

till commented Aug 11, 2020

@signalnerve Well, sort of.

I need something basic like, a action: build, and I figured I can always "include" the action multiple times (if the penalty of running or restarting it isn't too high), but see #29 for what I mean. E.g. if I wanted to kv:key put, I would include it again in the next step.

So as long as I can e.g. not do publish (it can be the default though), then that works. Right now this adds pre/post commands, so it's not exactly what I need.

@bradyjoslin
Copy link
Contributor

An idea for supporting builds without publish...

A new optional action input publish that if set to false could skip the wrangler publish portion of the Action's script.

That way if someone wanted to just build, run kv commands, or anything else, they could specify those as preCommands or postCommands with publish set to false.

@till
Copy link

till commented Aug 11, 2020

@bradyjoslin I think that sounds great! :)

@bradyjoslin
Copy link
Contributor

@till - Awesome! PR #35 updated to include this additional option.

Set the optional publish input to false to skip publishing your Worker project and secrets. Useful in conjunction with pre and post commands. For example, if you only wanted to run wrangler build against your project:

jobs:
  deploy:
    steps:
      uses: cloudflare/wrangler-action@1.2.0
      with:
        apiToken: ${{ secrets.CF_API_TOKEN }}
        publish: false
        preCommands: wrangler build

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

Successfully merging a pull request may close this issue.

4 participants