Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
123 changes: 123 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 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 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.

```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
Expand Down
1 change: 1 addition & 0 deletions lib/knapsack_pro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
32 changes: 32 additions & 0 deletions lib/knapsack_pro/config/ci/codefresh.rb
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions spec/knapsack_pro/config/ci/codefresh_spec.rb
Original file line number Diff line number Diff line change
@@ -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