Explore useful Automation Techniques with Powershell on Windows
Introduction We will list few interesting automation techniques with powershell…
October 07, 2022
I had to write a CICD system for one of our project. I had to deploy artifacts on different environments. We did not want it to be fully automated. This deployment job should be manually run, when we are fully sure.
The job should provide user the option of:
Here is the complete Jenkinsfile.
pipeline {
agent {
node {
label "MyNode"
}
}
stages {
stage('Take User Input') {
steps {
script {
properties([
parameters([
choice(
choices: ['Dev', 'Stage', 'Prod'],
name: 'DeployEnv',
description: "Select the environment"
),
choice(
choices: ['ws-1', 'ws-1', 'worker-1'],
name: 'ArtifactId',
description: "Which artifact",
),
string(
defaultValue: 'version of build',
name: 'Version',
description: "Write the version of build",
trim: true
)
])
])
}
}
}
stage('Run deployer') {
steps {
echo 'Deploying Artifacts to Kubernetes Env...'
withCredentials([
[$class: 'UsernamePasswordMultiBinding',
credentialsId:"MY_CRED_ID",
usernameVariable: 'AZURE_SERVICE_USERNAME',
passwordVariable: 'AZURE_SERVICE_SECRET'],
[$class: 'UsernamePasswordMultiBinding', credentialsId:"MY_API_KEY",
usernameVariable: 'ARTIFACTORY_USERNAME',
passwordVariable: 'ARTIFACTORY_PASSWORD']
]) {
sh """
# installing kubectl
sudo curl -LO "https://dl.k8s.io/release/v1.25.2/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client
echo "deploy to env=${params.DeployEnv}, artifact=${params.ArtifactId}, version=${params.Version}"
echo "Preparing runtime env..."
python3.8 -m pip install virtualenv
python3.8 -m venv python_env
source python_env/bin/activate
python3.8 -m pip install --upgrade pip
python3.8 -m pip install -r requirements.txt -i https://${ARTIFACTORY_USERNAME}:${ARTIFACTORY_PASSWORD}@artifactory.myorg.com/artifactory/api/pypi/pypi-myorg-release/simple
echo "Ready to run deployment..."
python3.8 deploy.py "${params.DeployEnv}" "${params.ArtifactId}" "${params.Version}"
deactivate
"""
}
}
}
}
}
Lets break down the file in following parts:
Jenkins provides option to render few UI widgets and taking their values. In my case, the values are static. But, you can take dynamic values too.
For example, a drop down is rendered by:
choice(
choices: ['Dev', 'Stage', 'Prod'],
name: 'DeployEnv',
description: "Select the environment"
)
Whatever user selects, its value will be stored in variable name mentioned in name
attribute.
To access above mentioned value, I will use ${params.DeployEnv}
in shell script step.
Example, this is how I’m using it:
echo "deploy to env=${params.DeployEnv}, artifact=${params.ArtifactId}, version=${params.Version}"
You can store as many credentials in Jenkins, and I’ve fetched two credentials at the same time by:
withCredentials([
[$class: 'UsernamePasswordMultiBinding',
credentialsId:"MY_CRED_ID",
usernameVariable: 'AZURE_SERVICE_USERNAME',
passwordVariable: 'AZURE_SERVICE_SECRET'],
[$class: 'UsernamePasswordMultiBinding', credentialsId:"MY_API_KEY",
usernameVariable: 'ARTIFACTORY_USERNAME',
passwordVariable: 'ARTIFACTORY_PASSWORD']
]) {
...
}
For example, MY_CRED_ID
is the key stored in Jenkins, and I’m exposing its username and password in environment variables: AZURE_SERVICE_USERNAME, AZURE_SERVICE_SECRET
And finally, I’m running a python code, which has the automation to deploy files on kubernetes.
And again, if you do not want to run this job on every code commit, i.e. you want this job to run manually only. See How not to run Jenkin job on Code-commit
Introduction We will list few interesting automation techniques with powershell…
Introduction When I migrated all of my drupal-7 website to drupal-8, I wrote…
MongoDB CRUD Operations Mongoose provides a simple schema based solution to…
I was using On page optimization of the article pages, and found that meta…
Introduction to Problem I have some data related to users. For example: which…
Introduction This post is about syncing your mongodo database data to…
Introduction In this post we will see following: How to schedule a job on cron…
Introduction There are some cases, where I need another git repository while…
Introduction In this post, we will see how to fetch multiple credentials and…
Introduction I have an automation script, that I want to run on different…
Introduction Java log4j has many ways to initialize and append the desired…
Introduction You have a running kubernetes setup, and have a webservice (exposed…