-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake.fsx
151 lines (115 loc) · 5.59 KB
/
fake.fsx
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
#r "paket:
nuget Fake.IO.FileSystem
nuget Fake.IO.Zip
nuget Fake.DotNet.Cli
nuget Fake.Core.Process
nuget Fake.Core.Target //"
#load "./.fake/fake.fsx/intellisense.fsx"
open System
open System.IO
open Fake.Core
open Fake.IO
open Fake.DotNet
open Fake.IO.Globbing.Operators
let date = DateTime.Now.ToString "yyyy-MM-ddTHH:mm:ssZ"
// Properties
let projectName = "aws-service-test"
let vcsRef = Environment.environVarOrDefault "VCSREF" ""
let vcsBranch = Environment.environVarOrDefault "VCSBRANCH" ""
let buildDate = Environment.environVarOrDefault "BUILDDATE" date
let configuration = Environment.environVarOrDefault "CONFIGURATION" ""
let tag = vcsRef + "-" + vcsBranch |> fun s -> s.Replace('/', '-')
let baseDir = __SOURCE_DIRECTORY__
let sourceDir = Path.combine baseDir "src"
let serviceDir = Path.combine sourceDir "aws-service-test"
let serviceBuildDir = Path.combine serviceDir (Path.combine "bin" configuration) |> Path.GetFullPath
let serviceDeployDir = Path.combine serviceDir (Path.combine "deploy" configuration) |> Path.GetFullPath
let lambdaDir = Path.combine sourceDir "aws-lambda-test"
let lambdaBuildDir = Path.combine lambdaDir (Path.combine "bin" configuration) |> Path.GetFullPath
let lambdaDeployDir = Path.combine lambdaDir (Path.combine "deploy" configuration) |> Path.GetFullPath
let lambdaPackageDir = Path.combine lambdaDir "deploy" |> Path.GetFullPath
let lambdaPackageFilename = "aws-lambda-test-" + tag + ".zip"
let dockerUser = Environment.environVarOrDefault "DOCKERUSER" ""
let dockerImageBranch = "-t " + dockerUser + "/" + projectName + ":" + tag
let dockerImageLatest = "-t " + dockerUser + "/" + projectName + ":latest"
let dockerImage =
match vcsBranch with
| "master" -> dockerImageBranch + " " + dockerImageLatest
| _ -> dockerImageBranch
let bucketName = Environment.environVarOrDefault "S3BUCKET" ""
let stackName = "aws-service-test"
let awsDir = Path.combine baseDir "aws"
let sshKey = Environment.environVarOrDefault "AWSKEYNAME" ""
let vpcId = Environment.environVarOrDefault "AWSVPCID" ""
let subnets = Environment.environVarOrDefault "AWSSUBNETS" ""
// Targets
Target.create "Clean" (fun _ ->
Shell.cleanDirs
[ serviceBuildDir
serviceDeployDir
lambdaBuildDir
lambdaDeployDir ]
)
Target.create "Build" (fun _ ->
DotNet.build (fun c ->
{ c with
Configuration = DotNet.BuildConfiguration.Custom(configuration)
OutputPath = Some(serviceBuildDir)
}) serviceDir
DotNet.build (fun c ->
{ c with
Configuration = DotNet.BuildConfiguration.Custom(configuration)
OutputPath = Some(lambdaBuildDir)
}) lambdaDir
)
Target.create "Publish" (fun _ ->
DotNet.publish (fun c ->
{ c with
Common = DotNet.Options.Create() |> (fun c ->
{ c with
CustomParams = Some("/property:PublishWithAspNetCoreTargetManifest=false")
})
Configuration = DotNet.BuildConfiguration.Release;
OutputPath = Some(serviceDeployDir)
}) serviceDir
DotNet.publish (fun c ->
{ c with
Common = DotNet.Options.Create() |> (fun c ->
{ c with
CustomParams = Some("/property:PublishWithAspNetCoreTargetManifest=false")
})
Configuration = DotNet.BuildConfiguration.Release;
OutputPath = Some(lambdaDeployDir)
}) lambdaDir
ignore(Shell.Exec("zip", "-r " + (lambdaPackageDir + "/" + lambdaPackageFilename) + " . -i *", lambdaDeployDir))
ignore(Shell.Exec("docker", "build -f Dockerfile " + dockerImage + " --build-arg VCSREF=" + vcsRef + " --build-arg VERSION=" + tag + " --build-arg BUILDDATE=" + buildDate + " .", sourceDir))
)
Target.create "PushLambda" (fun _ ->
ignore(Shell.Exec("aws", "s3 cp " + lambdaPackageFilename + " s3://" + bucketName, lambdaPackageDir))
)
Target.create "PushDocker" (fun _ ->
ignore(Shell.Exec("docker", "push " + dockerUser + "/" + projectName, sourceDir))
)
Target.create "StartStacks" (fun _ ->
ignore(Shell.Exec("aws", "cloudformation deploy --stack-name " + stackName + "-queue --template-file sqs.yaml", awsDir))
ignore(Shell.Exec("aws", "cloudformation deploy --stack-name " + stackName + "-lambda --template-file lambda.yaml --capabilities CAPABILITY_IAM --parameter-overrides BucketName=" + bucketName + " LambdaPackage=" + lambdaPackageFilename, awsDir))
ignore(Shell.Exec("aws", "cloudformation deploy --stack-name " + stackName + "-ecs --template-file ecs.yaml --capabilities CAPABILITY_IAM --parameter-overrides KeyName=" + sshKey + " VpcId=" + vpcId + " SubnetIds=" + subnets + " DockerImage=" + dockerImage + " DockerTag=" + tag, awsDir))
)
Target.create "StopStacks" (fun _ ->
ignore(Shell.Exec("aws", "cloudformation delete-stack --stack-name " + stackName + "-ecs", awsDir))
ignore(Shell.Exec("aws", "cloudformation wait stack-delete-complete --stack-name "+ stackName + "-ecs", awsDir))
ignore(Shell.Exec("aws", "cloudformation delete-stack --stack-name " + stackName + "-lambda", awsDir))
ignore(Shell.Exec("aws", "cloudformation wait stack-delete-complete --stack-name "+ stackName + "-lambda", awsDir))
ignore(Shell.Exec("aws", "cloudformation delete-stack --stack-name " + stackName + "-queue", awsDir))
ignore(Shell.Exec("aws", "cloudformation wait stack-delete-complete --stack-name "+ stackName + "-queue", awsDir))
)
open Fake.Core.TargetOperators
// Dependencies
"Clean"
==> "Build"
==> "Publish"
"Publish"
==> "PushLambda"
==> "PushDocker"
// start build
Target.runOrDefaultWithArguments "PushDocker"