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

Custom config for deployer #3976

Open
wants to merge 5 commits into
base: deployer
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
30 changes: 26 additions & 4 deletions deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
CREATE_AND_PUSH_BRANCH = !get_env("DEPLOY_CREATE_AND_PUSH_BRANCH").nil?
FLYIO_BRANCH_NAME = "flyio-new-files"

DEPLOYER_FLY_CONFIG_PATH = get_env("DEPLOYER_FLY_CONFIG_PATH")
DEPLOYER_SOURCE_CWD = get_env("DEPLOYER_SOURCE_CWD")
DEPLOY_APP_NAME = get_env("DEPLOY_APP_NAME")
if !DEPLOY_CUSTOMIZE && !DEPLOY_APP_NAME
event :error, { type: :validation, message: "missing app name" }
Expand Down Expand Up @@ -89,10 +91,30 @@
head = JSON.parse(exec_capture("git log -1 --pretty=format:'{\"commit\": \"%H\", \"author\": \"%an\", \"author_email\": \"%ae\", \"date\": \"%ad\", \"message\": \"%f\"}'", log: false))

artifact Artifact::GIT_HEAD, head

if !DEPLOYER_SOURCE_CWD.nil?
Dir.chdir(DEPLOYER_SOURCE_CWD)
end

if !DEPLOYER_FLY_CONFIG_PATH.nil? && !File.exists?(DEPLOYER_FLY_CONFIG_PATH)
event :error, { type: :validation, message: "Config file #{DEPLOYER_FLY_CONFIG_PATH} does not exist" }
exit 1
end
end
end

HAS_FLY_CONFIG = Dir.entries(".").any? { |f| File.fnmatch('fly.{toml,json,yaml,yml}', f, File::FNM_EXTGLOB)}
FLY_CONFIG_PATH = if !DEPLOYER_FLY_CONFIG_PATH.nil?
DEPLOYER_FLY_CONFIG_PATH
else
Dir.entries(".").find { |f| File.fnmatch('fly.{toml,json,yaml,yml}', f, File::FNM_EXTGLOB)}
end
HAS_FLY_CONFIG = !FLY_CONFIG_PATH.nil?

CONFIG_COMMAND_STRING = if HAS_FLY_CONFIG
"--config #{FLY_CONFIG_PATH}"
else
""
end

if !DEPLOY_ONLY
MANIFEST_PATH = "/tmp/manifest.json"
Expand Down Expand Up @@ -271,7 +293,7 @@
end

# TODO: better error if missing config
fly_config = manifest && manifest.dig("config") || JSON.parse(exec_capture("flyctl config show --local", log: false))
fly_config = manifest && manifest.dig("config") || JSON.parse(exec_capture("flyctl config show --local #{CONFIG_COMMAND_STRING}", log: false))
APP_NAME = DEPLOY_APP_NAME || fly_config["app"]

image_ref = in_step Step::BUILD do
Expand All @@ -282,7 +304,7 @@
else
image_ref = "registry.fly.io/#{APP_NAME}:#{image_tag}"

exec_capture("flyctl deploy --build-only --push -a #{APP_NAME} --image-label #{image_tag}")
exec_capture("flyctl deploy --build-only --push -a #{APP_NAME} --image-label #{image_tag} #{CONFIG_COMMAND_STRING}")
artifact Artifact::DOCKER_IMAGE, { ref: image_ref }
image_ref
end
Expand Down Expand Up @@ -385,7 +407,7 @@

if DEPLOY_NOW
in_step Step::DEPLOY do
exec_capture("flyctl deploy -a #{APP_NAME} --image #{image_ref}")
exec_capture("flyctl deploy -a #{APP_NAME} --image #{image_ref} #{CONFIG_COMMAND_STRING}")
end
end

Expand Down
25 changes: 25 additions & 0 deletions test/deployer/deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ func TestDeployBasicNode(t *testing.T) {
require.Contains(t, string(body), fmt.Sprintf("Hello, World! %s", deploy.Extra["TEST_ID"].(string)))
}

func TestDeployBasicNodeWithCustomConfigPath(t *testing.T) {
deploy := testDeployer(t,
withFixtureApp("deploy-node-custom-config-path"),
createRandomApp,
withOverwrittenConfig(func(d *testlib.DeployTestRun) map[string]any {
return map[string]any{
"app": d.Extra["appName"],
"region": d.PrimaryRegion(),
"env": map[string]string{
"TEST_ID": d.ID(),
"DEPLOYER_FLY_CONFIG_PATH": "custom-fly-config.toml",
},
}
}),
testlib.DeployOnly,
testlib.DeployNow,
withWorkDirAppSource,
)

body, err := testlib.RunHealthCheck(fmt.Sprintf("https://%s.fly.dev", deploy.Extra["appName"].(string)))
require.NoError(t, err)

require.Contains(t, string(body), fmt.Sprintf("Hello, World! %s", deploy.Extra["TEST_ID"].(string)))
}

func TestLaunchBasicNodeWithDockerfile(t *testing.T) {
deploy := testDeployer(t,
withFixtureApp("deploy-node"),
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/deploy-node-custom-config-path/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.git
/node_modules
.dockerignore
.env
Dockerfile
fly.toml
39 changes: 39 additions & 0 deletions test/fixtures/deploy-node-custom-config-path/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=21.6.2
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="Node.js"

# Node.js app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
RUN npm ci

# Copy application code
COPY --link . .


# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app /app

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD [ "npm", "run", "start" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
app = "{{apps.0}}"
primary_region = '{{region}}'

[build]
dockerfile = 'Dockerfile'

[deploy]
release_command = "sleep 2"

[env]
TEST_ID = "{{test.id}}"

[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = "stop"
auto_start_machines = true
min_machines_running = 0
processes = ['app']

[[http_service.checks]]
grace_period = "5s"
interval = "20s"
method = "GET"
timeout = "5s"
path = "/"

[[vm]]
memory = '1gb'
cpu_kind = 'shared'
cpus = 1
14 changes: 14 additions & 0 deletions test/fixtures/deploy-node-custom-config-path/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const http = require('http');

http.createServer((request, response) => {
response.writeHead(200,
{
'Content-Type': 'text/plain'
}
);

// prints environment variable value
response.write(`Hello, World! ${process.env["TEST_ID"]}\n`);
response.end();

}).listen(8080);
12 changes: 12 additions & 0 deletions test/fixtures/deploy-node-custom-config-path/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test/fixtures/deploy-node-custom-config-path/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "hello-node",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node index.js"
}
}
Empty file.
Loading