From 58b8e06a29a9b566ed3bf791ef4b0d56cfcb7892 Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Sat, 18 Jan 2025 17:11:22 +0100 Subject: [PATCH 01/12] add resources packaging example --- Examples/ResourcesPackaging/.gitignore | 8 ++ Examples/ResourcesPackaging/Package.swift | 27 +++++++ .../ResourcesPackaging/Sources/main.swift | 26 +++++++ Examples/ResourcesPackaging/hello.txt | 1 + scripts/test-plugin-ubuntu.sh | 78 +++++++++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 Examples/ResourcesPackaging/.gitignore create mode 100644 Examples/ResourcesPackaging/Package.swift create mode 100644 Examples/ResourcesPackaging/Sources/main.swift create mode 100644 Examples/ResourcesPackaging/hello.txt create mode 100644 scripts/test-plugin-ubuntu.sh diff --git a/Examples/ResourcesPackaging/.gitignore b/Examples/ResourcesPackaging/.gitignore new file mode 100644 index 00000000..0023a534 --- /dev/null +++ b/Examples/ResourcesPackaging/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/Examples/ResourcesPackaging/Package.swift b/Examples/ResourcesPackaging/Package.swift new file mode 100644 index 00000000..16d4e4be --- /dev/null +++ b/Examples/ResourcesPackaging/Package.swift @@ -0,0 +1,27 @@ +// swift-tools-version: 6.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "ResourcesPackaging", + platforms: [.macOS(.v15)], + products: [ + .executable(name: "MyLambda", targets: ["MyLambda"]), + ], + dependencies: [ + .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"), + ], + targets: [ + .executableTarget( + name: "MyLambda", + dependencies: [ + .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), + ], + path: ".", + resources: [ + .process("hello.txt"), + ] + ), + ] +) diff --git a/Examples/ResourcesPackaging/Sources/main.swift b/Examples/ResourcesPackaging/Sources/main.swift new file mode 100644 index 00000000..aac34ff6 --- /dev/null +++ b/Examples/ResourcesPackaging/Sources/main.swift @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftAWSLambdaRuntime open source project +// +// Copyright (c) 2025 Apple Inc. and the SwiftAWSLambdaRuntime project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import AWSLambdaRuntime +import Foundation + +let runtime = LambdaRuntime { + (event: String, context: LambdaContext) in + guard let fileURL = Bundle.module.url(forResource: "hello", withExtension: "txt") else { + fatalError("no file url") + } + return try String(contentsOf: fileURL, encoding: .utf8) +} + +try await runtime.run() \ No newline at end of file diff --git a/Examples/ResourcesPackaging/hello.txt b/Examples/ResourcesPackaging/hello.txt new file mode 100644 index 00000000..557db03d --- /dev/null +++ b/Examples/ResourcesPackaging/hello.txt @@ -0,0 +1 @@ +Hello World diff --git a/scripts/test-plugin-ubuntu.sh b/scripts/test-plugin-ubuntu.sh new file mode 100644 index 00000000..8a98f0d2 --- /dev/null +++ b/scripts/test-plugin-ubuntu.sh @@ -0,0 +1,78 @@ +#!/bin/bash +##===----------------------------------------------------------------------===## +## +## This source file is part of the SwiftAWSLambdaRuntime open source project +## +## Copyright (c) 2025 Apple Inc. and the SwiftAWSLambdaRuntime project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## + +sudo apt update && sudo apt -y upgrade + +# Install Swift 6.0.3 +sudo apt-get -y install \ + binutils \ + git \ + gnupg2 \ + libc6-dev \ + libcurl4-openssl-dev \ + libedit2 \ + libgcc-13-dev \ + libncurses-dev \ + libpython3-dev \ + libsqlite3-0 \ + libstdc++-13-dev \ + libxml2-dev \ + libz3-dev \ + pkg-config \ + tzdata \ + unzip \ + zip \ + zlib1g-dev + +wget https://download.swift.org/swift-6.0.3-release/ubuntu2404-aarch64/swift-6.0.3-RELEASE/swift-6.0.3-RELEASE-ubuntu24.04-aarch64.tar.gz + +tar xfvz swift-6.0.3-RELEASE-ubuntu24.04-aarch64.tar.gz + +export PATH=/home/ubuntu/swift-6.0.3-RELEASE-ubuntu24.04-aarch64/usr/bin:"${PATH}" + +swift --version + +# Install Docker +sudo apt-get update +sudo apt-get install -y ca-certificates curl +sudo install -m 0755 -d /etc/apt/keyrings +sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc +sudo chmod a+r /etc/apt/keyrings/docker.asc + +# Add the repository to Apt sources: +echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ + $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ + sudo tee /etc/apt/sources.list.d/docker.list > /dev/null +sudo apt-get update + +sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin + +# Add the current user to the docker group +sudo usermod -aG docker $USER + +# LOGOUT and LOGIN to apply the changes +exit + +# reconnect with ssh + +export PATH=/home/ubuntu/swift-6.0.3-RELEASE-ubuntu24.04-aarch64/usr/bin:"${PATH}" + +# clone a project +git clone https://github.com/swift-server/swift-aws-lambda-runtime.git + +# build the project +cd swift-aws-lambda-runtime/Examples/ResourcesPackaging/ +LAMBDA_USE_LOCAL_DEPS=../.. swift package archive --allow-network-connections docker From 02fc6c9bba6ed8fcffa89f92515169f6fa5380fd Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Sat, 18 Jan 2025 17:35:04 +0100 Subject: [PATCH 02/12] [CI[ add ResourcesPackaging to the integration test --- .github/workflows/pull_request.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 2051c091..ec4a7bb4 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -36,7 +36,7 @@ jobs: # We pass the list of examples here, but we can't pass an array as argument # Instead, we pass a String with a valid JSON array. # The workaround is mentioned here https://github.com/orgs/community/discussions/11692 - examples: "[ 'APIGateway', 'APIGateway+LambdaAuthorizer', 'BackgroundTasks', 'HelloJSON', 'HelloWorld', 'S3_AWSSDK', 'S3_Soto', 'Streaming', 'Testing', 'Tutorial' ]" + examples: "[ 'APIGateway', 'APIGateway+LambdaAuthorizer', 'BackgroundTasks', 'HelloJSON', 'HelloWorld', 'ResourcesPackaging', 'S3_AWSSDK', 'S3_Soto', 'Streaming', 'Testing', 'Tutorial' ]" archive_plugin_enabled: true From 84b63af6e39b0fb68b1b6854247bb3ee08badcc5 Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Sat, 18 Jan 2025 17:42:57 +0100 Subject: [PATCH 03/12] fix ci --- .licenseignore | 3 +- Examples/ResourcesPackaging/Package.swift | 10 +++---- .../ResourcesPackaging/Sources/main.swift | 10 +++---- ...ugin-ubuntu.sh => ubuntu-install-swift.sh} | 2 +- scripts/ubuntu-test-plugin.sh | 28 +++++++++++++++++++ 5 files changed, 41 insertions(+), 12 deletions(-) rename scripts/{test-plugin-ubuntu.sh => ubuntu-install-swift.sh} (98%) create mode 100644 scripts/ubuntu-test-plugin.sh diff --git a/.licenseignore b/.licenseignore index db42f1da..d47f45a2 100644 --- a/.licenseignore +++ b/.licenseignore @@ -33,4 +33,5 @@ Package.resolved *.yaml *.yml **/.npmignore -**/*.json \ No newline at end of file +**/*.json +**/*.txt \ No newline at end of file diff --git a/Examples/ResourcesPackaging/Package.swift b/Examples/ResourcesPackaging/Package.swift index 16d4e4be..85f07877 100644 --- a/Examples/ResourcesPackaging/Package.swift +++ b/Examples/ResourcesPackaging/Package.swift @@ -7,21 +7,21 @@ let package = Package( name: "ResourcesPackaging", platforms: [.macOS(.v15)], products: [ - .executable(name: "MyLambda", targets: ["MyLambda"]), + .executable(name: "MyLambda", targets: ["MyLambda"]) ], dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"), + .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main") ], targets: [ .executableTarget( name: "MyLambda", dependencies: [ - .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), + .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime") ], path: ".", resources: [ - .process("hello.txt"), + .process("hello.txt") ] - ), + ) ] ) diff --git a/Examples/ResourcesPackaging/Sources/main.swift b/Examples/ResourcesPackaging/Sources/main.swift index aac34ff6..dccbd863 100644 --- a/Examples/ResourcesPackaging/Sources/main.swift +++ b/Examples/ResourcesPackaging/Sources/main.swift @@ -17,10 +17,10 @@ import Foundation let runtime = LambdaRuntime { (event: String, context: LambdaContext) in - guard let fileURL = Bundle.module.url(forResource: "hello", withExtension: "txt") else { - fatalError("no file url") - } - return try String(contentsOf: fileURL, encoding: .utf8) + guard let fileURL = Bundle.module.url(forResource: "hello", withExtension: "txt") else { + fatalError("no file url") + } + return try String(contentsOf: fileURL, encoding: .utf8) } -try await runtime.run() \ No newline at end of file +try await runtime.run() diff --git a/scripts/test-plugin-ubuntu.sh b/scripts/ubuntu-install-swift.sh similarity index 98% rename from scripts/test-plugin-ubuntu.sh rename to scripts/ubuntu-install-swift.sh index 8a98f0d2..323e1dad 100644 --- a/scripts/test-plugin-ubuntu.sh +++ b/scripts/ubuntu-install-swift.sh @@ -61,7 +61,7 @@ sudo apt-get update sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin # Add the current user to the docker group -sudo usermod -aG docker $USER +sudo usermod -aG docker "$USER" # LOGOUT and LOGIN to apply the changes exit diff --git a/scripts/ubuntu-test-plugin.sh b/scripts/ubuntu-test-plugin.sh new file mode 100644 index 00000000..19d74609 --- /dev/null +++ b/scripts/ubuntu-test-plugin.sh @@ -0,0 +1,28 @@ +#!/bin/bash +##===----------------------------------------------------------------------===## +## +## This source file is part of the SwiftAWSLambdaRuntime open source project +## +## Copyright (c) 2025 Apple Inc. and the SwiftAWSLambdaRuntime project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## + +# Connect with ssh + +export PATH=/home/ubuntu/swift-6.0.3-RELEASE-ubuntu24.04-aarch64/usr/bin:"${PATH}" + +# clone a project +git clone https://github.com/swift-server/swift-aws-lambda-runtime.git + +# be sure Swift is install. +# Youc an install swift with the following command: ./scripts/ubuntu-install-swift.sh + +# build the project +cd swift-aws-lambda-runtime/Examples/ResourcesPackaging/ || exit 1 +LAMBDA_USE_LOCAL_DEPS=../.. swift package archive --allow-network-connections docker From 9fe518a6a841c43f49908855e9c65d2a87b0b87e Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Sat, 18 Jan 2025 17:44:23 +0100 Subject: [PATCH 04/12] fix ci --- scripts/ubuntu-install-swift.sh | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/scripts/ubuntu-install-swift.sh b/scripts/ubuntu-install-swift.sh index 323e1dad..6fac1f7e 100644 --- a/scripts/ubuntu-install-swift.sh +++ b/scripts/ubuntu-install-swift.sh @@ -64,15 +64,4 @@ sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plug sudo usermod -aG docker "$USER" # LOGOUT and LOGIN to apply the changes -exit - -# reconnect with ssh - -export PATH=/home/ubuntu/swift-6.0.3-RELEASE-ubuntu24.04-aarch64/usr/bin:"${PATH}" - -# clone a project -git clone https://github.com/swift-server/swift-aws-lambda-runtime.git - -# build the project -cd swift-aws-lambda-runtime/Examples/ResourcesPackaging/ -LAMBDA_USE_LOCAL_DEPS=../.. swift package archive --allow-network-connections docker +exit 0 From 2cdb3ce403f5b2507e27c307f830ef634d4d3471 Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Sat, 18 Jan 2025 17:52:10 +0100 Subject: [PATCH 05/12] fix ci --- scripts/ubuntu-install-swift.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/ubuntu-install-swift.sh b/scripts/ubuntu-install-swift.sh index 6fac1f7e..105e6106 100644 --- a/scripts/ubuntu-install-swift.sh +++ b/scripts/ubuntu-install-swift.sh @@ -52,9 +52,11 @@ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyring sudo chmod a+r /etc/apt/keyrings/docker.asc # Add the repository to Apt sources: +# shellcheck source=/etc/os-release +. /etc/os-release echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ - $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ + "$VERSION_CODENAME" stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update From bab98987887a0d2ef2fb537a34d25d7e6d778ff6 Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Sat, 18 Jan 2025 17:54:18 +0100 Subject: [PATCH 06/12] fix ci --- scripts/ubuntu-install-swift.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/ubuntu-install-swift.sh b/scripts/ubuntu-install-swift.sh index 105e6106..5ff58f46 100644 --- a/scripts/ubuntu-install-swift.sh +++ b/scripts/ubuntu-install-swift.sh @@ -53,10 +53,11 @@ sudo chmod a+r /etc/apt/keyrings/docker.asc # Add the repository to Apt sources: # shellcheck source=/etc/os-release +# shellcheck disable=SC1091 . /etc/os-release echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ - "$VERSION_CODENAME" stable" | \ + $VERSION_CODENAME stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update From 5802656e64fdfbfae0b00ad1d6b55c04d31ba94d Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Sat, 18 Jan 2025 22:21:47 +0100 Subject: [PATCH 07/12] temp workaround for issue #449 --- Plugins/AWSLambdaPackager/Plugin.swift | 28 +++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/Plugins/AWSLambdaPackager/Plugin.swift b/Plugins/AWSLambdaPackager/Plugin.swift index a8693945..f6ac2b03 100644 --- a/Plugins/AWSLambdaPackager/Plugin.swift +++ b/Plugins/AWSLambdaPackager/Plugin.swift @@ -249,11 +249,29 @@ struct AWSLambdaPackager: CommandPlugin { let resourcesDirectoryName = artifactURL.lastPathComponent let relocatedResourcesDirectory = workingDirectory.appending(path: resourcesDirectoryName) if FileManager.default.fileExists(atPath: artifactURL.path()) { - try FileManager.default.copyItem( - atPath: artifactURL.path(), - toPath: relocatedResourcesDirectory.path() - ) - arguments.append(resourcesDirectoryName) + do { + try FileManager.default.copyItem( + atPath: artifactURL.path(), + toPath: relocatedResourcesDirectory.path() + ) + arguments.append(resourcesDirectoryName) + } catch let error as CocoaError { + + // On Linux, when the build has been done with Docker, + // the source file are owned by root + // this causes a permission error **after** the files have been copied + // see https://github.com/swift-server/swift-aws-lambda-runtime/issues/449 + // see https://forums.swift.org/t/filemanager-copyitem-on-linux-fails-after-copying-the-files/77282 + + // because this error happens after the files have been copied, we can ignore it + // this code checks if the destination file exists + // if they do, just ignore error, otherwise throw it up to the caller. + if !(error.code == CocoaError.Code.fileWriteNoPermission + && FileManager.default.fileExists(atPath: relocatedResourcesDirectory.path())) + { + throw error + } // else just ignore it + } } } From 86149fddc18db95ad349cf89d50a93665be5720e Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Sat, 18 Jan 2025 22:33:31 +0100 Subject: [PATCH 08/12] add integration test to check the resources inside the package --- .../workflows/scripts/check-archive-plugin.sh | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/.github/workflows/scripts/check-archive-plugin.sh b/.github/workflows/scripts/check-archive-plugin.sh index 3a15c138..dd7f2003 100755 --- a/.github/workflows/scripts/check-archive-plugin.sh +++ b/.github/workflows/scripts/check-archive-plugin.sh @@ -13,25 +13,40 @@ ## ##===----------------------------------------------------------------------===## -EXAMPLE=HelloWorld -OUTPUT_DIR=.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager -OUTPUT_FILE=${OUTPUT_DIR}/MyLambda/bootstrap -ZIP_FILE=${OUTPUT_DIR}/MyLambda/MyLambda.zip +check_archive_plugin() { + local EXAMPLE=$1 + OUTPUT_DIR=.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager + OUTPUT_FILE=${OUTPUT_DIR}/MyLambda/bootstrap + ZIP_FILE=${OUTPUT_DIR}/MyLambda/MyLambda.zip -pushd Examples/${EXAMPLE} || exit 1 + pushd Examples/${EXAMPLE} || exit 1 -# package the example (docker and swift toolchain are installed on the GH runner) -LAMBDA_USE_LOCAL_DEPS=../.. swift package archive --allow-network-connections docker || exit 1 + # package the example (docker and swift toolchain are installed on the GH runner) + LAMBDA_USE_LOCAL_DEPS=../.. swift package archive --allow-network-connections docker || exit 1 -# did the plugin generated a Linux binary? -[ -f "${OUTPUT_FILE}" ] -file "${OUTPUT_FILE}" | grep --silent ELF + # did the plugin generated a Linux binary? + [ -f "${OUTPUT_FILE}" ] + file "${OUTPUT_FILE}" | grep --silent ELF -# did the plugin created a ZIP file? -[ -f "${ZIP_FILE}" ] + # did the plugin created a ZIP file? + [ -f "${ZIP_FILE}" ] -# does the ZIP file contain the bootstrap? -unzip -l "${ZIP_FILE}" | grep --silent bootstrap + # does the ZIP file contain the bootstrap? + unzip -l "${ZIP_FILE}" | grep --silent bootstrap -echo "✅ The archive plugin is OK" -popd || exit 1 + # if EXAMPLE is ResourcesPackaging, check if the ZIP file contains hello.txt + if [ "$EXAMPLE" == "ResourcesPackaging" ]; then + unzip -l "${ZIP_FILE}" | grep --silent hello.txt + fi + + echo "✅ The archive plugin is OK with example ${EXAMPLE}" + popd || exit 1 +} + +# List of examples +EXAMPLES=("HelloWorld" "ResourcesPackaging") + +# Iterate over each example and call check_archive_plugin +for EXAMPLE in "${EXAMPLES[@]}"; do + check_archive_plugin "$EXAMPLE" +done \ No newline at end of file From fa2a6e3eaef0cfe471096417cb7d58b121dc055d Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Sat, 18 Jan 2025 22:37:04 +0100 Subject: [PATCH 09/12] fix ci --- .github/workflows/scripts/check-archive-plugin.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/scripts/check-archive-plugin.sh b/.github/workflows/scripts/check-archive-plugin.sh index dd7f2003..9535c549 100755 --- a/.github/workflows/scripts/check-archive-plugin.sh +++ b/.github/workflows/scripts/check-archive-plugin.sh @@ -19,7 +19,7 @@ check_archive_plugin() { OUTPUT_FILE=${OUTPUT_DIR}/MyLambda/bootstrap ZIP_FILE=${OUTPUT_DIR}/MyLambda/MyLambda.zip - pushd Examples/${EXAMPLE} || exit 1 + pushd "Examples/${EXAMPLE}" || exit 1 # package the example (docker and swift toolchain are installed on the GH runner) LAMBDA_USE_LOCAL_DEPS=../.. swift package archive --allow-network-connections docker || exit 1 From 1c4527b0dbd5799728a38048ada6c97b9804810d Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Sat, 18 Jan 2025 23:06:48 +0100 Subject: [PATCH 10/12] fix ci --- .github/workflows/integration_tests.yml | 15 +++++- .github/workflows/pull_request.yml | 4 +- .../workflows/scripts/check-archive-plugin.sh | 51 +++++++++---------- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/.github/workflows/integration_tests.yml b/.github/workflows/integration_tests.yml index 468fc295..1a67f637 100644 --- a/.github/workflows/integration_tests.yml +++ b/.github/workflows/integration_tests.yml @@ -19,6 +19,11 @@ on: type: boolean description: "Boolean to enable the compilation of examples. Defaults to true." default: true + archive_plugin_examples: + type: string + description: "The list of examples to run through the archive plugin test. Pass a String with a valid JSON array such as \"[ 'HelloWorld', 'APIGateway' ]\"" + required: true + default: "" archive_plugin_enabled: type: boolean description: "Boolean to enable the test of the archive plugin. Defaults to true." @@ -54,7 +59,7 @@ jobs: # We are using only one Swift version swift: - image: ${{ inputs.matrix_linux_swift_container_image }} - swift_version: "6.0.1-amazonlinux2" + swift_version: "6.0.3-amazonlinux2" container: image: ${{ matrix.swift.image }} steps: @@ -98,6 +103,10 @@ jobs: name: Test archive plugin if: ${{ inputs.archive_plugin_enabled }} runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + examples: ${{ fromJson(inputs.archive_plugin_examples) }} steps: - name: Checkout repository uses: actions/checkout@v4 @@ -107,8 +116,10 @@ jobs: # https://github.com/actions/checkout/issues/766 run: git config --global --add safe.directory ${GITHUB_WORKSPACE} - name: Test the archive plugin + env: + EXAMPLE: ${{ matrix.examples }} run: | - .github/workflows/scripts/check-archive-plugin.sh + .github/workflows/scripts/check-archive-plugin.sh check-foundation: name: No dependencies on Foundation diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index ec4a7bb4..1b58d375 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -36,8 +36,8 @@ jobs: # We pass the list of examples here, but we can't pass an array as argument # Instead, we pass a String with a valid JSON array. # The workaround is mentioned here https://github.com/orgs/community/discussions/11692 - examples: "[ 'APIGateway', 'APIGateway+LambdaAuthorizer', 'BackgroundTasks', 'HelloJSON', 'HelloWorld', 'ResourcesPackaging', 'S3_AWSSDK', 'S3_Soto', 'Streaming', 'Testing', 'Tutorial' ]" - + examples: "[ 'APIGateway', 'APIGateway+LambdaAuthorizer', 'BackgroundTasks', 'HelloJSON', 'HelloWorld', 'ResourcePackaging', 'S3_AWSSDK', 'S3_Soto', 'Streaming', 'Testing', 'Tutorial' ]" + archive_plugin_examples: "[ 'HelloWorld', 'ResourcePackaging' ]" archive_plugin_enabled: true swift-6-language-mode: diff --git a/.github/workflows/scripts/check-archive-plugin.sh b/.github/workflows/scripts/check-archive-plugin.sh index 9535c549..3c127be8 100755 --- a/.github/workflows/scripts/check-archive-plugin.sh +++ b/.github/workflows/scripts/check-archive-plugin.sh @@ -13,40 +13,35 @@ ## ##===----------------------------------------------------------------------===## -check_archive_plugin() { - local EXAMPLE=$1 - OUTPUT_DIR=.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager - OUTPUT_FILE=${OUTPUT_DIR}/MyLambda/bootstrap - ZIP_FILE=${OUTPUT_DIR}/MyLambda/MyLambda.zip +log() { printf -- "** %s\n" "$*" >&2; } +error() { printf -- "** ERROR: %s\n" "$*" >&2; } +fatal() { error "$@"; exit 1; } - pushd "Examples/${EXAMPLE}" || exit 1 +test -n "${EXAMPLE:-}" || fatal "EXAMPLE unset" - # package the example (docker and swift toolchain are installed on the GH runner) - LAMBDA_USE_LOCAL_DEPS=../.. swift package archive --allow-network-connections docker || exit 1 +OUTPUT_DIR=.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager +OUTPUT_FILE=${OUTPUT_DIR}/MyLambda/bootstrap +ZIP_FILE=${OUTPUT_DIR}/MyLambda/MyLambda.zip - # did the plugin generated a Linux binary? - [ -f "${OUTPUT_FILE}" ] - file "${OUTPUT_FILE}" | grep --silent ELF +pushd "Examples/${EXAMPLE}" || exit 1 - # did the plugin created a ZIP file? - [ -f "${ZIP_FILE}" ] +# package the example (docker and swift toolchain are installed on the GH runner) +LAMBDA_USE_LOCAL_DEPS=../.. swift package archive --allow-network-connections docker || exit 1 - # does the ZIP file contain the bootstrap? - unzip -l "${ZIP_FILE}" | grep --silent bootstrap +# did the plugin generated a Linux binary? +[ -f "${OUTPUT_FILE}" ] +file "${OUTPUT_FILE}" | grep --silent ELF - # if EXAMPLE is ResourcesPackaging, check if the ZIP file contains hello.txt - if [ "$EXAMPLE" == "ResourcesPackaging" ]; then - unzip -l "${ZIP_FILE}" | grep --silent hello.txt - fi +# did the plugin created a ZIP file? +[ -f "${ZIP_FILE}" ] - echo "✅ The archive plugin is OK with example ${EXAMPLE}" - popd || exit 1 -} +# does the ZIP file contain the bootstrap? +unzip -l "${ZIP_FILE}" | grep --silent bootstrap -# List of examples -EXAMPLES=("HelloWorld" "ResourcesPackaging") +# if EXAMPLE is ResourcesPackaging, check if the ZIP file contains hello.txt +if [ "$EXAMPLE" == "ResourcesPackaging" ]; then + unzip -l "${ZIP_FILE}" | grep --silent hello.txt +fi -# Iterate over each example and call check_archive_plugin -for EXAMPLE in "${EXAMPLES[@]}"; do - check_archive_plugin "$EXAMPLE" -done \ No newline at end of file +echo "✅ The archive plugin is OK with example ${EXAMPLE}" +popd || exit 1 From 466f46eea34498ae2e9285401e2d304dd3fe2288 Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Sat, 18 Jan 2025 23:08:31 +0100 Subject: [PATCH 11/12] fix ci --- .github/workflows/pull_request.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 1b58d375..72c2a24c 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -36,8 +36,8 @@ jobs: # We pass the list of examples here, but we can't pass an array as argument # Instead, we pass a String with a valid JSON array. # The workaround is mentioned here https://github.com/orgs/community/discussions/11692 - examples: "[ 'APIGateway', 'APIGateway+LambdaAuthorizer', 'BackgroundTasks', 'HelloJSON', 'HelloWorld', 'ResourcePackaging', 'S3_AWSSDK', 'S3_Soto', 'Streaming', 'Testing', 'Tutorial' ]" - archive_plugin_examples: "[ 'HelloWorld', 'ResourcePackaging' ]" + examples: "[ 'APIGateway', 'APIGateway+LambdaAuthorizer', 'BackgroundTasks', 'HelloJSON', 'HelloWorld', 'ResourcesPackaging', 'S3_AWSSDK', 'S3_Soto', 'Streaming', 'Testing', 'Tutorial' ]" + archive_plugin_examples: "[ 'HelloWorld', 'ResourcesPackaging' ]" archive_plugin_enabled: true swift-6-language-mode: From 799af6a42dcbff0970cbc27602be1c6bb5d7e54d Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Sat, 18 Jan 2025 23:13:06 +0100 Subject: [PATCH 12/12] fix ci --- Examples/ResourcesPackaging/Package.swift | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Examples/ResourcesPackaging/Package.swift b/Examples/ResourcesPackaging/Package.swift index 85f07877..4680b74a 100644 --- a/Examples/ResourcesPackaging/Package.swift +++ b/Examples/ResourcesPackaging/Package.swift @@ -3,6 +3,9 @@ import PackageDescription +// needed for CI to test the local version of the library +import struct Foundation.URL + let package = Package( name: "ResourcesPackaging", platforms: [.macOS(.v15)], @@ -25,3 +28,30 @@ let package = Package( ) ] ) + +if let localDepsPath = Context.environment["LAMBDA_USE_LOCAL_DEPS"], + localDepsPath != "", + let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]), + v.isDirectory == true +{ + // when we use the local runtime as deps, let's remove the dependency added above + let indexToRemove = package.dependencies.firstIndex { dependency in + if case .sourceControl( + name: _, + location: "https://github.com/swift-server/swift-aws-lambda-runtime.git", + requirement: _ + ) = dependency.kind { + return true + } + return false + } + if let indexToRemove { + package.dependencies.remove(at: indexToRemove) + } + + // then we add the dependency on LAMBDA_USE_LOCAL_DEPS' path (typically ../..) + print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)") + package.dependencies += [ + .package(name: "swift-aws-lambda-runtime", path: localDepsPath) + ] +}