From c4173ec2f167ad5f07611378d5808d56a451bc1c Mon Sep 17 00:00:00 2001 From: Kouji Takao Date: Sat, 20 Sep 2025 17:02:54 +0900 Subject: [PATCH 1/4] fix: eliminate lambda_handler redefinition warnings in tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add conditional lambda_handler definition using ENV["CI"] check - Prevent global lambda_handler definitions during test execution - Update Rakefile to set CI=true for all test tasks - Fix mesh zone test to use module method consistently Changes implemented: - All lambda_function.rb files now conditionally define global lambda_handler - Only define lambda_handler when ENV["CI"] != "true" - Rakefile sets CI=true for both :test and :test:lambda tasks - Fixed remaining test that called global lambda_handler directly Test results: - ✅ 29 examples, 0 failures (no warnings) - ✅ All lambda_handler redefinition warnings eliminated - ✅ Local SAM testing still works (CI env not set) - ✅ RSpec testing works cleanly (CI env set to true) This maintains backward compatibility for AWS Lambda deployment while providing a clean testing environment without warnings. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- Rakefile | 4 ++++ lambda/cors-for-smalruby/lambda_function.rb | 6 ++++-- lambda/smalruby-cors-proxy/lambda_function.rb | 6 ++++-- lambda/smalruby-mesh-zone-get/lambda_function.rb | 6 ++++-- .../lambda_function.rb | 6 ++++-- .../smalruby-scratch-api-proxy-translate/lambda_function.rb | 6 ++++-- spec/lambda/smalruby_mesh_zone_get_spec.rb | 4 ++-- 7 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Rakefile b/Rakefile index 71a579d..1d3463f 100644 --- a/Rakefile +++ b/Rakefile @@ -15,11 +15,15 @@ end desc "Run all tests" RSpec::Core::RakeTask.new(:test) do |t| t.pattern = "spec/**/*_spec.rb" + # Set CI environment variable to prevent lambda_handler redefinition warnings + ENV["CI"] = "true" end desc "Run Lambda function tests only" RSpec::Core::RakeTask.new("test:lambda") do |t| t.pattern = "spec/lambda/*_spec.rb" + # Set CI environment variable to prevent lambda_handler redefinition warnings + ENV["CI"] = "true" end desc "Run lint and tests" diff --git a/lambda/cors-for-smalruby/lambda_function.rb b/lambda/cors-for-smalruby/lambda_function.rb index 68f7293..56c711d 100644 --- a/lambda/cors-for-smalruby/lambda_function.rb +++ b/lambda/cors-for-smalruby/lambda_function.rb @@ -24,6 +24,8 @@ def self.lambda_handler(event:, context:) end # AWS Lambda entry point -def lambda_handler(event:, context:) - CorsForSmalruby.lambda_handler(event: event, context: context) +unless ENV["CI"] == "true" + def lambda_handler(event:, context:) + CorsForSmalruby.lambda_handler(event: event, context: context) + end end diff --git a/lambda/smalruby-cors-proxy/lambda_function.rb b/lambda/smalruby-cors-proxy/lambda_function.rb index e2f3569..9d1480f 100644 --- a/lambda/smalruby-cors-proxy/lambda_function.rb +++ b/lambda/smalruby-cors-proxy/lambda_function.rb @@ -199,6 +199,8 @@ def self.is_binary_content?(content_type) end # AWS Lambda entry point -def lambda_handler(event:, context:) - SmalrubyCorsProxy.lambda_handler(event: event, context: context) +unless ENV["CI"] == "true" + def lambda_handler(event:, context:) + SmalrubyCorsProxy.lambda_handler(event: event, context: context) + end end diff --git a/lambda/smalruby-mesh-zone-get/lambda_function.rb b/lambda/smalruby-mesh-zone-get/lambda_function.rb index 29e2468..860591c 100644 --- a/lambda/smalruby-mesh-zone-get/lambda_function.rb +++ b/lambda/smalruby-mesh-zone-get/lambda_function.rb @@ -20,6 +20,8 @@ def self.lambda_handler(event:, context:) end # AWS Lambda entry point -def lambda_handler(event:, context:) - SmalrubyMeshZoneGet.lambda_handler(event: event, context: context) +unless ENV["CI"] == "true" + def lambda_handler(event:, context:) + SmalrubyMeshZoneGet.lambda_handler(event: event, context: context) + end end diff --git a/lambda/smalruby-scratch-api-proxy-get-project-info/lambda_function.rb b/lambda/smalruby-scratch-api-proxy-get-project-info/lambda_function.rb index e99bc4a..2094b27 100644 --- a/lambda/smalruby-scratch-api-proxy-get-project-info/lambda_function.rb +++ b/lambda/smalruby-scratch-api-proxy-get-project-info/lambda_function.rb @@ -30,6 +30,8 @@ def self.lambda_handler(event:, context:) end # AWS Lambda entry point -def lambda_handler(event:, context:) - SmalrubyScratchApiProxyGetProjectInfo.lambda_handler(event: event, context: context) +unless ENV["CI"] == "true" + def lambda_handler(event:, context:) + SmalrubyScratchApiProxyGetProjectInfo.lambda_handler(event: event, context: context) + end end diff --git a/lambda/smalruby-scratch-api-proxy-translate/lambda_function.rb b/lambda/smalruby-scratch-api-proxy-translate/lambda_function.rb index dece040..71413eb 100644 --- a/lambda/smalruby-scratch-api-proxy-translate/lambda_function.rb +++ b/lambda/smalruby-scratch-api-proxy-translate/lambda_function.rb @@ -58,6 +58,8 @@ def self.lambda_handler(event:, context:) end # AWS Lambda entry point -def lambda_handler(event:, context:) - SmalrubyScratchApiProxyTranslate.lambda_handler(event: event, context: context) +unless ENV["CI"] == "true" + def lambda_handler(event:, context:) + SmalrubyScratchApiProxyTranslate.lambda_handler(event: event, context: context) + end end diff --git a/spec/lambda/smalruby_mesh_zone_get_spec.rb b/spec/lambda/smalruby_mesh_zone_get_spec.rb index 5b2dbe0..b0f4e93 100644 --- a/spec/lambda/smalruby_mesh_zone_get_spec.rb +++ b/spec/lambda/smalruby_mesh_zone_get_spec.rb @@ -35,8 +35,8 @@ end it "generates consistent domain for same IP" do - result1 = lambda_handler(event: event, context: context) - result2 = lambda_handler(event: event, context: context) + result1 = SmalrubyMeshZoneGet.lambda_handler(event: event, context: context) + result2 = SmalrubyMeshZoneGet.lambda_handler(event: event, context: context) body1 = JSON.parse(result1[:body]) body2 = JSON.parse(result2[:body]) From 60888a293dd99afc75468a7cc53406a77fc10493 Mon Sep 17 00:00:00 2001 From: Kouji Takao Date: Sat, 20 Sep 2025 17:07:07 +0900 Subject: [PATCH 2/4] feat: implement GitHub Actions CI/CD pipeline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add comprehensive GitHub Actions workflow for automated testing and deployment: CI/CD Pipeline Features: - **Triggers**: Push to main branch and Pull Requests to main - **Multi-job workflow**: lint-and-test → validate-sam-template → deploy Job 1: lint-and-test - Ruby 3.3 setup with bundler cache - Run `bundle exec rake standard` for linting - Run `bundle exec rake test` for comprehensive testing - Upload test results artifacts Job 2: validate-sam-template - AWS SAM CLI setup - Validate SAM template syntax - Build SAM application - Upload build artifacts Job 3: deploy (main branch only) - Deploy to AWS using SAM CLI - Requires AWS credentials from GitHub secrets - Stack name: smalruby-infra-prod - Deploys to ap-northeast-1 region - Upload deployment logs Security & Best Practices: - Environment protection for production deployment - Artifacts retention (7-30 days) - Conditional deployment only on main branch pushes - Uses latest GitHub Actions (v4) This enables automated quality assurance and deployment for the smalruby-infra Lambda functions and API Gateway infrastructure. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .github/workflows/ci-cd.yml | 109 ++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 .github/workflows/ci-cd.yml diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml new file mode 100644 index 0000000..b68c944 --- /dev/null +++ b/.github/workflows/ci-cd.yml @@ -0,0 +1,109 @@ +name: CI/CD Pipeline + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + lint-and-test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.3' + bundler-cache: true + + - name: Install dependencies + run: bundle install + + - name: Run linter (Standard Ruby) + run: bundle exec rake standard + + - name: Run tests + run: bundle exec rake test + + - name: Upload test results + uses: actions/upload-artifact@v4 + if: always() + with: + name: test-results + path: | + coverage/ + tmp/ + retention-days: 7 + + validate-sam-template: + runs-on: ubuntu-latest + needs: lint-and-test + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up AWS SAM CLI + uses: aws-actions/setup-sam@v2 + + - name: Validate SAM template + run: sam validate --template template.yaml + + - name: Build SAM application + run: sam build --template template.yaml + + - name: Upload SAM build artifacts + uses: actions/upload-artifact@v4 + with: + name: sam-build-artifacts + path: .aws-sam/ + retention-days: 7 + + deploy: + runs-on: ubuntu-latest + needs: [lint-and-test, validate-sam-template] + if: github.ref == 'refs/heads/main' && github.event_name == 'push' + environment: production + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up AWS SAM CLI + uses: aws-actions/setup-sam@v2 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ap-northeast-1 + + - name: Download SAM build artifacts + uses: actions/download-artifact@v4 + with: + name: sam-build-artifacts + path: .aws-sam/ + + - name: Deploy to AWS + run: | + sam deploy \ + --template-file .aws-sam/build/template.yaml \ + --stack-name smalruby-infra-prod \ + --parameter-overrides Stage=prod \ + --capabilities CAPABILITY_IAM \ + --no-confirm-changeset \ + --no-fail-on-empty-changeset + + - name: Upload deployment logs + uses: actions/upload-artifact@v4 + if: always() + with: + name: deployment-logs + path: | + .aws-sam/ + retention-days: 30 \ No newline at end of file From 7a8b188ede5534e2d61cf0b140304f51888b611b Mon Sep 17 00:00:00 2001 From: Kouji Takao Date: Sat, 20 Sep 2025 17:10:32 +0900 Subject: [PATCH 3/4] fix: add Ruby setup to validate-sam-template and deploy jobs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SAM build requires Ruby and bundler to be available for Ruby Lambda functions. This fixes the 'No such file or directory: bundle' error in GitHub Actions. Changes: - Add Ruby 3.3 setup to validate-sam-template job - Add Ruby 3.3 setup to deploy job - Enable bundler-cache for faster builds - Ensure consistent Ruby environment across all CI/CD jobs This resolves the RubyBundlerBuilder:RubyBundle error during sam build. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .github/workflows/ci-cd.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index b68c944..53c3ba3 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -47,6 +47,12 @@ jobs: - name: Checkout code uses: actions/checkout@v4 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.3' + bundler-cache: true + - name: Set up AWS SAM CLI uses: aws-actions/setup-sam@v2 @@ -73,6 +79,12 @@ jobs: - name: Checkout code uses: actions/checkout@v4 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.3' + bundler-cache: true + - name: Set up AWS SAM CLI uses: aws-actions/setup-sam@v2 From 24791c95004bd87b903ae5a69049837ca09ff202 Mon Sep 17 00:00:00 2001 From: Kouji Takao Date: Sat, 20 Sep 2025 17:34:53 +0900 Subject: [PATCH 4/4] feat: disable deployment in CI/CD workflow for safety MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Comment out the deploy job in GitHub Actions workflow until manual deployment testing is completed. This ensures we can safely test deployment manually before enabling automated deployment. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .github/workflows/ci-cd.yml | 82 +++++++++++++++---------------------- 1 file changed, 32 insertions(+), 50 deletions(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 53c3ba3..6ca9b7b 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -69,53 +69,35 @@ jobs: path: .aws-sam/ retention-days: 7 - deploy: - runs-on: ubuntu-latest - needs: [lint-and-test, validate-sam-template] - if: github.ref == 'refs/heads/main' && github.event_name == 'push' - environment: production - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: '3.3' - bundler-cache: true - - - name: Set up AWS SAM CLI - uses: aws-actions/setup-sam@v2 - - - name: Configure AWS credentials - uses: aws-actions/configure-aws-credentials@v4 - with: - aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} - aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: ap-northeast-1 - - - name: Download SAM build artifacts - uses: actions/download-artifact@v4 - with: - name: sam-build-artifacts - path: .aws-sam/ - - - name: Deploy to AWS - run: | - sam deploy \ - --template-file .aws-sam/build/template.yaml \ - --stack-name smalruby-infra-prod \ - --parameter-overrides Stage=prod \ - --capabilities CAPABILITY_IAM \ - --no-confirm-changeset \ - --no-fail-on-empty-changeset - - - name: Upload deployment logs - uses: actions/upload-artifact@v4 - if: always() - with: - name: deployment-logs - path: | - .aws-sam/ - retention-days: 30 \ No newline at end of file + # Deploy job is temporarily disabled for initial testing + # Will be enabled after manual deployment verification + # deploy: + # runs-on: ubuntu-latest + # needs: [lint-and-test, validate-sam-template] + # if: github.ref == 'refs/heads/main' && github.event_name == 'push' + # environment: production + # steps: + # - name: Checkout code + # uses: actions/checkout@v4 + # - name: Set up Ruby + # uses: ruby/setup-ruby@v1 + # with: + # ruby-version: '3.3' + # bundler-cache: true + # - name: Set up AWS SAM CLI + # uses: aws-actions/setup-sam@v2 + # - name: Configure AWS credentials + # uses: aws-actions/configure-aws-credentials@v4 + # with: + # aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + # aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + # aws-region: ap-northeast-1 + # - name: Deploy to AWS + # run: | + # sam deploy \ + # --template-file .aws-sam/build/template.yaml \ + # --stack-name smalruby-infra-prod \ + # --parameter-overrides Stage=prod \ + # --capabilities CAPABILITY_IAM \ + # --no-confirm-changeset \ + # --no-fail-on-empty-changeset \ No newline at end of file