#6 Orbs cache | General cache

#Circleci #CiCd

ยท

5 min read

#6 Orbs cache | General cache

Inception

Hello everyone, This article is part of the CircleCI series, The knowledge in this series is built in sequence.

Today's example will try to cover The High level of Orbs, Orbs cache, and General Cache. With some code examples to provide how to use it and to get the value it provides.


Orbs Overview

As Defined in Circleci Academy, "CircleCI orbs are shareable packages of configuration elements, including jobs, commands, and executors. Orbs make writing and customizing CircleCI config simple." by defining the Orb in the config file and simply using its commands, you can consider it as built-in functions in any programming language.

Public and Private Orbs

There are two types of Orbs Certified_and_partner, Popular Orbs.
Certified and partner orbs have been built by Circleci itself or its technology partners, and Popular Orbs as you imagine it's built by the community, Visit CircleCI Orbs Page

Therefore you can build your own Orb and you have the ability to Allow access to the public or not, Which means you can have private Orbs just you and your Company use, Or you can make it accessible for the public. Visit manual orb Authoring, and Orb development Kit to build your Own Orb.

So, The Public Orbs its available for any one to use, Meanwhile Private Orbs is just available for you.

Press on public orbs to discover the orbs list


Orbs Caching

Another very useful advantage of using Orbs is Cahing, Orbs enable caching of dependencies, Which means when using Orbs it'll speed up the build progress, the next Code example will provide more understanding.


Orbs Code examples

#1

The config.yaml file below we defined a Python image with a small resouces_Class, Then in steps section will install the depensinces that listed in requrementes.txt file.

Press here to learn more about resouces_Class, and Configuration reference required.

In this build definition each pipeline run will install the dependencies, Thats for sure will slow down the pipeline.

# Use the latest 2.1 version of CircleCI pipeline process engine.
version: 2.1

jobs: 
  Build:

    docker:
      - image: cimg/python:3.11.4
    resource_class: small  # not required

    steps:
      - checkout
      - run: pip install -r python/requirements.txt
      - run: python python/main.py


workflows:
  python_App:
    jobs:
      - Build

Let's build this example togther:

  • Open your config.yaml file, you can open and edit on your local or github codespace, however my favorite place is from CircleCI editor as it'll debug your code while typeing.

    • Now, let's specify our docker image, by opening CircelCI Images.
      here we're running python application, so we need an image have Python preinstalled.

      • Click on cimg/Python image to know how to use it.

        • copy and past this code to your config.yml file

        • Now, Lets discover The GitHub repo files and structure.

  • Run the yaml file, and you should see the steps as below:

    • just note here there's no cache steps by default.

#2

Now let's use Orbs to cache the installed depencies for the future runs.
Will Build the confing.yaml below with the same Python image and resouces_Class, etc.
However, as mentioed will use The Orbs here to provide cache depencies.

# Use the latest 2.1 version of CircleCI pipeline process engine.
version: 2.1

orbs: # python orbs package
  python: circleci/python@2.1.1

jobs: 
  Build:

    docker:
      - image: cimg/python:3.11.4
    resource_class: small 

    steps:
      - checkout

      - python/install-packages:  # using the install-packages from python orbs
          pkg-manager: pip
          app-dir: python/  # If your requirements.txt isn't in the root directory.


      - run: python python/main.py


workflows:
  python_App:
    jobs:
      - Build

Let's build this example togther:

  • Open Orbs page and search for "Python" Orbs package, and select the one provided from CircleCI.

  • 1: make sure that you're using this version.

  • 2: copy and past the orbs code.

  • 3: The Python Orbs Package provide loads of commands (e.g. testing, dist, and install-packages commands) and will use here the install-package command as its provide the caching progress. will pass the installation package -pip- and requirments.txt file path as defined at the yaml file above.

  • Run the yaml file and let's see the result togther.


Orbs cache result discovery

After edit and run our pipeline, now let's discover the pipeline steps result.

  • Press on the Build job.

  • The steps highlighed below has been added to our pipeline:

    To summarize this example, we had used Python Orbs package to cache the dependencies by using the install-package command provided by the Python Orbs itself instead of install the dependencies for each time pipline line running, Thats for sure speed up our Pipline, Meanwhile there're other usage of the Python Orbs package like testing and more.
    If you attending to use another Orbs package it'll be the same as we did, Just search for it and discover it's commands usage.

    Simple like that..
    once you got the idea you do the magic ๐Ÿš€


Caching Overview

Here also we talking about caching, on the opposite of Orbs Caching is a more general caching mechanism, it allows you to cache the depencies between jobs as well, However it's by using save_cache and restore_cache commands. So no need to search and type orbs commands in you yaml file.

get confusied of the diffrence between Orbs cache and cache? get the answer from My Friend Sage.


Cache code example

In this simple example will try to explain how simple use general cache.

# Use the latest 2.1 version of CircleCI pipeline process engine.
version: 2.1

jobs: 
  Build:

    docker:
      - image: cimg/python:3.11.4
    resource_class: small

    steps:
      - checkout

      - restore_cache:  # restore the cache with its key to load 
          keys: [pythonAppBuild]

      - run: pip install -r python/requirements.txt  # install the dependencies
      - run: python python/main.py

      - save_cache:  # save the cache with the provided key below
          paths: [python/save_cache]
          key: pythonAppBuild
workflows:
  python_App:
    jobs:
      - Build

Cache result discover

  • Now, open the job steps to discover, You should see result as the below

Simple like that.. ๐Ÿš€


That's it, Hope this article inspired you and will appreciate your feedback. Thank you.

ย