From 93b9408aecf45546b2be4bd23ee483f7ab3361dc Mon Sep 17 00:00:00 2001 From: "Joslin, Brady W (Brady)" Date: Fri, 7 Aug 2020 16:16:42 -0500 Subject: [PATCH 1/4] pre-post-commands --- .github/workflows/deploy.yml | 7 ++++++- README.md | 18 ++++++++++++++++- action.yml | 6 ++++++ entrypoint.sh | 38 +++++++++++++++++++++++++++++++----- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 1140052..7f4468d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -60,6 +60,11 @@ jobs: secrets: | SECRET1 SECRET2 + preCommands: echo "*** pre command ***" + postCommands: | + echo "*** post commands ***" + wrangler build + echo "******" env: SECRET1: ${{ secrets.SECRET1 }} - SECRET2: ${{ secrets.SECRET2 }} + SECRET2: ${{ secrets.SECRET2 }} \ No newline at end of file diff --git a/README.md b/README.md index 4fc6530..7325925 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,22 @@ jobs: SECRET2: ${{ secrets.SECRET2 }} ``` +If you need to run additional shell commands before or after `wrangler publish`, you can specify them as input to `preCommands` (before publish) or `postCommands` (after publish). These can include additional `wrangler` commands (i.e. `build`, `kv:key put`) or any other commands available inside the `wrangler-action` context. + +```yaml +jobs: + deploy: + steps: + uses: cloudflare/wrangler-action@1.2.0 + with: + apiToken: ${{ secrets.CF_API_TOKEN }} + preCommands: echo "*** pre command ***" + postCommands: | + echo "*** post commands ***" + wrangler kv:key put --binding=MY_KV key2 value2 + echo "******" +``` + ## Use cases ### Deploying when commits are merged to master @@ -228,4 +244,4 @@ jobs: uses: cloudflare/wrangler-action@1.2.0 with: apiToken: ${{ secrets.CF_API_TOKEN }} -``` +``` \ No newline at end of file diff --git a/action.yml b/action.yml index 57d8764..dbc4df7 100644 --- a/action.yml +++ b/action.yml @@ -22,3 +22,9 @@ inputs: secrets: description: "A new line deliminated string of environment variable names that should be configured as Worker secrets" required: false + preCommands: + description: "Commands to execute before publishing the Workers project" + required: false + postCommands: + description: "Commands to execute after publishing the Workers project" + required: false diff --git a/entrypoint.sh b/entrypoint.sh index ffa0580..6ccda21 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e @@ -10,6 +10,27 @@ chmod -R 770 "$HOME/.wrangler" export API_CREDENTIALS="" +# Used to execute any specified pre and post commands +execute_commands() { + COMMANDS=$1 + while IFS= read -r COMMAND; do + CHUNKS=() + + for CHUNK in $COMMAND; do + CHUNKS+=("$CHUNK") + done + + "${CHUNKS[@]}" + + CHUNKS=() + done <<< "$COMMANDS" +} + +secret_not_found() { + echo "::error::Specified secret \"$1\" not found in environment variables." + exit 1 +} + # If an API token is detected as input if [ -n "$INPUT_APITOKEN" ] then @@ -58,10 +79,11 @@ then cd "$INPUT_WORKINGDIRECTORY" fi -secret_not_found() { - echo "::error::Specified secret \"$1\" not found in environment variables." - exit 1 -} +# If precommands is detected as input +if [ -n "$INPUT_PRECOMMANDS" ] +then + execute_commands "$INPUT_PRECOMMANDS" +fi # 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. @@ -82,6 +104,12 @@ else done fi +# If postcommands is detected as input +if [ -n "$INPUT_POSTCOMMANDS" ] +then + execute_commands "$INPUT_POSTCOMMANDS" +fi + # If a working directory is detected as input, revert to the # original directory before continuing with the workflow if [ -n "$INPUT_WORKINGDIRECTORY" ] From 176cda2c0ea28839485ad6849ab425d1c9b262bb Mon Sep 17 00:00:00 2001 From: "Joslin, Brady W (Brady)" Date: Tue, 11 Aug 2020 22:46:42 -0500 Subject: [PATCH 2/4] add ability to skip publish --- .github/workflows/deploy.yml | 23 ++++++++++++++++++++++- README.md | 12 ++++++++++++ action.yml | 3 +++ entrypoint.sh | 28 ++++++++++++++++------------ 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 7f4468d..2b5377d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -10,6 +10,28 @@ jobs: uses: azohra/shell-linter@v0.3.0 with: path: "entrypoint.sh" + build-only: + runs-on: ubuntu-latest + name: Only build the app + steps: + - uses: actions/checkout@v2 + - name: Build app + uses: ./ + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + workingDirectory: "test" + publish: false + secrets: | + SECRET1 + SECRET2 + preCommands: echo "*** pre commands ***" + postCommands: | + echo "*** post commands ***" + wrangler build + echo "******" + env: + SECRET1: ${{ secrets.SECRET1 }} + SECRET2: ${{ secrets.SECRET2 }} publish: runs-on: ubuntu-latest name: Publish app @@ -63,7 +85,6 @@ jobs: preCommands: echo "*** pre command ***" postCommands: | echo "*** post commands ***" - wrangler build echo "******" env: SECRET1: ${{ secrets.SECRET1 }} diff --git a/README.md b/README.md index 7325925..3d82948 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,18 @@ jobs: echo "******" ``` +Set the optional `publish` input to false to skip publishing your Worker project and secrets. + +```yaml +jobs: + deploy: + steps: + uses: cloudflare/wrangler-action@1.2.0 + with: + apiToken: ${{ secrets.CF_API_TOKEN }} + publish: false +``` + ## Use cases ### Deploying when commits are merged to master diff --git a/action.yml b/action.yml index dbc4df7..4f038db 100644 --- a/action.yml +++ b/action.yml @@ -28,3 +28,6 @@ inputs: postCommands: description: "Commands to execute after publishing the Workers project" required: false + publish: + description: "Set to false to skip publishing your Worker project and secrets. Defaults to true." + required: false \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh index 6ccda21..51d9195 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -87,21 +87,25 @@ fi # 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" ] +# Skip if publish is set to false. +if [ "$INPUT_PUBLISH" != "false" ] then - wrangler publish + 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 -else - wrangler publish -e "$INPUT_ENVIRONMENT" + for SECRET in $INPUT_SECRETS; do + VALUE=$(printenv "$SECRET") || secret_not_found "$SECRET" + echo "$VALUE" | wrangler secret put "$SECRET" + done + else + wrangler publish -e "$INPUT_ENVIRONMENT" - for SECRET in $INPUT_SECRETS; do - VALUE=$(printenv "$SECRET") || secret_not_found "$SECRET" - echo "$VALUE" | wrangler secret put "$SECRET" --env "$INPUT_ENVIRONMENT" - done + for SECRET in $INPUT_SECRETS; do + VALUE=$(printenv "$SECRET") || secret_not_found "$SECRET" + echo "$VALUE" | wrangler secret put "$SECRET" --env "$INPUT_ENVIRONMENT" + done + fi fi # If postcommands is detected as input From bc7b76075d5454028fb89058095eab5cdc068e93 Mon Sep 17 00:00:00 2001 From: "Joslin, Brady W (Brady)" Date: Tue, 11 Aug 2020 22:53:34 -0500 Subject: [PATCH 3/4] update explanation of publish option --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3d82948..1874c10 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ jobs: echo "******" ``` -Set the optional `publish` input to false to skip publishing your Worker project and secrets. +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: ```yaml jobs: @@ -136,6 +136,7 @@ jobs: with: apiToken: ${{ secrets.CF_API_TOKEN }} publish: false + preCommands: wrangler build ``` ## Use cases From 212d5a512566930af60bbb813b7672d8b5cc7a2b Mon Sep 17 00:00:00 2001 From: Brady Joslin Date: Thu, 13 Aug 2020 00:12:47 -0500 Subject: [PATCH 4/4] eval the input command --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 51d9195..7b68e34 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -20,7 +20,7 @@ execute_commands() { CHUNKS+=("$CHUNK") done - "${CHUNKS[@]}" + eval "${CHUNKS[@]}" CHUNKS=() done <<< "$COMMANDS"