Devops Shack Azure DevOps Pipeline
Devops Shack Azure DevOps Pipeline
Real-Time Corporate
. Azure DevOps CICD Pipeline .
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: Setup
jobs:
- job: InstallRequiredTools
displayName: 'Install Required Tools'
steps:
- script: |
sudo apt-get update
sudo apt-get install -y default-jdk nodejs npm maven
displayName: 'Install JDK, Node.js, and Maven'
Explanation:
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: Setup_and_Dependencies
jobs:
- job: InstallRequiredTools
displayName: 'Install Required Tools'
steps:
- script: |
sudo apt-get update
sudo apt-get install -y default-jdk nodejs npm maven
displayName: 'Install JDK, Node.js, and Maven'
- job: InstallProjectDependencies
displayName: 'Install Project Dependencies'
steps:
- script: |
npm install # For Node.js projects
mvn clean install # For Maven projects
displayName: 'Install npm and Maven Dependencies'
Explanation:
• This stage includes two jobs: one for installing required tools and the other for
installing project dependencies.
• Each job consists of steps to execute the necessary commands for installing tools and
dependencies.
• npm install and mvn clean install commands are used to install project-specific
dependencies based on the project type.
• This stage includes a single job named ExecuteTestCases to run automated test
cases.
• Depending on the project type, either npm test or mvn test command will be
executed to run the tests.
• The displayName attribute provides a human-readable name for the job, making it
easier to understand the purpose of the job in the pipeline.
- job: ScanDockerImage
displayName: 'Scan Docker Image for Vulnerabilities'
steps:
- script: |
trivy your_image_name:latest
displayName: 'Scan Docker Image'
Explanation:
• This stage includes two jobs: one for performing a file system scan and the other for
scanning a Docker image for vulnerabilities.
• Each job executes the appropriate security scanning tool (trivy) with the necessary
parameters.
• The displayName attribute provides a descriptive name for each job in the pipeline.
• This stage contains a single job named CompileAndPackage responsible for building
the application artifact.
• The mvn clean package command is used to compile and package the application
using Maven.
• The artifacts section specifies the artifact paths to be published by the job for use
in subsequent stages.
• This stage contains a single job named UploadArtifact responsible for publishing
the artifact to Nexus.
• The mvn deploy command is used to deploy the artifact to Nexus using Maven.
• This stage assumes that the Maven settings.xml file is properly configured with Nexus
credentials for deployment.
• This stage includes a single job named BuildImage responsible for building the
Docker image.
• The docker build command is used to build the Docker image using the Dockerfile
in the project directory.
• your_image_name:latest should be replaced with the appropriate image name and
tag.
• This stage includes a single job named SecurityScan responsible for scanning the
Docker image for vulnerabilities.
• The trivy command is used to run the security scan on the Docker image.
• your_image_name:latest should be replaced with the name and tag of your Docker
image.
• This stage includes a single job named PushImage responsible for pushing the
Docker image to Docker Hub.
• The docker login command is used to authenticate with Docker Hub using the
provided credentials.
• The docker push command is used to push the Docker image to Docker Hub.
• your_image_name:latest should be replaced with the name and tag of your Docker
image.
• This stage includes a single job named UpdateManifests responsible for updating
Kubernetes manifest files.
• The sed command is used to replace occurrences of the old image name with the
new image name in the Kubernetes manifest files.
• old_image_name should be replaced with the previous image name,
and your_image_name should be replaced with the new image name.
• This stage includes a single job named DeployApp responsible for deploying the
application to a Kubernetes cluster.
• The kubectl apply command is used to apply the Kubernetes manifest files to the
cluster.
• path/to/kubernetes/*.yaml should be replaced with the path to your Kubernetes
manifest files.
• This stage includes a single job named VerifyApp responsible for verifying the
deployment of the application.
• The kubectl get pods command is used to retrieve the status of pods in the
specified namespace.
• Additional verification steps can be added as needed to ensure the application is
running correctly.
• This stage includes a single job named SecurityTest responsible for conducting
security testing.
• The zap-cli command is used to run OWASP ZAP security testing on the provided
application URL.
• The --spider, --ajax, and --quick-scan options are used to configure the security
scan.
• The results of the security test are output to a specified file.
• This stage includes a single job named SendEmail responsible for sending email
notifications.
• The echo command is used to indicate that email notifications are being sent.
• You can replace the comment with the actual command to send email notifications
using your email service provider.
This completes the Azure DevOps CI/CD YAML pipeline, covering all the stages from
setup to email notifications. Each stage performs specific tasks essential for the
continuous integration and deployment process.
Here's the combined Azure DevOps CI/CD YAML pipeline with
all the stages:
trigger:
branches:
include:
- main # Replace with your branch name
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: Setup_and_Dependencies
jobs:
- job: InstallRequiredTools
displayName: 'Install Required Tools'
steps:
- script: |
sudo apt-get update
sudo apt-get install -y default-jdk nodejs npm maven
displayName: 'Install JDK, Node.js, and Maven'
- job: InstallProjectDependencies
displayName: 'Install Project Dependencies'
steps:
- script: |
npm install # For Node.js projects
mvn clean install # For Maven projects
displayName: 'Install npm and Maven Dependencies'
- stage: Test
jobs:
- job: ExecuteTestCases
displayName: 'Execute Test Cases'
steps:
- script: |
npm test # For Node.js projects
mvn test # For Maven projects
displayName: 'Run Automated Tests'
- job: PerformFileSystemScan
displayName: 'Perform File System Scan'
steps:
- script: |
trivy <path_to_project_directory>
displayName: 'Scan Project Files for Vulnerabilities'
- stage: CodeQualityAnalysis
jobs:
- job: EvaluateCodeQuality
displayName: 'Evaluate Code Quality'
steps:
- script: |
sonar-scanner
displayName: 'Run SonarQube Scanner'
- stage: BuildArtifact
jobs:
- job: CompileAndPackage
displayName: 'Compile and Package Application'
steps:
- script: |
mvn clean package
displayName: 'Build Artifact'
artifacts:
paths:
- target/*.jar # Adjust based on artifact type (e.g., WAR, JAR)
- stage: PublishToNexus
jobs:
- job: UploadArtifact
displayName: 'Upload Artifact to Nexus'
steps:
- script: |
mvn deploy
displayName: 'Publish Artifact'
- stage: BuildDockerImage
jobs:
- job: BuildImage
displayName: 'Build Docker Image'
steps:
- script: |
docker build -t your_image_name:latest .
displayName: 'Building Docker Image'
- stage: ScanDockerImage
jobs:
- job: SecurityScan
displayName: 'Scan Docker Image for Vulnerabilities'
steps:
- script: |
trivy your_image_name:latest
displayName: 'Running Trivy Security Scan'
- stage: PushToDockerHub
jobs:
- job: PushImage
displayName: 'Push Docker Image to Docker Hub'
steps:
- script: |
docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
docker push your_image_name:latest
displayName: 'Pushing Docker Image'
- stage: UpdateKubernetesManifests
jobs:
- job: UpdateManifests
displayName: 'Update Kubernetes Manifests'
steps:
- script: |
sed -i 's|old_image_name|your_image_name|g'
path/to/kubernetes/*.yaml
displayName: 'Updating Manifests'
- stage: DeployToKubernetes
jobs:
- job: DeployApp
displayName: 'Deploy Application to Kubernetes'
steps:
- script: |
kubectl apply -f path/to/kubernetes/*.yaml
displayName: 'Applying Kubernetes Manifests'
- stage: VerifyDeployment
jobs:
- job: VerifyApp
displayName: 'Verify Deployment'
steps:
- script: |
kubectl get pods --namespace your_namespace
# Additional verification steps can be added here
displayName: 'Check Pod Status'
- stage: SecurityTesting
jobs:
- job: SecurityTest
displayName: 'Security Testing with OWASP ZAP'
steps:
- script: |
zap-cli --url <your_application_url> --spider --ajax --quick-scan -
-output <output_file>
displayName: 'Running OWASP ZAP Security Test'
- stage: SendEmailNotifications
jobs:
- job: SendEmail
displayName: 'Send Email Notifications'
steps:
- script: |
echo "Sending email notifications..."
# Command to send email notifications using your email service
provider
displayName: 'Send Email'
Explanation:
• The pipeline consists of multiple stages, each containing one or more jobs.
• Jobs within each stage are executed sequentially.
• Each stage represents a specific phase of the CI/CD process, such as setup, testing,
building, deployment, security scanning, and notification.
• You'll need to replace placeholders
like your_image_name, path/to/project_directory, old_image_name, your_name
space, and <your_application_url> with your actual values.
• Ensure you have appropriate permissions and configurations for Docker Hub, Nexus,
Kubernetes, and any other services used in the pipeline.