forked from akannan1087/myPythonDockerRepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
73 lines (71 loc) · 2.46 KB
/
Jenkinsfile
File metadata and controls
73 lines (71 loc) · 2.46 KB
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
pipeline {
agent any
// environment {
// registry = "068643504245.dkr.ecr.us-east-1.amazonaws.com/api-repo"
// }
stages {
//Loading variables
stage('Load Variables') {
steps {
script {
//make sure that file exists on this node
def constants = load 'Variables'
registry = constants.registryECR
branch = constants.branchName
gitURL = constants.gitURL
echo branch
echo registry
echo gitURL
}
}
}
//wanted to run these 2 below commands in shell - having permission issues
//usermod -a -G jenkins jenkins
//chmod 777 /var/run/docker.sock
stage ('Print Variables') {
steps {
script {
echo branch
echo registry
echo gitURL
}
}
}
stage ('Checkout') {
steps {
checkout scmGit(branches: [[name: branch]], extensions: [], userRemoteConfigs: [[url: gitURL]])
}
}
stage ('Docker Image Build') {
steps {
script {
dockerImage = docker.build registry
}
}
}
//This code pushes the image to AWS ECR
stage ('Docker Push') {
steps {
script {
sh 'aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 068643504245.dkr.ecr.us-east-1.amazonaws.com'
sh 'docker push 068643504245.dkr.ecr.us-east-1.amazonaws.com/api-repo:latest'
}
}
}
//Stopping Docker containers for cleaner Docker run
stage ('stop previous containers') {
steps {
sh 'docker ps -f name=mypythonContainer -q | xargs --no-run-if-empty docker container stop'
sh 'docker container ls -a -fname=mypythonContainer -q | xargs -r docker container rm'
}
}
//This will run the Docker container
stage ('Docker Run') {
steps {
script {
sh 'docker run -d -p 8096:5000 --rm --name mypythonContainer 068643504245.dkr.ecr.us-east-1.amazonaws.com/api-repo:latest'
}
}
}
}
}