-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
106 lines (99 loc) · 4.04 KB
/
Jenkinsfile
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
104
105
106
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout scmGit(branches: [[name: '*/main']],
extensions: [],
userRemoteConfigs: [[credentialsId: 'github_auth_id', url: 'https://github.com/AtlasAleks22/weatherapp_project.git']])
}
}
stage('Update Token in environments.ts') {
steps {
script {
withCredentials([string(credentialsId: 'jenkins_xrapidApi', variable: 'API_KEY')]) {
sh "sed -i \"s/api_key_placeholder/$API_KEY/g\" weatherapp_proj/src/app/environments/environment.ts"
}
}
}
}
stage('Image build') {
steps {
script {
sh 'docker --version'
dir('weatherapp_proj') {
sh 'docker build -t weather_app .'
}
}
}
}
stage('MegaLinter') {
options {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE')
}
agent {
docker {
image 'oxsecurity/megalinter:v7'
args "-u root -e VALIDATE_ALL_CODEBASE=true -v ${WORKSPACE}:/tmp/lint -v ${WORKSPACE}/weatherapp_proj:/tmp/lint/weatherapp_proj --entrypoint=''"
reuseNode true
customWorkspace '/tmp/lint'
}
}
steps {
sh '/entrypoint.sh'
}
post {
always {
archiveArtifacts allowEmptyArchive: true, artifacts: 'mega-linter.log', defaultExcludes: false, followSymlinks: false
archiveArtifacts allowEmptyArchive: true, artifacts: 'megalinter-reports/**/*.html', defaultExcludes: false, followSymlinks: false
}
}
}
stage('Run Docker Container') {
steps {
sh 'docker run -d -p 4200:4200 weather_app'
}
}
stage('Run Smoke Test') {
steps {
script {
withCredentials([string(credentialsId: 'jenkins_xrapidApi', variable: 'API_KEY')]) {
def weatherApiBaseUrl = 'https://openweather43.p.rapidapi.com/weather'
def XRapidAPIHostHeaderValue = 'openweather43.p.rapidapi.com'
sh "curl -s -H 'X-RapidAPI-Host: ${XRapidAPIHostHeaderValue}' -H 'X-RapidAPI-Key: ${API_KEY}' '${weatherApiBaseUrl}?q=Bucharest'"
sh "curl -s -H 'X-RapidAPI-Host: ${XRapidAPIHostHeaderValue}' -H 'X-RapidAPI-Key: ${API_KEY}' '${weatherApiBaseUrl}?q=Timisoara'"
}
}
}
}
stage('Login and Push Image') {
steps {
script {
withCredentials([
usernamePassword(credentialsId: 'docker_auth',
usernameVariable: 'DOCKER_USERNAME',
passwordVariable: 'DOCKER_PASSWORD')
])
{
sh "docker login -u ${DOCKER_USERNAME} -p \$DOCKER_PASSWORD"
sh "docker tag weather_app ${DOCKER_USERNAME}/weather_app:v1.0"
sh "docker push ${DOCKER_USERNAME}/weather_app:v1.0"
}
}
}
}
stage('Deploy with Docker Compose') {
steps {
script {
withCredentials([string(credentialsId: 'jenkins_xrapidApi', variable: 'API_KEY')]) {
withEnv(['XRapidAPIKeyHeaderValue=$API_KEY']) {
sh 'docker-compose -f ./weatherapp_proj/docker-compose.yml up -d'
}
}
echo 'Application deployed successfully!'
echo 'Access the application at: http://localhost:4200'
}
}
}
}
}