-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkins-cfn.yaml
346 lines (345 loc) · 11.8 KB
/
Jenkins-cfn.yaml
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
AWSTemplateFormatVersion: 2010-09-09
Description: JenkinsStack
Mappings:
RegionMap:
us-east-1:
"AMI": "ami-97785bed"
us-east-2:
"AMI": "ami-f63b1193"
us-west-1:
"AMI": "ami-c9c7978c"
eu-west-1:
"AMI": "ami-37c2f643"
ap-southeast-1:
"AMI": "ami-66f28c34"
ap-northeast-1:
"AMI": "ami-9c03a89d"
Parameters:
MaxSize:
Type: Number
Default: '1'
Description: The maximum size of the Auto Scaling group
StartSize:
Type: Number
Default: '1'
Description: Number of instances to launch in your Monitoring cluster
InstanceType:
Description: The EC2 instance type
Type: String
Default: t2.medium
AllowedValues:
- t2.micro
- t2.small
- t2.medium
- t2.large
- t2.xlarge
- t2.2xlarge
- m4.large
- m4.xlarge
- m4.2xlarge
ConstraintDescription: Must be a valid EC2 instance type.
KeyName:
Type: 'AWS::EC2::KeyPair::KeyName'
Description: >-
Name of an existing EC2 KeyPair to enable SSH access to the Monitoring
instances
MinLength: 1
JenkinsAdmin:
Type: String
Description: >-
Jenkins master User name
MinLength: 1
JenkinsPassword:
Type: String
Description: >-
Jenkins master User password
MinLength: 1
Subnets:
Description: List of VPC subnet IDs for the cluster
Type: 'List<AWS::EC2::Subnet::Id>'
SubnetAZ:
Description: Availability Zone of the Subnet
Type: 'List<AWS::EC2::AvailabilityZone::Name>'
VpcId:
Description: VPC associated with the provided subnets
Type: 'AWS::EC2::VPC::Id'
AdminSecurityGroup:
Description: >-
Existing security group that should be granted administrative access to
server (e.g., 'sg-123456')
Default: Primary
Type: 'AWS::EC2::SecurityGroup::Id'
GitUrl:
Type: String
Description: >-
Existing Project with cloudformation for testing.
MinLength: 1
CloudFormationFile:
Type: String
Description: >-
Existing Project with cloudformation for testing.
MinLength: 1
CloudFormationParams:
Type: String
Description: >-
Existing Project with cloudformation for testing.
MinLength: 1
Resources:
AutoclusterRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
AutoclusterPolicy:
Type: 'AWS::IAM::Policy'
Properties:
PolicyName: AutoclusterPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'ecr:GetAuthorizationToken'
- 'ecr:BatchCheckLayerAvailability'
- 'ecr:GetDownloadUrlForLayer'
- 'ecr:GetRepositoryPolicy'
- 'ecr:DescribeRepositories'
- 'ecr:ListImages'
- 'ecr:BatchGetImage'
Resource:
- '*'
- Effect: Allow
Action:
- 'autoscaling:DescribeAutoScalingInstances'
- 'ec2:DescribeInstances'
Resource:
- '*'
Roles:
- !Ref AutoclusterRole
JenkinsUser:
Type: AWS::IAM::User
Properties:
Path: "/"
Policies:
- PolicyName: cloudformation
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'cloudformation:*'
Resource:
- '*'
- PolicyName: ec2
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'ec2:*'
Resource:
- '*'
- PolicyName: iam
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'iam:*'
Resource:
- '*'
- PolicyName: elb
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'elasticloadbalancing:*'
Resource:
- '*'
- PolicyName: autoscaling
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'autoscaling:*'
Resource:
- '*'
AccessKey:
Type: AWS::IAM::AccessKey
Properties:
UserName:
!Ref JenkinsUser
AutoclusterInstanceProfile:
Type: 'AWS::IAM::InstanceProfile'
Properties:
Path: /
Roles:
- !Ref AutoclusterRole
AutoclusterSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: Open ports to the Jenkins
VpcId: !Ref VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '8080'
ToPort: '8080'
CidrIp: 0.0.0.0/0
LoadBalancerSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: Open ports to the Jenkins
VpcId: !Ref VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
AutoclusterELB:
Type: 'AWS::ElasticLoadBalancing::LoadBalancer'
Properties:
SecurityGroups:
- !Ref AdminSecurityGroup
- !Ref LoadBalancerSecurityGroup
Subnets: !Ref Subnets
Scheme: internet-facing
Listeners:
- LoadBalancerPort: '80'
InstancePort: '8080'
Protocol: TCP
HealthCheck:
Target: 'HTTP:8080/'
HealthyThreshold: '2'
UnhealthyThreshold: '10'
Interval: '30'
Timeout: '5'
Tags:
- Key: Name
Value: !Ref 'AWS::StackName'
AutoclusterLaunchConfig:
Type: 'AWS::AutoScaling::LaunchConfiguration'
Properties:
ImageId: !FindInMap [ RegionMap, !Ref "AWS::Region", "AMI" ]
InstanceMonitoring: true
InstanceType: !Ref InstanceType
IamInstanceProfile: !Ref AutoclusterInstanceProfile
KeyName: !Ref KeyName
SecurityGroups:
- !Ref AutoclusterSecurityGroup
- !Ref AdminSecurityGroup
UserData:
'Fn::Base64':
!Sub |
#cloud-config
repo_update: true
repo_upgrade: all
write_files:
- content: |
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description></description>
<keepDependencies>false</keepDependencies>
<scm class="hudson.plugins.git.GitSCM" plugin="[email protected]">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>${GitUrl}</url>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>*/master</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<submoduleCfg class="list"/>
<extensions/>
</scm>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<hudson.triggers.SCMTrigger>
<spec>*/5 * * * *</spec>
<ignorePostCommitHooks>false</ignorePostCommitHooks>
</hudson.triggers.SCMTrigger>
<concurrentBuild>false</concurrentBuild>
<builders/>
<publishers>
<org.jenkinsci.plugins.github.status.GitHubCommitStatusSetter plugin="[email protected]">
<commitShaSource class="org.jenkinsci.plugins.github.status.sources.BuildDataRevisionShaSource"/>
<reposSource class="org.jenkinsci.plugins.github.status.sources.AnyDefinedRepositorySource"/>
<contextSource class="org.jenkinsci.plugins.github.status.sources.DefaultCommitContextSource"/>
<statusResultSource class="org.jenkinsci.plugins.github.status.sources.DefaultStatusResultSource"/>
<statusBackrefSource class="org.jenkinsci.plugins.github.status.sources.BuildRefBackrefSource"/>
<errorHandlers/>
</org.jenkinsci.plugins.github.status.GitHubCommitStatusSetter>
</publishers>
<buildWrappers>
<com.syncapse.jenkinsci.plugins.awscloudformationwrapper.CloudFormationBuildWrapper plugin="[email protected]">
<stacks>
<com.syncapse.jenkinsci.plugins.awscloudformationwrapper.StackBean>
<stackName>JenkinsStackTest</stackName>
<description>JenkinsStackTest</description>
<cloudFormationRecipe>${CloudFormationFile}</cloudFormationRecipe>
<parameters>${CloudFormationParams}</parameters>
<timeout>1800</timeout>
<awsAccessKey>${AccessKey}</awsAccessKey>
<awsSecretKey>${AccessKey.SecretAccessKey}</awsSecretKey>
<autoDeleteStack>true</autoDeleteStack>
<awsRegion>US_East_Northern_Virginia</awsRegion>
</com.syncapse.jenkinsci.plugins.awscloudformationwrapper.StackBean>
</stacks>
</com.syncapse.jenkinsci.plugins.awscloudformationwrapper.CloudFormationBuildWrapper>
</buildWrappers>
</project>
path: /opt/cloudformationBuild.xml
runcmd:
- [ sh, -c, "sudo yum install -y docker git"]
- [ sh, -c, "sudo service docker start" ]
- [ sh, -c, "git clone https://github.com/metebilgin/HomeCookJenkins /opt/HomeCookJenkins"]
- [ sh, -c, "cd /opt/HomeCookJenkins && docker build . -t jenkins_home_cook"]
- [ sh, -c, "docker run -d -p 8080:8080 -e JENKINS_USERNAME=${JenkinsAdmin} -e JENKINS_PASSWORD=${JenkinsPassword} jenkins_home_cook"]
- [ sh, -c, "sleep 60"]
- [ sh, -c, "curl -X POST 'http:///${JenkinsAdmin}:${JenkinsPassword}@localhost:8080/createItem?name=CloudFormationCheck' --header \"Content-Type: application/xml\" -d @/opt/cloudformationBuild.xml"]
AutoclusterAutoScalingGroup:
Type: 'AWS::AutoScaling::AutoScalingGroup'
Properties:
VPCZoneIdentifier: !Ref Subnets
AvailabilityZones: !Ref SubnetAZ
DesiredCapacity: !Ref StartSize
MinSize: 0
MaxSize: !Ref MaxSize
HealthCheckGracePeriod: 300
HealthCheckType: ELB
LaunchConfigurationName: !Ref AutoclusterLaunchConfig
LoadBalancerNames:
- !Ref AutoclusterELB
Tags:
- Key: Name
Value: !Ref 'AWS::StackName'
PropagateAtLaunch: 'true'
- Key: Reference
Value: Created from Cloudformation Stack
PropagateAtLaunch: true
Outputs:
JenkinUrl:
Value: !GetAtt AutoclusterELB.DNSName
Export:
Name: !Sub '${AWS::StackName}-URL'