Skip to content

fix(ci): pipeline compat #1

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

Merged
merged 3 commits into from
Feb 23, 2024
Merged
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
146 changes: 79 additions & 67 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ class RustPlugin {
dockerTag: DEFAULT_DOCKER_TAG,
dockerImage: DEFAULT_DOCKER_IMAGE,
dockerless: false,
strictMode: true,
},
(this.serverless.service.custom && this.serverless.service.custom.rust) ||
{}
{},
);

// Docker can't access resources outside of the current build directory.
Expand All @@ -74,15 +75,13 @@ class RustPlugin {
""
).split(/\s+/);

let target = (funcArgs || {}).target || this.custom.target;

let target = (funcArgs || {}).target || this.custom.target

const targetArgs =
target ?
['--target', target]
: MUSL_PLATFORMS.includes(platform)
? ["--target", "x86_64-unknown-linux-musl"]
: [];
const targetArgs = target
? ["--target", target]
: MUSL_PLATFORMS.includes(platform)
? ["--target", "x86_64-unknown-linux-musl"]
: [];
return [
...defaultArgs,
...profileArgs,
Expand All @@ -94,30 +93,29 @@ class RustPlugin {
localBuildEnv(funcArgs, env, platform) {
const defaultEnv = { ...env };

let target = (funcArgs || {}).target || this.custom.target
let linker = (funcArgs || {}).linker || this.custom.linker
let target = (funcArgs || {}).target || this.custom.target;
let linker = (funcArgs || {}).linker || this.custom.linker;

const platformEnv =
linker ?
{
const platformEnv = linker
? {
RUSTFLAGS: (env["RUSTFLAGS"] || "") + ` -Clinker=${linker}`,
TARGET_CC: linker,
[`CC_${target || 'x86_64_unknown_linux_musl'}`]: linker,
[`CC_${target || "x86_64_unknown_linux_musl"}`]: linker,
}
: "win32" === platform
: "win32" === platform
? {
RUSTFLAGS: (env["RUSTFLAGS"] || "") + " -Clinker=rust-lld",
TARGET_CC: "rust-lld",
CC_x86_64_unknown_linux_musl: "rust-lld",
}
: "darwin" === platform
? {
RUSTFLAGS: (env["RUSTFLAGS"] || "") + " -Clinker=rust-lld",
TARGET_CC: "rust-lld",
CC_x86_64_unknown_linux_musl: "rust-lld",
RUSTFLAGS:
(env["RUSTFLAGS"] || "") + " -Clinker=x86_64-linux-musl-gcc",
TARGET_CC: "x86_64-linux-musl-gcc",
CC_x86_64_unknown_linux_musl: "x86_64-linux-musl-gcc",
}
: "darwin" === platform
? {
RUSTFLAGS:
(env["RUSTFLAGS"] || "") + " -Clinker=x86_64-linux-musl-gcc",
TARGET_CC: "x86_64-linux-musl-gcc",
CC_x86_64_unknown_linux_musl: "x86_64-linux-musl-gcc",
}
: {};
: {};
return {
...defaultEnv,
...platformEnv,
Expand All @@ -127,8 +125,11 @@ class RustPlugin {
localSourceDir(funcArgs, profile, platform) {
let executable = "target";
if (MUSL_PLATFORMS.includes(platform)) {
let target = (funcArgs || {}).target || this.custom.target
executable = path.join(executable, target ? target : "x86_64-unknown-linux-musl");
let target = (funcArgs || {}).target || this.custom.target;
executable = path.join(
executable,
target ? target : "x86_64-unknown-linux-musl",
);
}
return path.join(executable, profile !== "dev" ? "release" : "debug");
}
Expand All @@ -137,7 +138,7 @@ class RustPlugin {
return path.join(
"target",
"lambda",
profile !== "dev" ? "release" : "debug"
profile !== "dev" ? "release" : "debug",
);
}

Expand All @@ -147,7 +148,7 @@ class RustPlugin {
cargoPackage,
binary,
profile,
platform()
platform(),
);

const env = this.localBuildEnv(funcArgs, process.env, platform());
Expand All @@ -169,7 +170,7 @@ class RustPlugin {
"bootstrap",
readFileSync(path.join(sourceDir, binary)),
"",
0o755
0o755,
);
const targetDir = this.localArtifactDir(profile);
try {
Expand All @@ -195,7 +196,7 @@ class RustPlugin {
srcPath,
cargoRegistry,
cargoDownloads,
env
env,
) {
const defaultArgs = [
"run",
Expand Down Expand Up @@ -251,7 +252,7 @@ class RustPlugin {
this.srcPath,
cargoRegistry,
cargoDownloads,
process.env
process.env,
);

this.serverless.cli.log("Running containerized build");
Expand Down Expand Up @@ -281,6 +282,7 @@ class RustPlugin {

/** the entry point for building functions */
build() {
const strictMode = this.custom.strictMode !== false;
const service = this.serverless.service;
if (service.provider.name != "aws") {
return;
Expand All @@ -289,55 +291,65 @@ class RustPlugin {
this.functions().forEach((funcName) => {
const func = service.getFunction(funcName);
const runtime = func.runtime || service.provider.runtime;
if (runtime != RUST_RUNTIME) {

func.tags = func.tags || {};
if (!(runtime === RUST_RUNTIME || func.tags.language === "rust")) {
// skip functions which don't apply to rust
return;
}
rustFunctionsFound = true;
const { cargoPackage, binary } = this.cargoBinary(func);

this.serverless.cli.log(`Building Rust ${func.handler} func...`);
let profile = (func.rust || {}).profile || this.custom.profile;

const res = this.buildLocally(func)
? this.localBuild(func.rust, cargoPackage, binary, profile)
: this.dockerBuild(func.rust, cargoPackage, binary, profile);
if (res.error || res.status > 0) {
func.package = func.package || {};
if (func.package.artifact && func.package.artifact !== "") {
this.serverless.cli.log(
`Rust build encountered an error: ${res.error} ${res.status}.`
`Artifact defined for ${func.handler}, skipping build...`,
);
throw new Error(res.error);
}
// If all went well, we should now have find a packaged compiled binary under `target/lambda/release`.
//
// The AWS "provided" lambda runtime requires executables to be named
// "bootstrap" -- https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html
//
// To avoid artifact naming conflicts when we potentially have more than one function
// we leverage the ability to declare a package artifact directly
// see https://serverless.com/framework/docs/providers/aws/guide/packaging/
// for more information
const artifactPath = path.join(
this.srcPath,
`target/lambda/${"dev" === profile ? "debug" : "release"}`,
`${binary}.zip`
);
func.package = func.package || {};
func.package.artifact = artifactPath;
} else {
const { cargoPackage, binary } = this.cargoBinary(func);

this.serverless.cli.log(`Building Rust ${func.handler} func...`);
let profile = (func.rust || {}).profile || this.custom.profile;

const res = this.buildLocally(func)
? this.localBuild(func.rust, cargoPackage, binary, profile)
: this.dockerBuild(func.rust, cargoPackage, binary, profile);
if (res.error || res.status > 0) {
this.serverless.cli.log(
`Rust build encountered an error: ${res.error} ${res.status}.`,
);
throw new Error(res.error);
}
// If all went well, we should now have find a packaged compiled binary under `target/lambda/release`.
//
// The AWS "provided" lambda runtime requires executables to be named
// "bootstrap" -- https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html
//
// To avoid artifact naming conflicts when we potentially have more than one function
// we leverage the ability to declare a package artifact directly
// see https://serverless.com/framework/docs/providers/aws/guide/packaging/
// for more information
const artifactPath = path.join(
this.srcPath,
`target/lambda/${"dev" === profile ? "debug" : "release"}`,
`${binary}.zip`,
);
func.package = func.package || {};
func.package.artifact = artifactPath;

// Ensure the runtime is set to a sane value for other plugins
if (func.runtime == RUST_RUNTIME) {
func.runtime = BASE_RUNTIME;
// Ensure the runtime is set to a sane value for other plugins
if (func.runtime == RUST_RUNTIME) {
func.runtime = BASE_RUNTIME;
}
}
});
if (service.provider.runtime === RUST_RUNTIME) {
service.provider.runtime = BASE_RUNTIME;
}
if (!rustFunctionsFound) {
if (!rustFunctionsFound && strictMode) {
throw new Error(
`Error: no Rust functions found. ` +
`Use 'runtime: ${RUST_RUNTIME}' in global or ` +
`function configuration to use this plugin.`
`function configuration to use this plugin.`,
);
}
}
Expand Down
Loading