From 68154d2d665e47dcd888376d6d4d761f7001c916 Mon Sep 17 00:00:00 2001 From: Artur Trzop Date: Sat, 5 Oct 2019 19:07:15 +0200 Subject: [PATCH 1/5] Add support for Codefresh.io CI provider --- lib/knapsack_pro.rb | 1 + lib/knapsack_pro/config/ci/codefresh.rb | 32 +++++++++ spec/knapsack_pro/config/ci/codefresh_spec.rb | 66 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 lib/knapsack_pro/config/ci/codefresh.rb create mode 100644 spec/knapsack_pro/config/ci/codefresh_spec.rb diff --git a/lib/knapsack_pro.rb b/lib/knapsack_pro.rb index 2cbd84c8..fbd574af 100644 --- a/lib/knapsack_pro.rb +++ b/lib/knapsack_pro.rb @@ -14,6 +14,7 @@ require_relative 'knapsack_pro/config/ci/app_veyor' require_relative 'knapsack_pro/config/ci/circle' require_relative 'knapsack_pro/config/ci/cirrus_ci' +require_relative 'knapsack_pro/config/ci/codefresh' require_relative 'knapsack_pro/config/ci/gitlab_ci' require_relative 'knapsack_pro/config/ci/semaphore' require_relative 'knapsack_pro/config/ci/semaphore2' diff --git a/lib/knapsack_pro/config/ci/codefresh.rb b/lib/knapsack_pro/config/ci/codefresh.rb new file mode 100644 index 00000000..1cd46c56 --- /dev/null +++ b/lib/knapsack_pro/config/ci/codefresh.rb @@ -0,0 +1,32 @@ +# https://codefresh.io/docs/docs/codefresh-yaml/variables/#system-provided-variables +module KnapsackPro + module Config + module CI + class Codefresh < Base + def node_total + # not provided + end + + def node_index + # not provided + end + + def node_build_id + ENV['CF_BUILD_ID'] + end + + def commit_hash + ENV['CF_REVISION'] + end + + def branch + ENV['CF_BRANCH'] + end + + def project_dir + # not provided + end + end + end + end +end diff --git a/spec/knapsack_pro/config/ci/codefresh_spec.rb b/spec/knapsack_pro/config/ci/codefresh_spec.rb new file mode 100644 index 00000000..1f304dc9 --- /dev/null +++ b/spec/knapsack_pro/config/ci/codefresh_spec.rb @@ -0,0 +1,66 @@ +describe KnapsackPro::Config::CI::Codefresh do + let(:env) { {} } + + before do + stub_const('ENV', env) + end + + it { should be_kind_of KnapsackPro::Config::CI::Base } + + describe '#node_total' do + subject { described_class.new.node_total } + + it { should be nil } + end + + describe '#node_index' do + subject { described_class.new.node_index } + + it { should be nil } + end + + describe '#node_build_id' do + subject { described_class.new.node_build_id } + + context 'when environment exists' do + let(:env) { { 'CF_BUILD_ID' => '1005' } } + it { should eql '1005' } + end + + context "when environment doesn't exist" do + it { should be nil } + end + end + + describe '#commit_hash' do + subject { described_class.new.commit_hash } + + context 'when environment exists' do + let(:env) { { 'CF_REVISION' => 'b624067a61d2134df1db74ebdabb1d8d' } } + it { should eql 'b624067a61d2134df1db74ebdabb1d8d' } + end + + context "when environment doesn't exist" do + it { should be nil } + end + end + + describe '#branch' do + subject { described_class.new.branch } + + context 'when environment exists' do + let(:env) { { 'CF_BRANCH' => 'codefresh-branch' } } + it { should eql 'codefresh-branch' } + end + + context "when environment doesn't exist" do + it { should be nil } + end + end + + describe '#project_dir' do + subject { described_class.new.project_dir } + + it { should be nil } + end +end From 460525186f005d07c998ec3d3dbd62bb787adb68 Mon Sep 17 00:00:00 2001 From: Artur Trzop Date: Sat, 5 Oct 2019 19:20:13 +0200 Subject: [PATCH 2/5] Update CHANGELOG.md --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b32a592..e2e15cdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Change Log +### 1.15.0 + +* Add support for Codefresh.io CI provider + + https://github.com/KnapsackPro/knapsack_pro-ruby/pull/92 + +https://github.com/KnapsackPro/knapsack_pro-ruby/compare/v1.14.0...v1.15.0 + ### 1.14.0 * Add support for GitHub Actions From 3e1de1cd46648fb0bd0141b82025dcecd9eaac2f Mon Sep 17 00:00:00 2001 From: Artur Trzop Date: Fri, 11 Oct 2019 13:24:41 +0200 Subject: [PATCH 3/5] Add info for Codefresh.io users --- README.md | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/README.md b/README.md index fc296fea..70543147 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ You can see list of questions for common problems and tips in below [Table of Co - [Info for cirrus-ci.org users](#info-for-cirrus-ciorg-users) - [Info for Jenkins users](#info-for-jenkins-users) - [Info for GitHub Actions users](#info-for-github-actions-users) + - [Info for Codefresh.io users](#info-for-codefreshio-users) - [FAQ](#faq) - [Common problems](#common-problems) - [Why I see API error commit_hash parameter is required?](#why-i-see-api-error-commit_hash-parameter-is-required) @@ -1521,6 +1522,128 @@ jobs: bundle exec rake knapsack_pro:queue:minitest ``` +#### Info for Codefresh.io users + +knapsack_pro gem supports environment variables provided by Codefresh.io to run your tests. You will have to define a few things in `.codefresh/codefresh.yml` config file. + +* You need to set API token like `KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC` in Codefresh dashboard, see left menu Pipelines -> settings (cog icon next to pipeline) -> Variables tab (see vertical menu on the right side). Add there new API token depending on the test runner you use: + * KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC + * KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER + * KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST + * KNAPSACK_PRO_TEST_SUITE_TEST_UNIT + * KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH +* Set where Codefresh YAML file can be found. In Codefresh dashboard, see left menu Pipelines -> settings (cog icon next to pipeline) -> Workflow tab (horizontal menu on the top) -> Path to YAML (set there `./.codefresh/codefresh.yml`). +* Set how many parallel jobs (parallel CI nodes) you want to run with `KNAPSACK_PRO_CI_NODE_TOTAL` environment varible in `.codefresh/codefresh.yml` file. +* Ensure in `matrix` section you listed all `KNAPSACK_PRO_CI_NODE_INDEX` environment variables with value from `0` to `KNAPSACK_PRO_CI_NODE_TOTAL-1`. Codefresh will generate matrix of parallel jobs where each job has different value for `KNAPSACK_PRO_CI_NODE_INDEX`. Thanks to that Knapsack Pro knows what tests should be run on each parallel job. + +Below you can find Codefresh yaml config and `Test.Dockerfile` used by Codefresh to run Ruby on Rails project with PostgreSQL inside of Docker container. + +```yaml +# .codefresh/codefresh.yml +version: "1.0" + +stages: + - "clone" + - "build" + - "tests" + +steps: + main_clone: + type: "git-clone" + description: "Cloning main repository..." + repo: "${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}" + revision: "${{CF_BRANCH}}" + stage: "clone" + BuildTestDockerImage: + title: Building Test Docker image + type: build + arguments: + image_name: '${{CF_ACCOUNT}}/${{CF_REPO_NAME}}-test' + tag: '${{CF_BRANCH_TAG_NORMALIZED}}-${{CF_SHORT_REVISION}}' + dockerfile: Test.Dockerfile + stage: "build" + + run_tests: + stage: "tests" + image: '${{BuildTestDockerImage}}' + working_directory: /src + fail_fast: false + environment: + - RAILS_ENV=test + # set how many parallel jobs you want to run + - KNAPSACK_PRO_CI_NODE_TOTAL=2 + - PGHOST=postgres + - PGUSER=rails-app-with-knapsack_pro + - PGPASSWORD=password + services: + composition: + postgres: + image: postgres:latest + environment: + - POSTGRES_DB=rails-app-with-knapsack_pro_test + - POSTGRES_PASSWORD=password + - POSTGRES_USER=rails-app-with-knapsack_pro + ports: + - 5432 + matrix: + environment: + # please ensure you have here listed N-1 indexes + # where N is KNAPSACK_PRO_CI_NODE_TOTAL + - KNAPSACK_PRO_CI_NODE_INDEX=0 + - KNAPSACK_PRO_CI_NODE_INDEX=1 + commands: + - bin/rails db:prepare + + # run tests in Knapsack Pro Regular Mode + - bundle exec rake knapsack_pro:rspec + - bundle exec rake knapsack_pro:cucumber + - bundle exec rake knapsack_pro:minitest + - bundle exec rake knapsack_pro:test_unit + - bundle exec rake knapsack_pro:spinach + + # you can use Knapsack Pro in Queue Mode once recorded first CI build with Regular Mode + - bundle exec rake knapsack_pro:queue:rspec + - bundle exec rake knapsack_pro:queue:cucumber + - bundle exec rake knapsack_pro:queue:minitest +``` + +Add `Test.Dockerfile` to your project repository. + +```Dockerfile +# Test.Dockerfile +FROM ruby:2.6.5-alpine3.10 + +# Prepare Docker image for Nokogiri +RUN apk add --update \ + build-base \ + libxml2-dev \ + libxslt-dev \ + jq \ + nodejs \ + npm \ + postgresql-dev \ + python3-dev \ + sqlite-dev \ + git \ + && rm -rf /var/cache/apk/* + +# Install AWS CLI +RUN pip3 install awscli + +# Use libxml2, libxslt a packages from alpine for building nokogiri +RUN bundle config build.nokogiri --use-system-libraries + +# Install Codefresh CLI +RUN wget https://github.com/codefresh-io/cli/releases/download/v0.31.1/codefresh-v0.31.1-alpine-x64.tar.gz +RUN tar -xf codefresh-v0.31.1-alpine-x64.tar.gz -C /usr/local/bin/ + +COPY . /src + +WORKDIR /src + +RUN bundle install +``` + ## FAQ ### Common problems From 058db286f2505a3469a04f05a803ddd1f449274f Mon Sep 17 00:00:00 2001 From: Artur Trzop Date: Fri, 11 Oct 2019 13:31:22 +0200 Subject: [PATCH 4/5] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 70543147..3e04bce5 100644 --- a/README.md +++ b/README.md @@ -1527,11 +1527,11 @@ jobs: knapsack_pro gem supports environment variables provided by Codefresh.io to run your tests. You will have to define a few things in `.codefresh/codefresh.yml` config file. * You need to set API token like `KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC` in Codefresh dashboard, see left menu Pipelines -> settings (cog icon next to pipeline) -> Variables tab (see vertical menu on the right side). Add there new API token depending on the test runner you use: - * KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC - * KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER - * KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST - * KNAPSACK_PRO_TEST_SUITE_TEST_UNIT - * KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH + * `KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC` + * `KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER` + * `KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST` + * `KNAPSACK_PRO_TEST_SUITE_TEST_UNIT` + * `KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH` * Set where Codefresh YAML file can be found. In Codefresh dashboard, see left menu Pipelines -> settings (cog icon next to pipeline) -> Workflow tab (horizontal menu on the top) -> Path to YAML (set there `./.codefresh/codefresh.yml`). * Set how many parallel jobs (parallel CI nodes) you want to run with `KNAPSACK_PRO_CI_NODE_TOTAL` environment varible in `.codefresh/codefresh.yml` file. * Ensure in `matrix` section you listed all `KNAPSACK_PRO_CI_NODE_INDEX` environment variables with value from `0` to `KNAPSACK_PRO_CI_NODE_TOTAL-1`. Codefresh will generate matrix of parallel jobs where each job has different value for `KNAPSACK_PRO_CI_NODE_INDEX`. Thanks to that Knapsack Pro knows what tests should be run on each parallel job. From 4731652bb78f6ae334054396a4a296287678edac Mon Sep 17 00:00:00 2001 From: Artur Trzop Date: Fri, 11 Oct 2019 13:59:35 +0200 Subject: [PATCH 5/5] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3e04bce5..059f35d2 100644 --- a/README.md +++ b/README.md @@ -1526,17 +1526,17 @@ jobs: knapsack_pro gem supports environment variables provided by Codefresh.io to run your tests. You will have to define a few things in `.codefresh/codefresh.yml` config file. -* You need to set API token like `KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC` in Codefresh dashboard, see left menu Pipelines -> settings (cog icon next to pipeline) -> Variables tab (see vertical menu on the right side). Add there new API token depending on the test runner you use: +* You need to set an API token like `KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC` in Codefresh dashboard, see left menu Pipelines -> settings (cog icon next to the pipeline) -> Variables tab (see a vertical menu on the right side). Add there new API token depending on the test runner you use: * `KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC` * `KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER` * `KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST` * `KNAPSACK_PRO_TEST_SUITE_TEST_UNIT` * `KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH` * Set where Codefresh YAML file can be found. In Codefresh dashboard, see left menu Pipelines -> settings (cog icon next to pipeline) -> Workflow tab (horizontal menu on the top) -> Path to YAML (set there `./.codefresh/codefresh.yml`). -* Set how many parallel jobs (parallel CI nodes) you want to run with `KNAPSACK_PRO_CI_NODE_TOTAL` environment varible in `.codefresh/codefresh.yml` file. -* Ensure in `matrix` section you listed all `KNAPSACK_PRO_CI_NODE_INDEX` environment variables with value from `0` to `KNAPSACK_PRO_CI_NODE_TOTAL-1`. Codefresh will generate matrix of parallel jobs where each job has different value for `KNAPSACK_PRO_CI_NODE_INDEX`. Thanks to that Knapsack Pro knows what tests should be run on each parallel job. +* Set how many parallel jobs (parallel CI nodes) you want to run with `KNAPSACK_PRO_CI_NODE_TOTAL` environment variable in `.codefresh/codefresh.yml` file. +* Ensure in the `matrix` section you listed all `KNAPSACK_PRO_CI_NODE_INDEX` environment variables with a value from `0` to `KNAPSACK_PRO_CI_NODE_TOTAL-1`. Codefresh will generate a matrix of parallel jobs where each job has a different value for `KNAPSACK_PRO_CI_NODE_INDEX`. Thanks to that Knapsack Pro knows what tests should be run on each parallel job. -Below you can find Codefresh yaml config and `Test.Dockerfile` used by Codefresh to run Ruby on Rails project with PostgreSQL inside of Docker container. +Below you can find Codefresh YAML config and `Test.Dockerfile` used by Codefresh to run Ruby on Rails project with PostgreSQL inside of Docker container. ```yaml # .codefresh/codefresh.yml