From 2f153d67b5fff31ba92609a84f7d541fa9c24aeb Mon Sep 17 00:00:00 2001 From: Mateus Pereira Date: Wed, 12 Feb 2025 14:07:40 -0300 Subject: [PATCH 1/6] Configure code analysis docker image --- .github/workflows/code-analysis.yml | 29 +++++++++++++++++++++++++++++ Dockerfile.analysis | 25 +++++++++++++++++++++++++ entrypoint.rb | 28 ++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 .github/workflows/code-analysis.yml create mode 100644 Dockerfile.analysis create mode 100644 entrypoint.rb diff --git a/.github/workflows/code-analysis.yml b/.github/workflows/code-analysis.yml new file mode 100644 index 00000000..e2fc2eb2 --- /dev/null +++ b/.github/workflows/code-analysis.yml @@ -0,0 +1,29 @@ +name: Code Analysis + +on: + pull_request: + +jobs: + static_analysis: + runs-on: ubuntu-latest + + steps: + - name: Checkout Code + uses: actions/checkout@v3 + + - name: Set Up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Build Docker Image + run: docker build -t code-analysis -f Dockerfile . + + - name: Run Code Analysis + run: | + mkdir -p reports + docker run --rm -v $(pwd)/reports:/app/reports code-analysis + + - name: Upload Reports + uses: actions/upload-artifact@v3 + with: + name: code-analysis-reports + path: reports/ diff --git a/Dockerfile.analysis b/Dockerfile.analysis new file mode 100644 index 00000000..5a74b937 --- /dev/null +++ b/Dockerfile.analysis @@ -0,0 +1,25 @@ +# Use the base Ruby 3.4 image +FROM ruby:3.4 + +# Set working directory +WORKDIR /app + +# Install dependencies +RUN apt-get update && apt-get install -y \ + git \ + curl \ + && rm -rf /var/lib/apt/lists/* + +# Install Bundler (latest version compatible with Ruby 3.4) +RUN gem install rubycritic skunk + +# Copy the entrypoint script +COPY entrypoint.rb /entrypoint.rb + +# Set execute permissions +RUN chmod +x /entrypoint.rb + +VOLUME ["/app"] + +# Set the Ruby script as the entrypoint +ENTRYPOINT ["ruby", "/entrypoint.rb"] diff --git a/entrypoint.rb b/entrypoint.rb new file mode 100644 index 00000000..6ac90aca --- /dev/null +++ b/entrypoint.rb @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby + +# Get arguments passed to the container +commands = ARGV + +# Default behavior: Run both tools if no args are provided +if commands.empty? + puts "No arguments provided. Running both RubyCritic and Skunk..." + system("rubycritic") + system("skunk -o skunk.txt") + exit 0 +end + +# Execute based on provided arguments +commands.each do |command| + case command + when "rubycritic" + puts "Running RubyCritic..." + system("rubycritic") + when "skunk" + puts "Running Skunk..." + system("skunk -o skunk.txt") + else + puts "Invalid argument: #{command}" + puts "Usage: docker run --rm [rubycritic] [skunk]" + exit 1 + end +end From 1ec8d2dc6ad34d3ff0f636a592661029ce64444d Mon Sep 17 00:00:00 2001 From: Mateus Pereira Date: Wed, 12 Feb 2025 14:10:27 -0300 Subject: [PATCH 2/6] Use v4 of upload-artifact action --- .github/workflows/code-analysis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/code-analysis.yml b/.github/workflows/code-analysis.yml index e2fc2eb2..7a82c64b 100644 --- a/.github/workflows/code-analysis.yml +++ b/.github/workflows/code-analysis.yml @@ -23,7 +23,7 @@ jobs: docker run --rm -v $(pwd)/reports:/app/reports code-analysis - name: Upload Reports - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: code-analysis-reports path: reports/ From ac8caa18489e38acf14d710385d2f5a33ce8c7c6 Mon Sep 17 00:00:00 2001 From: Mateus Pereira Date: Wed, 12 Feb 2025 14:11:42 -0300 Subject: [PATCH 3/6] Fix Dockerfile name --- .github/workflows/code-analysis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/code-analysis.yml b/.github/workflows/code-analysis.yml index 7a82c64b..50d14075 100644 --- a/.github/workflows/code-analysis.yml +++ b/.github/workflows/code-analysis.yml @@ -15,7 +15,7 @@ jobs: uses: docker/setup-buildx-action@v2 - name: Build Docker Image - run: docker build -t code-analysis -f Dockerfile . + run: docker build -t code-analysis -f Dockerfile.analysis . - name: Run Code Analysis run: | From fcdce1021e2d8c379561ca14fd49928427c6195d Mon Sep 17 00:00:00 2001 From: Mateus Pereira Date: Wed, 12 Feb 2025 14:19:00 -0300 Subject: [PATCH 4/6] Fix rubycritic and skunk calls to save artifacts in the right folder --- entrypoint.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/entrypoint.rb b/entrypoint.rb index 6ac90aca..093c0d17 100644 --- a/entrypoint.rb +++ b/entrypoint.rb @@ -3,11 +3,18 @@ # Get arguments passed to the container commands = ARGV +def run_rubycritic + system("rubycritic -p reports/rubycritic") +end + +def run_skunk + system("skunk -o reports/skunk.txt") +end + # Default behavior: Run both tools if no args are provided if commands.empty? puts "No arguments provided. Running both RubyCritic and Skunk..." - system("rubycritic") - system("skunk -o skunk.txt") + run_rubycritic exit 0 end @@ -16,10 +23,10 @@ case command when "rubycritic" puts "Running RubyCritic..." - system("rubycritic") + run_rubycritic when "skunk" puts "Running Skunk..." - system("skunk -o skunk.txt") + run_skunk else puts "Invalid argument: #{command}" puts "Usage: docker run --rm [rubycritic] [skunk]" From 4219d325529768edd47c7b79bd358bf338a2d849 Mon Sep 17 00:00:00 2001 From: Mateus Pereira Date: Wed, 12 Feb 2025 14:22:25 -0300 Subject: [PATCH 5/6] Run skunk --- entrypoint.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/entrypoint.rb b/entrypoint.rb index 093c0d17..a5b054f9 100644 --- a/entrypoint.rb +++ b/entrypoint.rb @@ -15,6 +15,7 @@ def run_skunk if commands.empty? puts "No arguments provided. Running both RubyCritic and Skunk..." run_rubycritic + run_skunk exit 0 end From f0482957ccd44838f241176022f8c4ac038edaed Mon Sep 17 00:00:00 2001 From: Mateus Pereira Date: Wed, 12 Feb 2025 21:32:27 -0300 Subject: [PATCH 6/6] Fix code analysis automation config --- .github/workflows/code-analysis.yml | 2 +- Dockerfile.analysis | 42 +++++++++++++-------- bin/run_code_analysis | 34 +++++++++++++++++ entrypoint.rb | 58 +++++++++++++++-------------- 4 files changed, 92 insertions(+), 44 deletions(-) create mode 100755 bin/run_code_analysis diff --git a/.github/workflows/code-analysis.yml b/.github/workflows/code-analysis.yml index 50d14075..df5c2daa 100644 --- a/.github/workflows/code-analysis.yml +++ b/.github/workflows/code-analysis.yml @@ -15,7 +15,7 @@ jobs: uses: docker/setup-buildx-action@v2 - name: Build Docker Image - run: docker build -t code-analysis -f Dockerfile.analysis . + run: docker build --build-arg RUBY_VERSION=3.2.3 -t code-analysis -f Dockerfile.analysis . - name: Run Code Analysis run: | diff --git a/Dockerfile.analysis b/Dockerfile.analysis index 5a74b937..1fd8b466 100644 --- a/Dockerfile.analysis +++ b/Dockerfile.analysis @@ -1,25 +1,37 @@ -# Use the base Ruby 3.4 image -FROM ruby:3.4 +# Use a base image with Ruby, version set by ARG (default: 3.2) +ARG RUBY_VERSION=3.2 +FROM ruby:$RUBY_VERSION -# Set working directory -WORKDIR /app +# Set environment variables +ENV APP_HOME=/app +WORKDIR $APP_HOME -# Install dependencies +# Install necessary packages for Rails and system dependencies RUN apt-get update && apt-get install -y \ - git \ - curl \ + build-essential \ + libpq-dev \ + nodejs \ + yarn \ && rm -rf /var/lib/apt/lists/* -# Install Bundler (latest version compatible with Ruby 3.4) -RUN gem install rubycritic skunk +# Copy the application files +COPY . . -# Copy the entrypoint script -COPY entrypoint.rb /entrypoint.rb +# Install Bundler +RUN gem install bundler + +# Install dependencies +RUN bundle install -# Set execute permissions +# Copy the entrypoint script and set execution permissions +COPY entrypoint.rb /entrypoint.rb RUN chmod +x /entrypoint.rb -VOLUME ["/app"] +# Ensure reports directory exists before declaring a volume +RUN mkdir -p $APP_HOME/reports && chmod 777 $APP_HOME/reports + +# Declare a volume for the reports directory +VOLUME ["/app/reports"] -# Set the Ruby script as the entrypoint -ENTRYPOINT ["ruby", "/entrypoint.rb"] +# Set the entrypoint +ENTRYPOINT ["/entrypoint.rb"] diff --git a/bin/run_code_analysis b/bin/run_code_analysis new file mode 100755 index 00000000..b920d279 --- /dev/null +++ b/bin/run_code_analysis @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby + +require 'optparse' + +# Default Ruby version +ruby_version = "3.2" + +# Parse command-line options +OptionParser.new do |opts| + opts.banner = "Usage: build_and_run.rb --ruby-version " + + opts.on("--ruby-version VERSION", "Specify the Ruby version for the Docker build") do |version| + ruby_version = version + end +end.parse! + +# Docker image name +image_name = "code-analysis" + +puts "🚀 Building Docker image with Ruby #{ruby_version}..." +build_command = "docker build --build-arg RUBY_VERSION=#{ruby_version} -t #{image_name} -f Dockerfile.analysis ." +puts "🔨 Running: #{build_command}" +system(build_command) || abort("❌ Docker build failed!") + +# Ensure reports directory exists on the host +reports_dir = File.expand_path("./reports") +Dir.mkdir(reports_dir) unless Dir.exist?(reports_dir) + +puts "🏃 Running container with reports mounted..." +run_command = "docker run --rm -v #{reports_dir}:/app/reports #{image_name}" +puts "🔧 Running: #{run_command}" +system(run_command) || abort("❌ Docker run failed!") + +puts "✅ Done! Check the 'reports/' directory for analysis results." diff --git a/entrypoint.rb b/entrypoint.rb index a5b054f9..ecd99138 100644 --- a/entrypoint.rb +++ b/entrypoint.rb @@ -1,36 +1,38 @@ #!/usr/bin/env ruby -# Get arguments passed to the container -commands = ARGV +require "bundler" -def run_rubycritic - system("rubycritic -p reports/rubycritic") -end +APP_ROOT = "/app/#{__dir__}" +REPORTS_DIR = File.join(APP_ROOT, "reports") -def run_skunk - system("skunk -o reports/skunk.txt") -end +puts "Checking for rubycritic and skunk in the bundle..." -# Default behavior: Run both tools if no args are provided -if commands.empty? - puts "No arguments provided. Running both RubyCritic and Skunk..." - run_rubycritic - run_skunk - exit 0 +# Check if gems are installed +def gem_installed?(gem_name) + Bundler.locked_gems.dependencies.key?(gem_name) end -# Execute based on provided arguments -commands.each do |command| - case command - when "rubycritic" - puts "Running RubyCritic..." - run_rubycritic - when "skunk" - puts "Running Skunk..." - run_skunk - else - puts "Invalid argument: #{command}" - puts "Usage: docker run --rm [rubycritic] [skunk]" - exit 1 - end +# Add missing gems and install +missing_gems = [] +missing_gems << "rubycritic" unless gem_installed?("rubycritic") +missing_gems << "skunk" unless gem_installed?("skunk") + +unless missing_gems.empty? + puts "Adding missing gems: #{missing_gems.join(", ")}" + bundle_add_command = "bundle add" + missing_gems.each { |gem| bundle_add_command.concat(" #{gem}") } + system(bundle_add_command) + system("bundle install") end + +# Ensure the reports directory exists +# Dir.mkdir(REPORTS_DIR) unless Dir.exist?(REPORTS_DIR) +# File.chmod(0777, REPORTS_DIR) + +puts "Running rubycritic and skunk, saving reports to #{REPORTS_DIR}..." + +# Run rubycritic and skunk, directing output to the reports folder +system("bundle exec rubycritic -p #{REPORTS_DIR}/rubycritic") +system("bundle exec skunk -o #{REPORTS_DIR}/skunk-report.txt") + +puts "Analysis complete. Check the reports folder for results."