From c564975dece483b0a41dedcfd977eacec4d5e736 Mon Sep 17 00:00:00 2001 From: JargeZ Date: Fri, 19 Jul 2024 18:03:21 +1000 Subject: [PATCH 01/14] Allow bypassing of pr-number check --- action.yml | 3 +++ entrypoint.sh | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 0748d75a..b6d870e5 100644 --- a/action.yml +++ b/action.yml @@ -38,3 +38,6 @@ inputs: ha: description: Create spare machines that increases app availability (default false) default: false + allow_unsafe_name: + description: Allow the app name to be set without PR number + default: false diff --git a/entrypoint.sh b/entrypoint.sh index 6719a6f1..9459680b 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -26,8 +26,11 @@ image="$INPUT_IMAGE" config="${INPUT_CONFIG:-fly.toml}" if ! echo "$app" | grep "$PR_NUMBER"; then - echo "For safety, this action requires the app's name to contain the PR number." - exit 1 + if [ "$INPUT_ALLOW_UNSAFE_NAME" != "true" ]; then + echo "For safety, this action requires the app's name to contain the PR number." + exit 1 + fi + echo "WARNING: The app's name does not contain the PR number. it is recommended to include the PR number in the app's name." fi # PR was closed - remove the Fly app if one exists and exit. From 704a7e029d56be473e2c41320825bf92fea488a1 Mon Sep 17 00:00:00 2001 From: JargeZ Date: Fri, 19 Jul 2024 19:22:29 +1000 Subject: [PATCH 02/14] Documentation --- README.md | 33 +++++++++++++++++---------------- action.yml | 2 +- entrypoint.sh | 2 +- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 04f6d4e5..50d2dbdd 100644 --- a/README.md +++ b/README.md @@ -8,22 +8,23 @@ If you have an existing `fly.toml` in your repo, this action will copy it with a ## Inputs -| name | description | -| ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `name` | The name of the Fly app. Alternatively, set the env `FLY_APP`. For safety, must include the PR number. Example: `myapp-pr-${{ github.event.number }}`. Defaults to `pr-{number}-{repo_org}-{repo_name}`. | -| `image` | Optional pre-existing Docker image to use | -| `config` | Optional path to a custom Fly toml config. Config path should be relative to `path` parameter, if specified. | -| `region` | Which Fly region to run the app in. Alternatively, set the env `FLY_REGION`. Defaults to `iad`. | -| `org` | Which Fly organization to launch the app under. Alternatively, set the env `FLY_ORG`. Defaults to `personal`. | -| `path` | Path to run the `flyctl` commands from. Useful if you have an existing `fly.toml` in a subdirectory. | -| `postgres` | Optional name of an existing Postgres cluster to `flyctl postgres attach` to. | -| `update` | Whether or not to update this Fly app when the PR is updated. Default `true`. | -| `secrets` | Secrets to be set on the app. Separate multiple secrets with a space | -| `vmsize` | Set app VM to a named size, eg. shared-cpu-1x, dedicated-cpu-1x, dedicated-cpu-2x etc. Takes precedence over cpu, cpu kind, and memory inputs. | -| `cpu` | Set app VM CPU (defaults to 1 cpu). Default 1. | -| `cpukind` | Set app VM CPU kind - shared or performance. Default shared. | -| `memory` | Set app VM memory in megabytes. Default 256. | -| `ha` | Create spare machines that increases app availability. Default `false`. | +| name | description | +| ---------- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `name` | The name of the Fly app. Alternatively, set the env `FLY_APP`. For safety, **must include the PR number** (if not disabled by `allow_unsafe_name`). Example: `myapp-pr-${{ github.event.number }}`. Defaults to `pr-{number}-{repo_org}-{repo_name}`. | +| `image` | Optional pre-existing Docker image to use | +| `config` | Optional path to a custom Fly toml config. Config path should be relative to `path` parameter, if specified. | +| `region` | Which Fly region to run the app in. Alternatively, set the env `FLY_REGION`. Defaults to `iad`. | +| `org` | Which Fly organization to launch the app under. Alternatively, set the env `FLY_ORG`. Defaults to `personal`. | +| `path` | Path to run the `flyctl` commands from. Useful if you have an existing `fly.toml` in a subdirectory. | +| `postgres` | Optional name of an existing Postgres cluster to `flyctl postgres attach` to. | +| `update` | Whether or not to update this Fly app when the PR is updated. Default `true`. | +| `secrets` | Secrets to be set on the app. Separate multiple secrets with a space | +| `vmsize` | Set app VM to a named size, eg. shared-cpu-1x, dedicated-cpu-1x, dedicated-cpu-2x etc. Takes precedence over cpu, cpu kind, and memory inputs. | +| `cpu` | Set app VM CPU (defaults to 1 cpu). Default 1. | +| `cpukind` | Set app VM CPU kind - shared or performance. Default shared. | +| `memory` | Set app VM memory in megabytes. Default 256. | +| `ha` | Create spare machines that increases app availability. Default `false`. | +| `allow_unsafe_name` | Allow a name that does not contain a PR number. Useful for deploying preview environments across multiple repo with reusable pipelines. Default `false`. | ## Required Secrets diff --git a/action.yml b/action.yml index b6d870e5..c482970e 100644 --- a/action.yml +++ b/action.yml @@ -39,5 +39,5 @@ inputs: description: Create spare machines that increases app availability (default false) default: false allow_unsafe_name: - description: Allow the app name to be set without PR number + description: Allow a name that does not contain a PR number. Useful for deploying preview environments across multiple repo with reusable pipelines (default false) default: false diff --git a/entrypoint.sh b/entrypoint.sh index 9459680b..c75ce939 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -27,7 +27,7 @@ config="${INPUT_CONFIG:-fly.toml}" if ! echo "$app" | grep "$PR_NUMBER"; then if [ "$INPUT_ALLOW_UNSAFE_NAME" != "true" ]; then - echo "For safety, this action requires the app's name to contain the PR number." + echo "For safety, this action requires the app's name to contain the PR number. If you are sure you want to proceed, set the 'allow_unsafe_name' input to 'true'." exit 1 fi echo "WARNING: The app's name does not contain the PR number. it is recommended to include the PR number in the app's name." From 2c6343f3134e39172394a584f429320661cf31fc Mon Sep 17 00:00:00 2001 From: JargeZ Date: Sat, 20 Jul 2024 22:23:09 +1000 Subject: [PATCH 03/14] Pass build-args --- README.md | 35 ++++++++++++++++++----------------- action.yml | 2 ++ entrypoint.sh | 9 +++++++-- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 50d2dbdd..b2644024 100644 --- a/README.md +++ b/README.md @@ -8,23 +8,24 @@ If you have an existing `fly.toml` in your repo, this action will copy it with a ## Inputs -| name | description | -| ---------- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `name` | The name of the Fly app. Alternatively, set the env `FLY_APP`. For safety, **must include the PR number** (if not disabled by `allow_unsafe_name`). Example: `myapp-pr-${{ github.event.number }}`. Defaults to `pr-{number}-{repo_org}-{repo_name}`. | -| `image` | Optional pre-existing Docker image to use | -| `config` | Optional path to a custom Fly toml config. Config path should be relative to `path` parameter, if specified. | -| `region` | Which Fly region to run the app in. Alternatively, set the env `FLY_REGION`. Defaults to `iad`. | -| `org` | Which Fly organization to launch the app under. Alternatively, set the env `FLY_ORG`. Defaults to `personal`. | -| `path` | Path to run the `flyctl` commands from. Useful if you have an existing `fly.toml` in a subdirectory. | -| `postgres` | Optional name of an existing Postgres cluster to `flyctl postgres attach` to. | -| `update` | Whether or not to update this Fly app when the PR is updated. Default `true`. | -| `secrets` | Secrets to be set on the app. Separate multiple secrets with a space | -| `vmsize` | Set app VM to a named size, eg. shared-cpu-1x, dedicated-cpu-1x, dedicated-cpu-2x etc. Takes precedence over cpu, cpu kind, and memory inputs. | -| `cpu` | Set app VM CPU (defaults to 1 cpu). Default 1. | -| `cpukind` | Set app VM CPU kind - shared or performance. Default shared. | -| `memory` | Set app VM memory in megabytes. Default 256. | -| `ha` | Create spare machines that increases app availability. Default `false`. | -| `allow_unsafe_name` | Allow a name that does not contain a PR number. Useful for deploying preview environments across multiple repo with reusable pipelines. Default `false`. | +| name | description | +| ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `name` | The name of the Fly app. Alternatively, set the env `FLY_APP`. For safety, must include the PR number. Example: `myapp-pr-${{ github.event.number }}`. Defaults to `pr-{number}-{repo_org}-{repo_name}`. | +| `image` | Optional pre-existing Docker image to use | +| `config` | Optional path to a custom Fly toml config. Config path should be relative to `path` parameter, if specified. | +| `region` | Which Fly region to run the app in. Alternatively, set the env `FLY_REGION`. Defaults to `iad`. | +| `org` | Which Fly organization to launch the app under. Alternatively, set the env `FLY_ORG`. Defaults to `personal`. | +| `path` | Path to run the `flyctl` commands from. Useful if you have an existing `fly.toml` in a subdirectory. | +| `postgres` | Optional name of an existing Postgres cluster to `flyctl postgres attach` to. | +| `update` | Whether or not to update this Fly app when the PR is updated. Default `true`. | +| `secrets` | Secrets to be set on the app. Separate multiple secrets with a space | +| `vmsize` | Set app VM to a named size, eg. shared-cpu-1x, dedicated-cpu-1x, dedicated-cpu-2x etc. Takes precedence over cpu, cpu kind, and memory inputs. | +| `cpu` | Set app VM CPU (defaults to 1 cpu). Default 1. | +| `cpukind` | Set app VM CPU kind - shared or performance. Default shared. | +| `memory` | Set app VM memory in megabytes. Default 256. | +| `ha` | Create spare machines that increases app availability. Default `false`. | +| `allow_unsafe_name` | Allow a name that does not contain a PR number. Useful for deploying preview environments across multiple repo with reusable pipelines. Default `false`. | +| `build_args` | Build arguments to pass to the Docker build. Separate multiple arguments with a semicolon. `FOO=bar;BAZ=qux` | ## Required Secrets diff --git a/action.yml b/action.yml index c482970e..b5fdbb07 100644 --- a/action.yml +++ b/action.yml @@ -41,3 +41,5 @@ inputs: allow_unsafe_name: description: Allow a name that does not contain a PR number. Useful for deploying preview environments across multiple repo with reusable pipelines (default false) default: false + build_args: + description: Build arguments to pass to the Docker build. Separate multiple arguments with a semicolon. FOO=bar;BAZ=qux diff --git a/entrypoint.sh b/entrypoint.sh index c75ce939..0f734d79 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -56,12 +56,17 @@ if [ -n "$INPUT_POSTGRES" ]; then flyctl postgres attach "$INPUT_POSTGRES" --app "$app" || true fi +# create --build-arg array +if [ -n "$INPUT_BUILD_ARGS" ]; then + BUILD_ARGS="--build-arg ${INPUT_BUILD_ARGS//;/ --build-arg }" +fi + # Trigger the deploy of the new version. echo "Contents of config $config file: " && cat "$config" if [ -n "$INPUT_VM" ]; then - flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA --vm-size "$INPUT_VMSIZE" + flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA --vm-size "$INPUT_VMSIZE" $BUILD_ARGS else - flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA --vm-cpu-kind "$INPUT_CPUKIND" --vm-cpus $INPUT_CPU --vm-memory "$INPUT_MEMORY" + flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA --vm-cpu-kind "$INPUT_CPUKIND" --vm-cpus $INPUT_CPU --vm-memory "$INPUT_MEMORY" $BUILD_ARGS fi # Make some info available to the GitHub workflow. From 8c21c56db4a154fef7cad96f8afe17e653789f52 Mon Sep 17 00:00:00 2001 From: JargeZ Date: Tue, 30 Jul 2024 21:15:13 +1000 Subject: [PATCH 04/14] All the variables --- README.md | 37 +++++++++++++++++++------------------ action.yml | 6 +++++- entrypoint.sh | 34 ++++++++++++++++++++++++++-------- 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index b2644024..d8a8f4bd 100644 --- a/README.md +++ b/README.md @@ -8,24 +8,25 @@ If you have an existing `fly.toml` in your repo, this action will copy it with a ## Inputs -| name | description | -| ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| name | description | +| ---------- |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `name` | The name of the Fly app. Alternatively, set the env `FLY_APP`. For safety, must include the PR number. Example: `myapp-pr-${{ github.event.number }}`. Defaults to `pr-{number}-{repo_org}-{repo_name}`. | -| `image` | Optional pre-existing Docker image to use | -| `config` | Optional path to a custom Fly toml config. Config path should be relative to `path` parameter, if specified. | -| `region` | Which Fly region to run the app in. Alternatively, set the env `FLY_REGION`. Defaults to `iad`. | -| `org` | Which Fly organization to launch the app under. Alternatively, set the env `FLY_ORG`. Defaults to `personal`. | -| `path` | Path to run the `flyctl` commands from. Useful if you have an existing `fly.toml` in a subdirectory. | -| `postgres` | Optional name of an existing Postgres cluster to `flyctl postgres attach` to. | -| `update` | Whether or not to update this Fly app when the PR is updated. Default `true`. | -| `secrets` | Secrets to be set on the app. Separate multiple secrets with a space | -| `vmsize` | Set app VM to a named size, eg. shared-cpu-1x, dedicated-cpu-1x, dedicated-cpu-2x etc. Takes precedence over cpu, cpu kind, and memory inputs. | -| `cpu` | Set app VM CPU (defaults to 1 cpu). Default 1. | -| `cpukind` | Set app VM CPU kind - shared or performance. Default shared. | -| `memory` | Set app VM memory in megabytes. Default 256. | -| `ha` | Create spare machines that increases app availability. Default `false`. | -| `allow_unsafe_name` | Allow a name that does not contain a PR number. Useful for deploying preview environments across multiple repo with reusable pipelines. Default `false`. | -| `build_args` | Build arguments to pass to the Docker build. Separate multiple arguments with a semicolon. `FOO=bar;BAZ=qux` | +| `image` | Optional pre-existing Docker image to use | +| `config` | Optional path to a custom Fly toml config. Config path should be relative to `path` parameter, if specified. | +| `region` | Which Fly region to run the app in. Alternatively, set the env `FLY_REGION`. Defaults to `iad`. | +| `org` | Which Fly organization to launch the app under. Alternatively, set the env `FLY_ORG`. Defaults to `personal`. | +| `path` | Path to run the `flyctl` commands from. Useful if you have an existing `fly.toml` in a subdirectory. | +| `postgres` | Optional name of an existing Postgres cluster to `flyctl postgres attach` to. | +| `update` | Whether or not to update this Fly app when the PR is updated. Default `true`. | +| `secrets` | Secrets to be set on the app. Separate multiple secrets with a space | +| `vmsize` | Set app VM to a named size, eg. shared-cpu-1x, dedicated-cpu-1x, dedicated-cpu-2x etc. Takes precedence over cpu, cpu kind, and memory inputs. | +| `cpu` | Set app VM CPU (defaults to 1 cpu). Default 1. | +| `cpukind` | Set app VM CPU kind - shared or performance. Default shared. | +| `memory` | Set app VM memory in megabytes. Default 256. | +| `ha` | Create spare machines that increases app availability. Default `false`. | +| `allow_unsafe_name` | Allow a name that does not contain a PR number. Useful for deploying preview environments across multiple repo with reusable pipelines. Default `false`. | +| `build_args` | Optional Docker --build-arg Separate multiple arguments with a space | +| `build_secrets` | Optional Docker --build-secret | ## Required Secrets @@ -63,7 +64,7 @@ jobs: - name: Deploy id: deploy - uses: superfly/fly-pr-review-apps@1.0.0 + uses: superfly/fly-pr-review-apps@1.3.0 ``` ## Cleaning up GitHub environments diff --git a/action.yml b/action.yml index b5fdbb07..f8fb2d79 100644 --- a/action.yml +++ b/action.yml @@ -42,4 +42,8 @@ inputs: description: Allow a name that does not contain a PR number. Useful for deploying preview environments across multiple repo with reusable pipelines (default false) default: false build_args: - description: Build arguments to pass to the Docker build. Separate multiple arguments with a semicolon. FOO=bar;BAZ=qux + description: Optional Docker --build-arg + build_secrets: + description: Optional Docker --build-secret + environment: + description: Optional environment variables to set on the app. Separate multiple env vars with a space \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh index 0f734d79..e26d6e89 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -24,6 +24,9 @@ region="${INPUT_REGION:-${FLY_REGION:-iad}}" org="${INPUT_ORG:-${FLY_ORG:-personal}}" image="$INPUT_IMAGE" config="${INPUT_CONFIG:-fly.toml}" +build_args="" +build_secrets="" +runtime_environment="" if ! echo "$app" | grep "$PR_NUMBER"; then if [ "$INPUT_ALLOW_UNSAFE_NAME" != "true" ]; then @@ -39,11 +42,31 @@ if [ "$EVENT_TYPE" = "closed" ]; then exit 0 fi +#from https://github.com/superfly/fly-pr-review-apps/pull/50 +# FIXME spaces, maybe split by \D+= +if [ -n "$INPUT_BUILD_ARGS" ]; then + for ARG in $(echo "$INPUT_BUILD_ARGS" | tr " " "\n"); do + build_args="$build_args --build-arg ${ARG}" + done +fi + +if [ -n "$INPUT_BUILD_SECRETS" ]; then + for ARG in $(echo "$INPUT_BUILD_SECRETS" | tr " " "\n"); do + build_secrets="$build_secrets --build-secret ${ARG}" + done +fi + +if [ -n "$INPUT_ENVIRONMENT" ]; then + for ARG in $(echo "$INPUT_ENVIRONMENT" | sed 's/\b\(\w\+\)=/\n\1=/g'); do + runtime_environment="$runtime_environment --env ${ARG}" + done +fi + # Deploy the Fly app, creating it first if needed. if ! flyctl status --app "$app"; then # Backup the original config file since 'flyctl launch' messes up the [build.args] section cp "$config" "$config.bak" - flyctl launch --no-deploy --copy-config --name "$app" --image "$image" --region "$region" --org "$org" + flyctl launch --no-deploy --copy-config --name "$app" --image "$image" --region "$region" --org "$org" ${build_args} ${build_secrets} ${runtime_environment} # Restore the original config file cp "$config.bak" "$config" fi @@ -56,17 +79,12 @@ if [ -n "$INPUT_POSTGRES" ]; then flyctl postgres attach "$INPUT_POSTGRES" --app "$app" || true fi -# create --build-arg array -if [ -n "$INPUT_BUILD_ARGS" ]; then - BUILD_ARGS="--build-arg ${INPUT_BUILD_ARGS//;/ --build-arg }" -fi - # Trigger the deploy of the new version. echo "Contents of config $config file: " && cat "$config" if [ -n "$INPUT_VM" ]; then - flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA --vm-size "$INPUT_VMSIZE" $BUILD_ARGS + flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA ${build_args} ${build_secrets} ${runtime_environment} --vm-size "$INPUT_VMSIZE" else - flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA --vm-cpu-kind "$INPUT_CPUKIND" --vm-cpus $INPUT_CPU --vm-memory "$INPUT_MEMORY" $BUILD_ARGS + flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA ${build_args} ${build_secrets} ${runtime_environment} --vm-cpu-kind "$INPUT_CPUKIND" --vm-cpus $INPUT_CPU --vm-memory "$INPUT_MEMORY" fi # Make some info available to the GitHub workflow. From 2815f0bdccdcb85e6275cb572daaa3985a751210 Mon Sep 17 00:00:00 2001 From: JargeZ Date: Wed, 31 Jul 2024 00:04:32 +1000 Subject: [PATCH 05/14] Remote builder input argument --- README.md | 1 + action.yml | 3 +++ entrypoint.sh | 9 +++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d8a8f4bd..848957af 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ If you have an existing `fly.toml` in your repo, this action will copy it with a | `memory` | Set app VM memory in megabytes. Default 256. | | `ha` | Create spare machines that increases app availability. Default `false`. | | `allow_unsafe_name` | Allow a name that does not contain a PR number. Useful for deploying preview environments across multiple repo with reusable pipelines. Default `false`. | +| `remote_only` | Add --remote-only flag to flyctl deploy command to use remote build. Improve build speed (default false) | | `build_args` | Optional Docker --build-arg Separate multiple arguments with a space | | `build_secrets` | Optional Docker --build-secret | diff --git a/action.yml b/action.yml index f8fb2d79..e2205318 100644 --- a/action.yml +++ b/action.yml @@ -41,6 +41,9 @@ inputs: allow_unsafe_name: description: Allow a name that does not contain a PR number. Useful for deploying preview environments across multiple repo with reusable pipelines (default false) default: false + remote_only: + description: Add --remote-only flag to flyctl deploy command to use remote build. Improve build speed (default false) + default: false build_args: description: Optional Docker --build-arg build_secrets: diff --git a/entrypoint.sh b/entrypoint.sh index e26d6e89..7106c2d8 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -79,12 +79,17 @@ if [ -n "$INPUT_POSTGRES" ]; then flyctl postgres attach "$INPUT_POSTGRES" --app "$app" || true fi +# Use remote builders +if [ -n "$INPUT_REMOTE_ONLY" ]; then + remote_only="--remote-only" +fi + # Trigger the deploy of the new version. echo "Contents of config $config file: " && cat "$config" if [ -n "$INPUT_VM" ]; then - flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA ${build_args} ${build_secrets} ${runtime_environment} --vm-size "$INPUT_VMSIZE" + flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA ${build_args} ${build_secrets} ${runtime_environment} ${remote_only} --vm-size "$INPUT_VMSIZE" else - flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA ${build_args} ${build_secrets} ${runtime_environment} --vm-cpu-kind "$INPUT_CPUKIND" --vm-cpus $INPUT_CPU --vm-memory "$INPUT_MEMORY" + flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA ${build_args} ${build_secrets} ${runtime_environment} ${remote_only} --vm-cpu-kind "$INPUT_CPUKIND" --vm-cpus $INPUT_CPU --vm-memory "$INPUT_MEMORY" fi # Make some info available to the GitHub workflow. From fdc5687872643215b20eb9091a23dc0f07afdd06 Mon Sep 17 00:00:00 2001 From: JargeZ Date: Wed, 21 Aug 2024 14:54:58 +1000 Subject: [PATCH 06/14] Draft: attach redis --- Dockerfile | 2 +- action.yml | 3 +++ entrypoint.sh | 12 ++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 6371b33a..d18b2f50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM alpine -RUN apk add --no-cache curl jq +RUN apk add --no-cache curl jq util-linux RUN curl -L https://fly.io/install.sh | FLYCTL_INSTALL=/usr/local sh diff --git a/action.yml b/action.yml index e2205318..696314df 100644 --- a/action.yml +++ b/action.yml @@ -22,6 +22,9 @@ inputs: description: path to a directory containing a fly.toml to clone postgres: description: Optionally attach the app to a pre-existing Postgres cluster on Fly + redis: + description: Optionally create a new Redis cluster on Fly and attach it to the app (Will destroy automatically) + default: false secrets: description: Secrets to be set on the app. Separate multiple secrets with a space vmsize: diff --git a/entrypoint.sh b/entrypoint.sh index 7106c2d8..84289f7d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -39,6 +39,7 @@ fi # PR was closed - remove the Fly app if one exists and exit. if [ "$EVENT_TYPE" = "closed" ]; then flyctl apps destroy "$app" -y || true + flyctl redis destroy "redis-preview-$app" -y || true exit 0 fi @@ -79,6 +80,17 @@ if [ -n "$INPUT_POSTGRES" ]; then flyctl postgres attach "$INPUT_POSTGRES" --app "$app" || true fi +# Create a Redis instance if requested. +if [ "$INPUT_REDIS" == "true" ]; then + # TODO: issue + # Error: regions codes must be specified in a comma-separated when not running interactively + REDIS_URL=$(script -q -c 'flyctl redis create --enable-eviction --name "redis-preview-'$app'" --region "'$region'" --org "'$org'"' .tmp-out | grep -o "redis://.*") + cat .tmp-out + if [ -n "$REDIS_URL" ]; then + flyctl secrets set "REDIS_URL"="$REDIS_URL" --app "$app" + fi +fi + # Use remote builders if [ -n "$INPUT_REMOTE_ONLY" ]; then remote_only="--remote-only" From 591afd2ba96b475547bb0e433db544fac85e7eda Mon Sep 17 00:00:00 2001 From: JargeZ Date: Thu, 22 Aug 2024 11:18:44 +1000 Subject: [PATCH 07/14] Replace slash --- entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 84289f7d..e6a29102 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -18,8 +18,8 @@ EVENT_TYPE=$(jq -r .action /github/workflow/event.json) # Default the Fly app name to pr-{number}-{repo_owner}-{repo_name} app="${INPUT_NAME:-pr-$PR_NUMBER-$GITHUB_REPOSITORY_OWNER-$GITHUB_REPOSITORY_NAME}" -# Change underscores to hyphens. -app="${app//_/-}" +# Change underscores to hyphens and slashes to hyphens. +app=$(echo "$app" | sed 's/_/-/g' | sed 's/\//-/g') region="${INPUT_REGION:-${FLY_REGION:-iad}}" org="${INPUT_ORG:-${FLY_ORG:-personal}}" image="$INPUT_IMAGE" From c64761ce8113f88d9608db73a0368dacbb928223 Mon Sep 17 00:00:00 2001 From: JargeZ Date: Thu, 22 Aug 2024 15:28:52 +1000 Subject: [PATCH 08/14] Revert "Draft: attach redis" This reverts commit fdc5687872643215b20eb9091a23dc0f07afdd06. --- Dockerfile | 2 +- action.yml | 3 --- entrypoint.sh | 12 ------------ 3 files changed, 1 insertion(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index d18b2f50..6371b33a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM alpine -RUN apk add --no-cache curl jq util-linux +RUN apk add --no-cache curl jq RUN curl -L https://fly.io/install.sh | FLYCTL_INSTALL=/usr/local sh diff --git a/action.yml b/action.yml index 696314df..e2205318 100644 --- a/action.yml +++ b/action.yml @@ -22,9 +22,6 @@ inputs: description: path to a directory containing a fly.toml to clone postgres: description: Optionally attach the app to a pre-existing Postgres cluster on Fly - redis: - description: Optionally create a new Redis cluster on Fly and attach it to the app (Will destroy automatically) - default: false secrets: description: Secrets to be set on the app. Separate multiple secrets with a space vmsize: diff --git a/entrypoint.sh b/entrypoint.sh index e6a29102..5a8be422 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -39,7 +39,6 @@ fi # PR was closed - remove the Fly app if one exists and exit. if [ "$EVENT_TYPE" = "closed" ]; then flyctl apps destroy "$app" -y || true - flyctl redis destroy "redis-preview-$app" -y || true exit 0 fi @@ -80,17 +79,6 @@ if [ -n "$INPUT_POSTGRES" ]; then flyctl postgres attach "$INPUT_POSTGRES" --app "$app" || true fi -# Create a Redis instance if requested. -if [ "$INPUT_REDIS" == "true" ]; then - # TODO: issue - # Error: regions codes must be specified in a comma-separated when not running interactively - REDIS_URL=$(script -q -c 'flyctl redis create --enable-eviction --name "redis-preview-'$app'" --region "'$region'" --org "'$org'"' .tmp-out | grep -o "redis://.*") - cat .tmp-out - if [ -n "$REDIS_URL" ]; then - flyctl secrets set "REDIS_URL"="$REDIS_URL" --app "$app" - fi -fi - # Use remote builders if [ -n "$INPUT_REMOTE_ONLY" ]; then remote_only="--remote-only" From 4d6566e85d6b9320a781fb4fe42f1f8c31dc5c8e Mon Sep 17 00:00:00 2001 From: JargeZ Date: Fri, 23 Aug 2024 14:37:47 +1000 Subject: [PATCH 09/14] Make more generic --- action.yml | 4 ++++ entrypoint.sh | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index e2205318..e5cd7252 100644 --- a/action.yml +++ b/action.yml @@ -10,6 +10,10 @@ runs: inputs: name: description: Fly app name + event_action: + description: Event action name + default: ${{ github.event.action }} + required: true image: description: Optional pre-existing Docker image to use config: diff --git a/entrypoint.sh b/entrypoint.sh index 5a8be422..3ac92eec 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -14,7 +14,7 @@ if [ -z "$PR_NUMBER" ]; then fi GITHUB_REPOSITORY_NAME=${GITHUB_REPOSITORY#$GITHUB_REPOSITORY_OWNER/} -EVENT_TYPE=$(jq -r .action /github/workflow/event.json) +EVENT_TYPE=$INPUT_EVENT_ACTION # Default the Fly app name to pr-{number}-{repo_owner}-{repo_name} app="${INPUT_NAME:-pr-$PR_NUMBER-$GITHUB_REPOSITORY_OWNER-$GITHUB_REPOSITORY_NAME}" From bd8b0f6fae3148cf994b2ce2f142ea38b50cc398 Mon Sep 17 00:00:00 2001 From: JargeZ Date: Wed, 4 Sep 2024 12:56:58 +1000 Subject: [PATCH 10/14] App create and database drop --- README.md | 1 + action.yml | 3 +++ entrypoint.sh | 32 ++++++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 848957af..05473330 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ If you have an existing `fly.toml` in your repo, this action will copy it with a | `org` | Which Fly organization to launch the app under. Alternatively, set the env `FLY_ORG`. Defaults to `personal`. | | `path` | Path to run the `flyctl` commands from. Useful if you have an existing `fly.toml` in a subdirectory. | | `postgres` | Optional name of an existing Postgres cluster to `flyctl postgres attach` to. | +| `postgres_clean_on_close` | Drop database and role on Pull Request close event (default false) | | `update` | Whether or not to update this Fly app when the PR is updated. Default `true`. | | `secrets` | Secrets to be set on the app. Separate multiple secrets with a space | | `vmsize` | Set app VM to a named size, eg. shared-cpu-1x, dedicated-cpu-1x, dedicated-cpu-2x etc. Takes precedence over cpu, cpu kind, and memory inputs. | diff --git a/action.yml b/action.yml index e5cd7252..4c30d5c1 100644 --- a/action.yml +++ b/action.yml @@ -26,6 +26,9 @@ inputs: description: path to a directory containing a fly.toml to clone postgres: description: Optionally attach the app to a pre-existing Postgres cluster on Fly + postgres_clean_on_close: + description: Drop database and role on Pull Request close event (default false) + default: false secrets: description: Secrets to be set on the app. Separate multiple secrets with a space vmsize: diff --git a/entrypoint.sh b/entrypoint.sh index 3ac92eec..6c07a19d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -27,6 +27,8 @@ config="${INPUT_CONFIG:-fly.toml}" build_args="" build_secrets="" runtime_environment="" +database_name="${app/-/_}_pg" +database_role="${app/-/_}_pg" if ! echo "$app" | grep "$PR_NUMBER"; then if [ "$INPUT_ALLOW_UNSAFE_NAME" != "true" ]; then @@ -39,6 +41,15 @@ fi # PR was closed - remove the Fly app if one exists and exit. if [ "$EVENT_TYPE" = "closed" ]; then flyctl apps destroy "$app" -y || true + + if [ "$INPUT_POSTGRES_CLEAN_ON_CLOSE" == "true" && -n "$INPUT_POSTGRES" ]; then + flyctl postgres connect --app "$INPUT_POSTGRES" < Date: Tue, 10 Sep 2024 17:40:53 +1000 Subject: [PATCH 11/14] Awake postgres machine --- entrypoint.sh | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/entrypoint.sh b/entrypoint.sh index 6c07a19d..676a0a4a 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -96,6 +96,38 @@ fi # Attach postgres cluster to the app if specified. if [ -n "$INPUT_POSTGRES" ]; then + pg_status=$(flyctl postgres list | grep "$INPUT_POSTGRES" | awk '{print $3}') + echo "Postgres status: $pg_status" + + if [ "$pg_status" = "suspended" ]; then + echo "Postgres is suspended. Starting it..." + machine_id=$(fly machine list --app "$INPUT_POSTGRES" | tail -n 2 | head -n 1 | awk '{print $1}') + echo "machine_id: $machine_id" + flyctl machine start $machine_id --app "$INPUT_POSTGRES" + fi + + # wait for postgres to be deployed + while [ "$pg_status" != "deployed" ]; do + sleep 3 + pg_status=$(flyctl postgres list | grep "$INPUT_POSTGRES" | awk '{print $3}') + echo "Postgres status: $pg_status" + if [ "$pg_status" = "deployed" ]; then + # avoid Error: no active leader found + sleep 10 + fi + done & + # run loop in background and save pid + pid=$! + + # timeout + sleep 120 && kill $pid && echo "Timeout waiting for postgres to deploy" && exit 1 & + + + # back first job to foreground + fg 1 || true + + echo "Postgres status: $pg_status" + flyctl postgres attach "$INPUT_POSTGRES" --app "$app" --database-name "$database_name" --database-user "$database_role" || true fi From 2045310381f7102ae85c1b63d62dc12261f75de5 Mon Sep 17 00:00:00 2001 From: JargeZ Date: Fri, 25 Oct 2024 11:31:36 +1100 Subject: [PATCH 12/14] Reset db --- action.yml | 3 +++ entrypoint.sh | 25 +++++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/action.yml b/action.yml index 4c30d5c1..b963f33c 100644 --- a/action.yml +++ b/action.yml @@ -51,6 +51,9 @@ inputs: remote_only: description: Add --remote-only flag to flyctl deploy command to use remote build. Improve build speed (default false) default: false + reset_db: + description: Reset the database on each deploy (default false) + default: false build_args: description: Optional Docker --build-arg build_secrets: diff --git a/entrypoint.sh b/entrypoint.sh index 676a0a4a..92c61e53 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -27,8 +27,8 @@ config="${INPUT_CONFIG:-fly.toml}" build_args="" build_secrets="" runtime_environment="" -database_name="${app/-/_}_pg" -database_role="${app/-/_}_pg" +database_name="$(echo "$app" | sed 's/-/_/g')_pg" +database_role="$(echo "$app" | sed 's/-/_/g')_pg" if ! echo "$app" | grep "$PR_NUMBER"; then if [ "$INPUT_ALLOW_UNSAFE_NAME" != "true" ]; then @@ -38,18 +38,21 @@ if ! echo "$app" | grep "$PR_NUMBER"; then echo "WARNING: The app's name does not contain the PR number. it is recommended to include the PR number in the app's name." fi -# PR was closed - remove the Fly app if one exists and exit. -if [ "$EVENT_TYPE" = "closed" ]; then - flyctl apps destroy "$app" -y || true - - if [ "$INPUT_POSTGRES_CLEAN_ON_CLOSE" == "true" && -n "$INPUT_POSTGRES" ]; then +drop_db() { flyctl postgres connect --app "$INPUT_POSTGRES" < Date: Fri, 25 Oct 2024 12:19:11 +1100 Subject: [PATCH 13/14] Revert "Reset db" This reverts commit 2045310381f7102ae85c1b63d62dc12261f75de5. --- action.yml | 3 --- entrypoint.sh | 25 ++++++++----------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/action.yml b/action.yml index b963f33c..4c30d5c1 100644 --- a/action.yml +++ b/action.yml @@ -51,9 +51,6 @@ inputs: remote_only: description: Add --remote-only flag to flyctl deploy command to use remote build. Improve build speed (default false) default: false - reset_db: - description: Reset the database on each deploy (default false) - default: false build_args: description: Optional Docker --build-arg build_secrets: diff --git a/entrypoint.sh b/entrypoint.sh index 92c61e53..676a0a4a 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -27,8 +27,8 @@ config="${INPUT_CONFIG:-fly.toml}" build_args="" build_secrets="" runtime_environment="" -database_name="$(echo "$app" | sed 's/-/_/g')_pg" -database_role="$(echo "$app" | sed 's/-/_/g')_pg" +database_name="${app/-/_}_pg" +database_role="${app/-/_}_pg" if ! echo "$app" | grep "$PR_NUMBER"; then if [ "$INPUT_ALLOW_UNSAFE_NAME" != "true" ]; then @@ -38,21 +38,18 @@ if ! echo "$app" | grep "$PR_NUMBER"; then echo "WARNING: The app's name does not contain the PR number. it is recommended to include the PR number in the app's name." fi -drop_db() { +# PR was closed - remove the Fly app if one exists and exit. +if [ "$EVENT_TYPE" = "closed" ]; then + flyctl apps destroy "$app" -y || true + + if [ "$INPUT_POSTGRES_CLEAN_ON_CLOSE" == "true" && -n "$INPUT_POSTGRES" ]; then flyctl postgres connect --app "$INPUT_POSTGRES" < Date: Thu, 16 Jan 2025 12:36:31 +0700 Subject: [PATCH 14/14] Timeout --- entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 676a0a4a..90169f19 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -120,11 +120,11 @@ if [ -n "$INPUT_POSTGRES" ]; then pid=$! # timeout - sleep 120 && kill $pid && echo "Timeout waiting for postgres to deploy" && exit 1 & + sleep 300 && kill $pid && echo "Timeout waiting for postgres to deploy" && exit 1 & # back first job to foreground - fg 1 || true + wait $pid echo "Postgres status: $pg_status"