Overview
Hello everyone, This article is part of the CircleCI series, The knowledge in this series is built in sequence.
In the last Article #1 setup CircleCI environment we learned how to set up our CircleCI environment and make a simple CI test With very few steps and this proves how CircleCI is easy to start up with.
Today's Example will take a quick view on understanding the YAML file structure by surfing on CricleCI Docs, and Viewing the CircleCI tool capabilities, Also will create a simple pipeline project.
YAML configuration intro
The CI\CD Pipeline line is mainly configured by the config.yaml file.
it's one file for managing the whole pipeline configurations including jobs, parameters, and So forth.
The Introduction to YAML configurations document page provides the entire information about the YAML configuration intro; however, will shortly Get into the core of the subject; meanwhile, you can dig in their documents it's very helpful.
The two main components in The YAML file are Jobs and workflows.
in The Jobs section, you list down your jobs with their name. you can have multiple jobs in the jobs section.
Each job has a steps section, in the steps section, you list down your configuration list, View Screenshot 1.1 below
in screenshot 1.1 we have one Job "say-hello," this Job has a Steps section, and in this section, we have a Run step, Run step provides the ability to run commands on the provided docker container of that job.
Meanwhile, you can replace the docker container with a Linux machine, MacOS, or even Windows, Click on screenshot2.1 for more info.
Also, CircleCI provides prepared docker images that support various programming languages, databases, and Operating systems. That means it'll help you to not prepare your docker images for your prerequisites (i.e. Assuming that you have a Python application, to run it on a docker container you must first install Python, CircleCI provides containers that have these prerequisites for you to start building so fast.)
You can find the all CircleCI docker images list here.
The workflow section is a set of rules that provide the ability to orchestrate the jobs running process, The Jobs, and their run order (i.e. The jobs sequence run, which job should be the first, Which job should be run after another job, etc)
if you didn't get the idea behind the usage of workflows, No worries you will get more info in the next lessons, Just Put that in mind we list the jobs in sequence in the workflow section to start the jobs running process.
Summary
You can have multiple jobs in The Job section.
Each job can have its docker container provided by you, And the CircleCI platform will host and handle this containers for you.
In the Steps section, Will start building your application on The provided container.
In the workflow section, will list the jobs in sequence to start The jobs running process.
Create your 1st pipeline
After understanding The YAML file structure, Now let's start to edit our config.yaml file.
Will configure the pipeline to be:
have 2 jobs, Build and scan.
using the preconfigured Python docker image:
cimg/python:3.11.3
Change directory to python directory.
run the sample Python script.
The Scan job required a Build job, which means The Scan job won't run till the Build job is finished.
Note that, your Repo structure should be like this:
Pipeline code
# Use the latest 2.1 version of CircleCI pipeline process engine.
version: 2.1
jobs:
Build: # Job Name
docker: # in this example will built our code in docker container, however you can use linux, macOS, etc..
- image: cimg/python:3.11.3 # Spcecify The python image.
steps: # Job Steps
- checkout # checkout code from GitHub
- run: python --version # print out python version
- run:
name: "Build Python app"
command: |
#cd python
#python main.py
python --version
Scan: # Job Name
docker: # in this example will built our code in docker container, however you can use linux, macOS, etc..
- image: cimg/python:3.11.3 # Spcecify The python image.
steps: # Job Steps
- checkout # checkout code from GitHub
- run: python --version # print out python version
- run:
name: "Scan Python app"
command: |
#cd python
#python mainscan.py
python --version
# Orchestrate jobs using workflows
# See: https://circleci.com/docs/configuration-reference/#workflows
workflows:
default:
jobs:
- Build
- Scan: # this stage will run after Build stage
requires: [Build]
Type down This YAML code instead the existing one, Press Save and Run button.
This should fire the pipeline.
For a high-level view press on the project name
Then, Press on the workflow name
In the pipeline high-level view, we have two jobs, note that the scan job requires the build job to finish.
Discovery More
On the same page, Collapse the arrow of the rerun button.
note that you have two options to rerun, Rerun the whole pipeline or to Rerun it from the failed job, for sure it's hidden due to we don't have any failed jobs, yet.
If you want to go back to config.yaml files configuration, Click the ellipsis (...) next to Rerun at the top right of the page, and click on configuration file
try to edit the config.yaml file from the GitHub Repo, and note that the CircelCI pipeline gets the new change and start building.
That's it, Hope this article inspired you and will appreciate your feedback.
Thank you.