-
Notifications
You must be signed in to change notification settings - Fork 13
/
Makefile
118 lines (93 loc) · 4.98 KB
/
Makefile
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
.PHONY: build run dev test push scan-image rmi deploy-ecs airbrake
##
# Makefile used to build, test and (locally) run the parliament.uk-prototype project.
##
##
# ENVRONMENT VARIABLES
# We use a number of environment variables to customer the Docker image createad at build time. These are set and
# detailed below.
##
# App name used to created our Docker image. This name is important in the context of the AWS docker repository.
APP_NAME = parliamentukprototype
# AWS account ID used to create our Docker image. This value is important in the context of the AWS docker repository.
# When executed in GoCD, AWS_ACCOUNT_ID may be set by an environment variable
AWS_ACCOUNT_ID ?= $(or $(shell aws sts get-caller-identity --output text --query "Account" 2 > /dev/null), unknown)
# A counter that represents the build number within GoCD. Used to tag and version our images.
GO_PIPELINE_COUNTER ?= unknown
# Which Rack environment will our docker image be configured to run in?
RACK_ENV ?= development
# VERSION is used to tag the Docker images
VERSION = 0.2.$(GO_PIPELINE_COUNTER)
# ECS related variables used to build our image name
ECS_CLUSTER = ecs
AWS_REGION = eu-west-1
# Tenable.io
TENABLEIO_USER ?= user # passed from an envvar in the CDP
TENABLEIO_PASSWORD ?= password # passed from an envvar in the CDP
TENABLEIO_REGISTRY = cloud.flawcheck.com
TENABLEIO_REPO = web1live
# Airbrake.io
AIRBRAKE_PROJECT_ID ?=
AIRBRAKE_PROJECT_KEY ?=
AIRBRAKE_ENVIRONMENT = $(RACK_ENV)
AIRBRAKE_REPOSITORY = https://github.com/ukparliament/parliament.uk-prototype
GIT_SHA = $(or $(GO_REVISION), unknown)
GIT_TAG = $(or $(shell git describe --tags --exact-match 2> /dev/null), unknown)
AWS_ACCOUNT ?= unknown
# The name of our Docker image
IMAGE = $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com/$(APP_NAME)
# Container port used for mapping when running our Docker image.
CONTAINER_PORT = 3000
# Host port used for mapping when running our Docker image.
HOST_PORT = 80
# Github variables
GITHUB_API=https://api.github.com
ORG=ukparliament
REPO=parliament.uk-prototype
LATEST_REL=$(GITHUB_API)/repos/$(ORG)/$(REPO)/releases
REL_TAG=$(shell curl -s $(LATEST_REL) | jq -r '.[0].tag_name')
##
# MAKE TASKS
# Tasks used locally and within our build pipelines to build, test and run our Docker image.
##
checkout_to_release:
git checkout -b release $(REL_TAG)
build: # Using the variables defined above, run `docker build`, tagging the image and passing in the required arguments.
docker build -t $(IMAGE):$(VERSION) -t $(IMAGE):latest \
--build-arg IMAGE_SERVICE_URL=$(IMAGE_SERVICE_URL) \
--build-arg PARLIAMENT_BASE_URL=$(PARLIAMENT_BASE_URL) \
--build-arg PARLIAMENT_AUTH_TOKEN=$(PARLIAMENT_AUTH_TOKEN) \
--build-arg AIRBRAKE_PROJECT_ID=$(AIRBRAKE_PROJECT_ID) \
--build-arg AIRBRAKE_PROJECT_KEY=$(AIRBRAKE_PROJECT_KEY) \
--build-arg BANDIERA_URL=$(BANDIERA_URL) \
--build-arg GTM_KEY=$(GTM_KEY) \
--build-arg ASSET_LOCATION_URL=$(ASSET_LOCATION_URL) \
--build-arg SECRET_KEY_BASE=$(SECRET_KEY_BASE) \
--build-arg RACK_ENV=$(RACK_ENV) \
--build-arg GIT_SHA="$(GIT_SHA)" \
--build-arg GIT_TAG="$(GIT_TAG)" \
.
run: # Run the Docker image we have created, mapping the HOST_PORT and CONTAINER_PORT
docker run --rm -p $(HOST_PORT):$(CONTAINER_PORT) $(IMAGE)
test: # Build the docker image in development mode, using a test PARLIAMENT_BASE_URL. Then run rake within a Docker container using our image.
RACK_ENV=development PARLIAMENT_BASE_URL=http://localhost:3030 BANDIERA_URL=http://localhost:5000 make build
docker run --rm $(IMAGE):latest bundle exec rake parallel:spec
dev: # Build, bundle and run a development version of our application
docker-compose up -d bandiera_db
docker-compose run bandiera bundle exec rake db:migrate
push: # Push the Docker images we have build to the configured Docker repository (Run in GoCD to push the image to AWS)
docker push $(IMAGE):$(VERSION)
docker push $(IMAGE):latest
scan-image:
docker login -u $(TENABLEIO_USER) -p $(TENABLEIO_PASSWORD) $(TENABLEIO_REGISTRY)
docker tag $(IMAGE):$(VERSION) $(TENABLEIO_REGISTRY)/$(TENABLEIO_REPO)/$(APP_NAME):$(VERSION)
docker push $(TENABLEIO_REGISTRY)/$(TENABLEIO_REPO)/$(APP_NAME):$(VERSION)
rmi: # Remove local versions of our images.
docker rmi -f $(IMAGE):$(VERSION)
docker rmi -f $(IMAGE):latest
docker rmi $(docker images -a | grep "^<none>" | awk '{print $3}') || true
deploy-ecs: # Deploy our new Docker image onto an AWS cluster (Run in GoCD to deploy to various environments).
./aws_ecs/register-task-definition.sh $(APP_NAME)
aws ecs update-service --service $(APP_NAME) --cluster $(ECS_CLUSTER) --region $(AWS_REGION) --task-definition $(APP_NAME)
airbrake: # Notify Airbrake that we have made a new deployment
curl -X POST -H "Content-Type: application/json" -d "{ \"environment\":\"${AIRBRAKE_ENVIRONMENT}\", \"username\":\"${AWS_ACCOUNT}\", \"repository\":\"${AIRBRAKE_REPOSITORY}\", \"revision\":\"${GIT_SHA}\", \"version\": \"${GIT_TAG}\" }" "https://airbrake.io/api/v4/projects/${AIRBRAKE_PROJECT_ID}/deploys?key=${AIRBRAKE_PROJECT_KEY}"