-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile-buildartifact
135 lines (104 loc) · 5.34 KB
/
Jenkinsfile-buildartifact
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
pipeline{
agent any
environment{
AWS_ACCESS_KEY_ID = credentials('aws-access-key-id')
AWS_SECRET_ACCESS_KEY = credentials('aws-secret-access-key')
ARTIFACT_REPO = "buildartifactrepo"
REGION = "us-east-1"
AWS_ACCOUNT_ID="211125415675"
IMAGE_REPO_NAME = "helloworldrepo"
AWS_REGION = 'us-east-1'
ECR_URL= "211125415675.dkr.ecr.${REGION}.amazonaws.com"
}
stages{
stage('Code Checkout'){
steps{
script{
git branch:'master',url:'https://github.com/skykesavanke/Artifact_Imagebuild.git'
}
}
}
stage ('Token Generation'){
steps{
script{
def tokenFile = 'codeartifact_token.txt'
bat """aws codeartifact get-authorization-token --domain domain-1 --domain-owner 211125415675 --region us-east-1 --query authorizationToken --output text > ${tokenFile}"""
}
}
}
stage('Building Artifact'){
steps{
script{
def mavenPath = 'C:\\Users\\kesavank\\Maven\\apache-maven-3.9.6\\bin\\mvn'
bat "${mavenPath} clean"
echo ".......Cleaning the previous build......"
bat "${mavenPath} validate"
bat "${mavenPath} compile"
echo ".......Compiling the source code......"
bat "${mavenPath} package"
echo ".......Creating Jar files........"
bat "${mavenPath} verify"
bat "${mavenPath} install"
}
}
}
stage('Push to Code Artifact'){
steps{
script{
def tokenFile = 'codeartifact_token.txt'
def token = readFile(tokenFile).trim()
withEnv(["CODEARTIFACT_AUTH_TOKEN=${token}"]) {
def artifactPath = 'C:\\Windows\\System32\\config\\systemprofile\\AppData\\Local\\Jenkins\\.jenkins\\workspace\\Image_Build_from_Artifact\\target\\helloworld-0.0.1-SNAPSHOT.jar'
def artifactRepoUrl = "https://domain-1-211125415675.d.codeartifact.us-east-1.amazonaws.com/maven/buildartifactrepo"
bat "curl --request PUT ${artifactRepoUrl}/com/example/HelloWorld/helloworld-0.0.1-SNAPSHOT.jar --user \"aws:${CODEARTIFACT_AUTH_TOKEN}\" --data-binary @${artifactPath}"
def pomPath = 'C:\\Windows\\System32\\config\\systemprofile\\AppData\\Local\\Jenkins\\.jenkins\\workspace\\Image_Build_from_Artifact\\pom.xml'
bat "curl --request PUT ${artifactRepoUrl}/com/example/HelloWorld/helloworld-0.0.1-SNAPSHOT.jar --user \"aws:${CODEARTIFACT_AUTH_TOKEN}\" --data-binary @${pomPath}"
}
}
}
}
stage('Update Package Version Status') {
steps {
script {
def tokenFile = 'codeartifact_token.txt'
def token = readFile(tokenFile).trim()
withEnv(["CODEARTIFACT_AUTH_TOKEN=${token}"]) {
bat """
aws codeartifact list-packages --domain domain-1 --domain-owner 211125415675 --repository buildartifactrepo --format maven --region us-east-1
"""
bat """
aws codeartifact update-package-versions-status \
--domain domain-1 \
--domain-owner 211125415675 \
--repository buildartifactrepo \
--format maven \
--namespace com \
--package example \
--versions HelloWorld \
--region us-east-1 \
--target-status Published
"""
}
}
}
}
stage('Push image to ECR'){
steps{
script{
def imageRepoName = "${env.IMAGE_REPO_NAME}"
def awsRegion = "${env.REGION}"
def ecrUrl = "211125415675.dkr.ecr.${awsRegion}.amazonaws.com"
bat """
set IMAGE_REPO_NAME=${imageRepoName}
set AWS_REGION=${awsRegion}
set ECR_URL=${ecrUrl}
aws ecr get-login-password --region %AWS_REGION% | docker login --username AWS --password-stdin %ECR_URL%
docker build -t %IMAGE_REPO_NAME% .
docker tag %IMAGE_REPO_NAME%:latest %ECR_URL%/%IMAGE_REPO_NAME%:latest
docker push %ECR_URL%/%IMAGE_REPO_NAME%:latest
"""
}
}
}
}
}