#5 Commands | Parameters | and Pipeline values

#CircleCI #cicd

#5 Commands | Parameters | and Pipeline values

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 Commands, Parameters, and Pipeline values "env.var" With some code examples to provide how to use it.

Commands Overview

Commands allow you the ability to consolidate the list of commands in a single command. and call it in the pipeline. Consider it as a function, Function is a block of code, that sets some related commands in a Function. and will run when just call it in the pipeline.

Commands Code discovery

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

commands:  #1
  install_awscli:
    description: Install AWS CLI v2
    steps:
      - run:
          name: Install AWS CLI v2
          command: |
            curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
            unzip awscliv2.zip
            sudo ./aws/install

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
      - install_awscli  #2
      - run: aws --version  #3
      - run: python --version  # print out python version
      - run:
          name: "Build Python app"
          command: |
            cd python
            python3 main.py

      - run: mkdir -p workspace 
      - run: echo "Hello, Workspace" > workspace/echo-output

      - persist_to_workspace:
          root: workspace
          paths:
            - echo-output


  Scan_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: "Scan Python app"
          command: |
            cd python
            python3 main_scan.py

      - attach_workspace:
          at: /tmp/workspace
      - run: cat  /tmp/workspace/echo-output

  Deploy:  # 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
      - install_awscli
      - run: aws --version
      - run: python --version  # print out python version
      - run:
          name: "Build Python app"
          command: |
            cd python
            python3 main.py 

      - attach_workspace:
          at: /tmp/workspace
      - run: cat  /tmp/workspace/echo-output

  Scan_Deploy:  # 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
            python3 main_scan.py

      - attach_workspace:
          at: /tmp/workspace
      - run: cat  /tmp/workspace/echo-output


# Orchestrate jobs using workflows
# See: https://circleci.com/docs/configuration-reference/#workflows
workflows:
  default:
    jobs:
      - Build

      - Scan_Build:
          requires: [Build]

      - Deploy:
          requires: [Build]

      - Scan_Deploy:
          requires: [Deploy, Scan_Build]

Code explanation

  • #1 Define AWSCLI code installation in the install_awscli command

  • #2 Call the list of commands in this job

  • #3 Print out the AWS_CLI version


Parameters Overview

Parameters Allow you to pass some values to the pipeline to use in pipeline jobs.

CircleCI parameters support these types (String, boolean, integer, etc..)

Press CircleCI Docs for more info.

Parameters code discovery

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

parameters: #1
  release-tag:
    type: string
    default: "release name"
  working-dir:
    type: string
    default: "path/path"

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.

    environment: #2
      RELEASENAME: << pipeline.parameters.release-tag >>
      WORKINGDIR: << pipeline.parameters.working-dir >>

    steps:  # Job Steps
      - checkout  # checkout code from GitHub
      - run: python --version  # print out python version
      - run:
          name: "Build Python app"
          command: |
            cd python
            python3 main.py

      - run: mkdir -p workspace 
      - run: echo "Hello, Workspace" > workspace/echo-output

      #3
      #3 call parameters section     
      - run:
          name: "call parameters"
          command: |
            echo "release name is ${RELEASENAME}"
            echo "Working directory is ${WORKINGDIR}"

      - persist_to_workspace:
          root: workspace
          paths:
            - echo-output


  Scan_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: "Scan Python app"
          command: |
            cd python
            python3 main_scan.py

      - attach_workspace:
          at: /tmp/workspace
      - run: cat  /tmp/workspace/echo-output

  Deploy:  # 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
            python3 main.py 

      - attach_workspace:
          at: /tmp/workspace
      - run: cat  /tmp/workspace/echo-output

  Scan_Deploy:  # 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
            python3 main_scan.py

      - attach_workspace:
          at: /tmp/workspace
      - run: cat  /tmp/workspace/echo-output


# Orchestrate jobs using workflows
# See: https://circleci.com/docs/configuration-reference/#workflows
workflows:
  default:
    jobs:
      - Build

      - Scan_Build:
          requires: [Build]

      - Deploy:
          requires: [Build]

      - Scan_Deploy:
          requires: [Deploy, Scan_Build]

Code explanation

  • #1 Define parameters

  • #2 Call these parameters in variables

  • #3 Call The Variables that have parameters values


Pipeline values

Pipeline values are built-in environment variables that are available in all pipelines.

And you can call them in the same way of calling variables as above.

Press on the image below to get the entire list:


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