-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins-job.xml
117 lines (111 loc) · 4.93 KB
/
jenkins-job.xml
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
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description>A freestyle build configuration</description>
<keepDependencies>false</keepDependencies>
<properties>
<com.coravy.hudson.plugins.github.GithubProjectProperty plugin="github@1.27.0">
<projectUrl>https://github.com/slartibaartfast/aws-docker-jenkins/</projectUrl>
<displayName></displayName>
</com.coravy.hudson.plugins.github.GithubProjectProperty>
</properties>
<scm class="hudson.plugins.git.GitSCM" plugin="git@3.4.1">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>https://github.com/slartibaartfast/aws-docker-jenkins.git</url>
<credentialsId>85dc44be-a330-4328-af56-7de451f2a786</credentialsId>
</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>
<com.cloudbees.jenkins.GitHubPushTrigger plugin="github@1.27.0">
<spec></spec>
</com.cloudbees.jenkins.GitHubPushTrigger>
</triggers>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>#!/bin/bash
DOCKER_LOGIN=`aws ecr get-login --region us-east-1`
${DOCKER_LOGIN}</command>
</hudson.tasks.Shell>
<com.cloudbees.dockerpublish.DockerBuilder plugin="docker-build-publish@1.3.2">
<server plugin="docker-commons@1.8"/>
<registry plugin="docker-commons@1.8">
<url>https://817710089510.dkr.ecr.us-east-1.amazonaws.com</url>
</registry>
<repoName>scripted-repo</repoName>
<noCache>false</noCache>
<forcePull>true</forcePull>
<skipBuild>false</skipBuild>
<skipDecorate>false</skipDecorate>
<repoTag>v_$BUILD_NUMBER</repoTag>
<skipPush>false</skipPush>
<createFingerprint>true</createFingerprint>
<skipTagLatest>false</skipTagLatest>
<buildAdditionalArgs></buildAdditionalArgs>
<forceTag>false</forceTag>
</com.cloudbees.dockerpublish.DockerBuilder>
<hudson.tasks.Shell>
<command>#!/bin/bash
#Constants
REGION=us-east-1
REPOSITORY_NAME=scripted-repo
CLUSTER=scripted-cluster
FAMILY=`sed -n 's/.*"family": "\(.*\)",/\1/p' taskdef.json`
NAME=`sed -n 's/.*"name": "\(.*\)",/\1/p' taskdef.json`
SERVICE_NAME=${NAME}-service
#Store the repositoryUri as a variable
REPOSITORY_URI=`aws ecr describe-repositories --repository-names ${REPOSITORY_NAME} --region ${REGION} | jq .repositories[].repositoryUri | tr -d '"'`
#Replace the build number and respository URI placeholders with the constants above
sed -e "s;%BUILD_NUMBER%;${BUILD_NUMBER};g" -e "s;%REPOSITORY_URI%;${REPOSITORY_URI};g" taskdef.json > ${NAME}-v_${BUILD_NUMBER}.json
#Register the task definition in the repository
aws ecs register-task-definition --family ${FAMILY} --cli-input-json file://${WORKSPACE}/${NAME}-v_${BUILD_NUMBER}.json --region ${REGION}
SERVICES=`aws ecs describe-services --services ${SERVICE_NAME} --cluster ${CLUSTER} --region ${REGION} | jq .failures[]`
#Get latest revision
REVISION=`aws ecs describe-task-definition --task-definition ${NAME} --region ${REGION} | jq .taskDefinition.revision`
#Create or update service
if [ "$SERVICES" == "" ]; then
echo "entered existing service"
DESIRED_COUNT=`aws ecs describe-services --services ${SERVICE_NAME} --cluster ${CLUSTER} --region ${REGION} | jq .services[].desiredCount`
if [ ${DESIRED_COUNT} != "1" ]; then
echo "reseting desired count to 1"
DESIRED_COUNT="1"
fi
aws ecs update-service --cluster ${CLUSTER} --region ${REGION} --service ${SERVICE_NAME} --task-definition ${FAMILY}:${REVISION} --desired-count ${DESIRED_COUNT}
else
echo "entered new service"
aws ecs create-service --service-name ${SERVICE_NAME} --desired-count 1 --task-definition ${FAMILY} --cluster ${CLUSTER} --region ${REGION}
fi</command>
</hudson.tasks.Shell>
<hudson.tasks.Shell>
<command>#!/bin/bash
# Check that status is 200 OK
# TODOthis doesn't give aws time to bring up the new site/page
# this points at the wrong place
status_code=$(curl -o /dev/null --silent --head --write-out '%{http_code}\n' http://stelligent.tomrota.com)
if [[ $status_code == "200" ]]
then
echo "Pass: status code is ${status_code}";
else
echo "Fail: status code is ${status_code}";
fi
</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>