diff --git a/.ci/Jenkinsfile b/.ci/Jenkinsfile new file mode 100644 index 0000000..8b1296a --- /dev/null +++ b/.ci/Jenkinsfile @@ -0,0 +1,84 @@ +#!/usr/bin/env groovy +@Library('apm@current') _ + +pipeline { + agent { label 'linux && immutable' } + environment { + REPO = 'ecs-logging-ruby' + BASE_DIR = "src/github.com/elastic/${env.REPO}" + DOCKER_REGISTRY = 'docker.elastic.co' + DOCKER_SECRET = 'secret/apm-team/ci/docker-registry/prod' + PIPELINE_LOG_LEVEL = 'INFO' + NOTIFY_TO = credentials('notify-to') + JOB_GCS_BUCKET = credentials('gcs-bucket') + VERSION = '2.7.2' + HOME = "${WORKSPACE}" + PATH = "${WORKSPACE}/.rbenv/bin:${WORKSPACE}/.rbenv/versions/${VERSION}/bin:${PATH}" + } + options { + timeout(time: 2, unit: 'HOURS') + buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '20', daysToKeepStr: '30')) + timestamps() + ansiColor('xterm') + disableResume() + durabilityHint('PERFORMANCE_OPTIMIZED') + rateLimitBuilds(throttle: [count: 60, durationName: 'hour', userBoost: true]) + quietPeriod(10) + } + triggers { + issueCommentTrigger('(?i).*(?:jenkins\\W+)?run\\W+(?:the\\W+)?tests(?:\\W+please)?.*') + } + stages { + stage('Checkout') { + options { skipDefaultCheckout() } + steps { + pipelineManager([ cancelPreviousRunningBuilds: [ when: 'PR' ] ]) + deleteDir() + gitCheckout(basedir: "${BASE_DIR}", githubNotifyFirstTimeContributor: true) + stash allowEmpty: true, name: 'source', useDefaultExcludes: false + } + } + stage('Tests') { + failFast false + matrix { + agent { label 'linux && docker && ubuntu-18.04 && immutable' } + options { skipDefaultCheckout() } + axes { + axis { + name 'RUBY_VERSION' + values 'ruby:2.7', 'ruby:2.6', 'ruby:2.5', 'ruby:2.4', 'ruby:2.3', 'jruby:9.2', 'jruby:9.1' + } + axis { + name 'FRAMEWORK' + values 'rails-6.0', 'rails-5.2', 'rails-5.1', 'sinatra-2.0' + } + } + stages { + stage('Tests') { + steps { + withGithubNotify(context: "Tests-${RUBY_VERSION}-${FRAMEWORK}") { + deleteDir() + unstash 'source' + dir("${BASE_DIR}"){ + dockerLogin(secret: "${DOCKER_SECRET}", registry: "${DOCKER_REGISTRY}") + sh(label: 'install rbenv', script: '.ci/install-rbenv.sh "${VERSION}"') + sh("bin/dev -i${RUBY_VERSION} -f${FRAMEWORK}") + } + } + } + post { + always { + junit(allowEmptyResults: true, keepLongStdio: true, testResults: "${BASE_DIR}/spec/junit-reports/**/*-junit.xml") + } + } + } + } + } + } + } + post { + cleanup { + notifyBuildResult() + } + } +} diff --git a/.ci/install-rbenv.sh b/.ci/install-rbenv.sh new file mode 100755 index 0000000..5e4bfe4 --- /dev/null +++ b/.ci/install-rbenv.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +VERSION=${1:-"2.7.2"} +export PATH="$HOME/.rbenv/bin:$PATH" +git clone https://github.com/rbenv/rbenv.git ~/.rbenv +git clone git@github.com:rbenv/ruby-build.git ~/.rbenv/plugins/ruby-buildCloning +eval "$(rbenv init -)" +rbenv install "${VERSION}" diff --git a/.gitignore b/.gitignore index 1694fee..f4b6c73 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,6 @@ # rspec failure tracking .rspec_status /vendor + +# junit reports folder +spec/junit-reports diff --git a/Dockerfile b/Dockerfile index 3626a63..c06868e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,10 +22,11 @@ ENV BUNDLE_APP_CONFIG=$BUNDLE_PATH \ ENV PATH=/app/bin:$BUNDLE_BIN:$PATH ENV FRAMEWORKS $FRAMEWORKS +ENV RUBY_IMAGE $RUBY_IMAGE -RUN mkdir -p $VENDOR_PATH \ - && chown -R $USER_ID_GROUP $VENDOR_PATH - +# Copy cached folder to speed up docker containers +COPY vendor /vendor +RUN chown -R $USER_ID_GROUP /vendor USER $USER_ID_GROUP # Upgrade RubyGems and install required Bundler version diff --git a/Gemfile b/Gemfile index b06134e..91beafa 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ gemspec gem "rake" gem "rspec" +gem 'yarjuf' gem "rack-test", require: nil gem "sinatra", require: nil diff --git a/Gemfile.lock b/Gemfile.lock index 88a8f6c..8c29edd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,6 +6,7 @@ PATH GEM remote: https://rubygems.org/ specs: + builder (3.2.4) diff-lcs (1.4.4) mustermann (1.1.1) ruby2_keywords (~> 0.0.1) @@ -35,6 +36,9 @@ GEM rack-protection (= 2.1.0) tilt (~> 2.0) tilt (2.0.10) + yarjuf (2.0.0) + builder + rspec (~> 3) PLATFORMS ruby @@ -45,6 +49,7 @@ DEPENDENCIES rake rspec sinatra + yarjuf BUNDLED WITH 2.1.4 diff --git a/README.md b/README.md index 985ba89..f6c0603 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Jenkins](https://apm-ci.elastic.co/buildStatus/icon?job=apm-agent-ruby/ecs-logging-ruby-mbp/master)](https://apm-ci.elastic.co/job/apm-agent-ruby/job/ecs-logging-ruby-mbp/job/master/) # ecs-logging-ruby This set of libraries allows you to transform your application logs to structured logs that comply with the [Elastic Common Schema (ECS)](https://www.elastic.co/guide/en/ecs/current/ecs-reference.html). diff --git a/bin/dev b/bin/dev index 3d8055b..c76ee0f 100755 --- a/bin/dev +++ b/bin/dev @@ -33,7 +33,7 @@ IMAGE_NAME = "apm-agent-ruby:#{IMAGE_PATH_SAFE}" VENDOR_PATH = "/vendor/#{IMAGE_PATH_SAFE}" def run(cmd) - "IMAGE_NAME=#{IMAGE_NAME} #{cmd}".tap do |str| + "IMAGE_NAME=#{IMAGE_NAME} USER_ID_GROUP=#{USER_ID_GROUP} #{cmd}".tap do |str| puts str system str end @@ -49,7 +49,7 @@ unless options[:skip_build] end run 'docker-compose run' \ - " -u #{USER_ID_GROUP}" \ ' --rm' \ " specs #{ARGV.join}" +exit $?.exitstatus unless $?.success? diff --git a/docker-compose.yml b/docker-compose.yml index 9848f99..f070656 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,14 +15,13 @@ services: tty: true volumes: - .:/app:cached - - ./vendor:/vendor tmpfs: - /tmp:exec,mode=1777 - user: ${USER_ID} + user: ${USER_ID_GROUP} ruby_rspec: image: apm-agent-ruby:${RUBY_VERSION} - user: ${USER_ID} + user: ${USER_ID_GROUP} volumes: vendor: diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index c840803..f700466 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -1,17 +1,35 @@ #!/bin/bash set -x +runRspec(){ + local case=${1:-""} + local bn=${case} + + if [ -n "${case}" ]; then + bn="$(basename "${case}")/" + fi + if [ -n "${RUBY_VERSION}" ]; then + bn="$RUBY_VERSION-$bn" + fi + if [ -n "${FRAMEWORKS}" ]; then + bn="$FRAMEWORKS-$bn" + fi + bundle exec rspec \ + -f progress \ + -r yarjuf -f JUnit -o "spec/junit-reports/${bn}ruby-agent-junit.xml" ${case} +} + bundle check || (rm Gemfile.lock && bundle) # If first arg is a spec path, run spec(s) if [[ $1 == spec/* ]]; then - bundle exec rspec $@ + runRspec $@ exit $? fi # If no arguments, run all specs if [[ $# == 0 ]]; then - bundle exec rspec + runRspec exit $? fi diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ce7e6c6..c16f3c4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -22,6 +22,7 @@ Bundler.require :default, :test require "ecs_logging" +require 'yarjuf' RSpec.configure do |config| config.example_status_persistence_file_path = ".rspec_status" diff --git a/vendor/.gitkeep b/vendor/.gitkeep new file mode 100644 index 0000000..e69de29