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

Add --build-arg and --build-secret #50

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ If you have an existing `fly.toml` in your repo, this action will copy it with a
| `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 |
| `secrets` | Secrets to be set on the app at runtime. Separate multiple secrets with a space |
| `build_args` | Optional Docker --build-arg |

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we add this option we don't need to pass dockerfile option explicitly anymore ?

Copy link
Author

@fredericrous fredericrous May 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the answer to your question is yes. I don't have enough context to give you a straight forward good quality answer tho.
my use case is as follow:

I use build_args to set, inside the image, as an environment variable:

  • the commit sha. I use this because I need to tell apart which sourcemap sent to Sentry correspond to which build
  • a flag autosleep. My Remix server checks at runtime if that flag is there and if so it shuts itself down after a while in order to scale the fly machine down

build_secrets I use to set credentials needed by Sentry to upload the sourcemaps

and finally my dockerfile is the same for build and for production but the generated image is different in production than what's used for build because I make use of FROM more than once in the same dockerfile

FROM can appear multiple times within a single Dockerfile to create multiple images or use one build stage as a dependency for another. Simply make a note of the last image ID output by the commit before each new FROM instruction. Each FROM instruction clears any state created by previous instructions.

https://docs.docker.com/reference/dockerfile/#from

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it , thanks

| `build_secrets` | Optional Docker --build-secret |
| `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. |
Expand Down Expand Up @@ -61,7 +63,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
Expand Down Expand Up @@ -89,7 +91,7 @@ jobs:

- name: Deploy app
id: deploy
uses: superfly/fly-pr-review-apps@1.0.0
uses: superfly/fly-pr-review-apps@1.3.0

- name: Clean up GitHub environment
uses: strumwolf/delete-deployment-environment@v2
Expand All @@ -113,7 +115,7 @@ steps:

- name: Deploy app
id: deploy
uses: superfly/fly-pr-review-apps@1.0.0
uses: superfly/fly-pr-review-apps@1.3.0
with:
postgres: myapp-postgres-staging-apps
```
Expand All @@ -129,7 +131,7 @@ steps:
- uses: actions/checkout@v4

- name: Deploy redis
uses: superfly/fly-pr-review-apps@1.0.0
uses: superfly/fly-pr-review-apps@1.3.0
with:
update: false # Don't need to re-deploy redis when the PR is updated
path: redis # Keep fly.toml in a subdirectory to avoid confusing flyctl
Expand All @@ -138,7 +140,7 @@ steps:

- name: Deploy app
id: deploy
uses: superfly/fly-pr-review-apps@1.0.0
uses: superfly/fly-pr-review-apps@1.3.0
with:
name: pr-${{ github.event.number }}-myapp-app
```
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ inputs:
description: Optional pre-existing Docker image to use
config:
description: Optional path to a custom Fly toml config. Config path should be relative to `path` parameter, if specified.
build_args:
description: Optional Docker --build-arg
build_secrets:
description: Optional Docker --build-secret
region:
description: Region to launch the app in (alternatively, set the env FLY_REGION)
org:
Expand All @@ -23,7 +27,7 @@ inputs:
postgres:
description: Optionally attach the app to a pre-existing Postgres cluster on Fly
secrets:
description: Secrets to be set on the app. Separate multiple secrets with a space
description: Secrets to be set on the app at runtime. Separate multiple secrets with a space
vmsize:
description: 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:
Expand Down
21 changes: 18 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ region="${INPUT_REGION:-${FLY_REGION:-iad}}"
org="${INPUT_ORG:-${FLY_ORG:-personal}}"
image="$INPUT_IMAGE"
config="${INPUT_CONFIG:-fly.toml}"
build_args=""
build_secrets=""

if ! echo "$app" | grep "$PR_NUMBER"; then
echo "For safety, this action requires the app's name to contain the PR number."
Expand All @@ -36,14 +38,27 @@ if [ "$EVENT_TYPE" = "closed" ]; then
exit 0
fi

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

# 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}
# Restore the original config file
cp "$config.bak" "$config"
fi

if [ -n "$INPUT_SECRETS" ]; then
echo $INPUT_SECRETS | tr " " "\n" | flyctl secrets import --app "$app"
fi
Expand All @@ -56,9 +71,9 @@ 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 ${build_args} ${build_secrets} --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"
flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA ${build_args} ${build_secrets} --vm-cpu-kind "$INPUT_CPUKIND" --vm-cpus $INPUT_CPU --vm-memory "$INPUT_MEMORY"
fi

# Make some info available to the GitHub workflow.
Expand Down