-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure-pipelines.yml
103 lines (97 loc) · 3.69 KB
/
azure-pipelines.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Variabled defined that will be referenced in the jobs.
variables:
sonar_org: hazemabdelmagid
sonar_project_key: HazemAbdelmagid_DevOps-Tasks
registry_domain: devopsserver2021.jfrog.io
repo_name: docker-image-repo
image_name: app
image_tag: latest
aws_region: ca-central-1
# Agents image that will execute the jobs.
pool:
vmImage: ubuntu-latest
# Stages define in Azure Pipeline.
stages:
# Linting & Testing Stage to check the Flask Code.
- stage: 'Testing'
jobs:
- job: 'Testing_Job'
steps:
- script: |
python3 -m pip install --upgrade pip
pip3 install -r requirements.txt
wget -O ./hadolint https://github.com/hadolint/hadolint/releases/download/v1.16.3/hadolint-Linux-x86_64 && chmod +x ./hadolint
displayName: 'Install dependencies'
- script: |
./hadolint Dockerfile
pylint --disable=R,C,W1203 app.py
displayName: 'Linting'
- script: |
python3 -m pytest -vv tests/*.py
displayName: 'Testing'
# Perform Code Analysis using Sonar Cloud.
- stage: 'CodeAnalysis'
jobs:
- job: 'Code_Analysis'
steps:
# First task is used to prepare the analysis configuration for specific repo.
- task: SonarCloudPrepare@1
displayName: 'Preparing Anaylsis Configuration'
inputs:
SonarCloud: 'Sonar Cloud Connection'
organization: $(sonar_org)
scannerMode: 'CLI'
configMode: 'manual'
cliProjectKey: $(sonar_project_key)
cliSources: '.'
# Make the analysis and provide a link for the result.
- task: SonarCloudAnalyze@1
displayName: 'Running Code Analysis'
# Building Docker Image and Pushing it to JFrog Artifactory Repo.
- stage: 'Building'
jobs:
- job: 'Building_Docker_Image'
steps:
# Building the docker image with specific name and tag.
- task: Docker@2
displayName: 'Building Docker Image'
inputs:
command: build
arguments: --tag $(registry_domain)/$(repo_name)/$(image_name)
# Pushing the docker image into JFrog Artifactory Repo.
- task: ArtifactoryDocker@1
displayName: 'Pushing Docker Image to JFrog Artifactory'
inputs:
command: 'push'
artifactoryService: 'JFrog Connection'
targetRepo: '$(registry_domain)/$(repo_name)'
imageName: '$(registry_domain)/$(repo_name)/$(image_name):$(image_tag)'
# Deployment stage for Simple flask app.
- stage: 'Deploying'
jobs:
- job: 'Deployment_Job'
steps:
# Install some Deps [Kubectl & aws-iam-authenticator] to connect to AWS EKS cluster and deploy flask app.
- script: |
chmod +x script.sh
./script.sh
displayName: 'Install Dependencies'
# Make sure that the image exists on the repo and create config.json file to be used for authentication to Jfrog in next task.
- task: ArtifactoryDocker@1
inputs:
command: 'pull'
artifactoryService: 'JFrog Connection'
sourceRepo: '$(repo_name)'
imageName: '$(registry_domain)/$(repo_name)/$(image_name):$(image_tag)'
displayName: 'Pulling docker Image'
# Using AWS task to connect to AWS EKS cluster, creating a secret contains all info auth data to connect to JFrog Artifactory and pull the image to create the pods.
- task: AWSShellScript@1
inputs:
awsCredentials: 'AWS Credentials'
regionName: $(aws_region)
scriptType: 'inline'
inlineScript: |
aws eks update-kubeconfig --name app-eks-cluster
kubectl create secret generic my-key --from-file=.dockerconfigjson=/home/vsts/.docker/config.json --type=kubernetes.io/dockerconfigjson
kubectl apply -f app-config.yaml
displayName: 'EKS Cluster Creation and Connection and Deployment'