Your First Workflow in Kraken CI
Tags: ci, cd, buildautomation, testing
Published: 2021-06-17 04:12:32

[Kraken CI](https://kraken.ci) is a new Continuous Integration tool. It is a modern, open-source, on-premise CI/CD system that is highly scalable and focused on testing. It is licensed under Apache 2.0 license. Its source code is available on [Kraken CI GitHub page](https://github.com/Kraken-CI/kraken).

This tutorial is the second installment of the series of articles about Kraken CI. [Part 1](https://dev.to/godfryd/kraken-ci-new-kid-on-the-ci-block-4imo), Kraken CI, New Kid on the CI block, presented the installation of Kraken. This time we will cover how to prepare a workflow for a simple Python project. The workflow will:
1. fetch sources from GitHub,
1. run tests using Pytest,
1. build a wheel package,
1. upload built package to Kraken's artifacts storage.

Here we are using fresh Kraken installation using Docker Compose with built-in Kraken Agent running in Docker container. Installation procedure details can be found in [part 1](https://dev.to/godfryd/kraken-ci-new-kid-on-the-ci-block-4imo) or in [the installation manual](https://kraken.ci/docs/installation) from [https://kraken.ci](https://kraken.ci) project web page.

#Project Preparation

Details about a new project setup in Kraken CI can be found in the previous article, [part 1](https://dev.to/godfryd/kraken-ci-new-kid-on-the-ci-block-4imo) as well.

Here, in short, let's do:
1. Create a project with a name: *Demo*
1. Create a branch with a name: *main*
1. Use a pre-created stage and rename it to: *Hello World*

#Stage Hello World

The stage should look as follows:

![Branch management page with workflow schema code editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6f5e14o5ctyiymo4f1im.png)

And the original workflow schema code looks as follows:

```python
def stage(ctx):
    return {
        "parent": "root",
        "triggers": {
            "parent": True,
        },
        "parameters": [],
        "configs": [],
        "jobs": [{
            "name": "hello",
            "steps": [{
                "tool": "shell",
                "cmd": "echo 'hello world'"
            }],
            "environments": [{
                "system": "any",
                "agents_group": "all",
                "config": "default"
            }]
        }]
    }
```

Now let's try if it works. Click **Run CI Flow** button.

![Logs from the job with echo Hello World](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmzlt68dxcxyahs4g9ue.png)
 
#Git Repo Checkout Step

Ok, let's start extending our schema to make it more real life. First, let's add a step for checking out a Git repository:

```python
"steps": [{
    "tool": "git",
    "checkout": "https://github.com/Kraken-CI/sample-project-python.git"
}, {
    "tool": "shell",
    "cmd": "ls -al"
}],
```

Now run new CI flow. The output should look like this:

![Logs from the job with git clone](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qp6dff9oeyaenvlw2js.png)

The logs show that the repository has been cloned and **ls -al** shows **sample-project-python** folder.

#Run Tests Step

Now let's replace the step with listing the current directory with something more interesting, like running tests. Kraken provides several built-in tools for running tests. In the case of our Python sample project, we will use **pytest** built-in tool.

```python
"steps": [{
    "tool": "git",
    "checkout": "https://github.com/Kraken-CI/sample-project-python.git"
 }, {
    "tool": "pytest",
    "cwd": "sample-project-python",
    "params": "-vv",
    "pythonpath": "src"
 }],
```

Change the steps in schema editor, save it and run new CI flow. And the job output should be:

![Logs from the job with running Pytest](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fcq16f7k9aqawxx38hpr.png)

#Building Step

So if tests succeeded we could build a package of our Python project. Add step for building a wheel package using project's setup.py script:

```python
"steps": [{
    "tool": "git",
    "checkout": "https://github.com/Kraken-CI/sample-project-python.git"
 }, {
    "tool": "pytest",
    "cwd": "sample-project-python",
    "params": "-vv",
    "pythonpath": "src"
 }, {
    "tool": "shell",
    "cwd": "sample-project-python",
    "cmd": "python3 setup.py sdist bdist_wheel"
 }],
```

Now run CI flow again. The output should look as follows:

![Logs from the job with packaging step with error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lonn3bb9xsrytnmjt5qh.png)
 
It seems that there is missing a package that provides wheel support for Python. Let's install it.

```python
"steps": [{
    "tool": "git",
    "checkout": "https://github.com/Kraken-CI/sample-project-python.git"
 }, {
    "tool": "pytest",
    "cwd": "sample-project-python",
    "params": "-vv",
    "pythonpath": "src"
 }, {
    "tool": "shell",
    "cmd": "sudo apt update && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends python3-wheel python3-setuptools"
 }, {
    "tool": "shell",
    "cwd": "sample-project-python",
    "cmd": "python3 setup.py sdist bdist_wheel"
 }],
```

New output with building:

![Logs from the job with successful deps installation and package building](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7c0ri9bdl2pjvrcdxbtl.png)
 
#Storing Artifacts Step

The results of building the sample project are two packages:
* dist/sampleproject-2.0.0-py3-none-any.whl
* dist/sampleproject-2.0.0.tar.gz

We could store it Kraken storage and make it available for user for downloading. For that purpose there is another built-in tool: *artifacts*.

```python
"steps": [{
    "tool": "git",
    "checkout": "https://github.com/Kraken-CI/sample-project-python.git"
}, {
    "tool": "pytest",
    "cwd": "sample-project-python",
    "params": "-vv",
    "pythonpath": "src"
 }, {
    "tool": "shell",
    "cmd": "sudo apt update && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y - no-install-recommends python3-wheel python3-setuptools"
 }, {
    "tool": "shell",
    "cwd": "sample-project-python",
    "cmd": "python3 setup.py sdist bdist_wheel"
 }, {
    "tool": "artifacts",
    "action": "upload",
    "cwd": "sample-project-python/dist",
    "source": [
        "sampleproject-2.0.0-py3-none-any.whl",
        "sampleproject-2.0.0.tar.gz"
    ],
    "public": True
 }],
```

And the job output:

![Logs from the job with storing build artifacts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o73lih1m4v3uj84phyfk.png)
 
Here the log shows that artifacts have been stored. Now, as they were marked as public in artifacts step, they are available in the UI:

![The Run page, Artifacts tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8e0r1iicelmbixfsxhob.png)
 
This is it. In this installment we learn how to clone Git repository, how to run tests using Pytest, how to install dependencies and how to build artifacts and store them in Kraken's storage. More about Kraken CI you can find on [https://kraken.ci](https://kraken.ci).