-
Notifications
You must be signed in to change notification settings - Fork 5
/
Makefile
166 lines (134 loc) · 4.19 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
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
# -*- mode:makefile; coding:utf-8 -*-
CONFIG_ENV:= common-dev-assets/module-assets/config.env
COMMON_DEV_ASSETS_CONFIG_ENV:= module-assets/config.env
ifneq ("$(wildcard $(CONFIG_ENV))","")
include $(CONFIG_ENV)
else
ifneq ("$(wildcard $(COMMON_DEV_ASSETS_CONFIG_ENV))","")
include $(COMMON_DEV_ASSETS_CONFIG_ENV)
endif
endif
export
# explicitly tell Make these commands not associated with files
.PHONY: all dependency-install-darwin-linux dependency-pre-commit docker-pull docker-run pre-commit pre-commit-no-terraform go-mod-tidy run-tests run-tests-local run-go-module-tests renovate-sweeper netrc ghe-netrc
#
# simply expanded variables
#
IMAGE := ${IMAGE_NAME}:stable
ifeq ($(OS),Windows_NT) # is Windows_NT on XP, 2000, 7, Vista, 10...
detected_OS := Windows
else
detected_OS := $(shell uname) # same as "uname -s"
endif
.DEFAULT_GOAL = dependency-pre-commit
ifneq (,$(filter $(detected_OS), Darwin Linux))
.DEFAULT_GOAL = all
endif
ifndef DOCKER_ENV
DOCKER_ENV := common-dev-assets/module-assets/docker.env
endif
ifndef GO_TEST_DIR
GO_TEST_DIR := tests
endif
ifndef GO_MOD_DIR
GO_MOD_DIR := tests
endif
ifndef RUN
RUN := ''
endif
ifndef CONTAINER_RUNTIME
CONTAINER_RUNTIME := 'docker'
endif
ifneq (,$(findstring github.ibm.com,$(shell git config --get remote.origin.url)))
GHE := 'true'
endif
#
# install dependencies
#
all: dependency-install-darwin-linux dependency-pre-commit
dependency-install-darwin-linux:
./ci/install-deps.sh
dependency-pre-commit:
pre-commit install
#
# docker
#
docker-pull:
${CONTAINER_RUNTIME} pull "${DOCKER_REGISTRY}/$(IMAGE)"
docker-run:
@# create all of the directories + files we are mounting if they do not already exist to prevent "Error response from daemon: error while creating mount source path"
@mkdir -p ~/.ssh
@touch ~/.netrc ~/.gitconfig
@# Mount into /tmp (as read only) so files can be moved and permissions updated for root user
@${CONTAINER_RUNTIME} run -it -v $$(pwd):/mnt \
--platform linux/amd64 \
-v ~/.ssh:/tmp/.ssh:ro \
-v ~/.netrc:/tmp/.netrc:ro \
-v ~/.gitconfig:/tmp/.gitconfig:ro \
${DOCKER_REGISTRY}/$(IMAGE) \
bash -c "cp -R /tmp/.ssh /root && \
cp /tmp/.netrc /root/.netrc && \
cp /tmp/.gitconfig /etc/gitconfig && \
chown -R root /root/.ssh /root/.netrc /etc/gitconfig && \
chgrp -R root /root/.ssh /root/.netrc && \
git config --global --add safe.directory '*' && \
bash"
#
# pre-commit
#
# pre-commit for repos with terraform code
pre-commit: ghe-netrc
@echo "Running terraform init .."
@terraform init
@echo
@echo "Running pre-commit hooks .."
@pre-commit run --all-files
# pre-commit for non-terraform repos
pre-commit-no-terraform:
@echo "Running pre-commit hooks .."
@pre-commit run --all-files
# run pre-commit checks against renovate PRs, and commit back any changes to the PR (e.g. doc updates, secrets baseline etc)
renovate-sweeper: ghe-netrc
ci/renovate-sweeper.sh
#
# tests
#
# run tests for terraform repos (pipeline execution only - not for local usage)
run-tests: SHELL:=/bin/bash
run-tests: ghe-netrc
ci/run-tests.sh
# run tests for terraform repos locally
run-tests-local:
cd ${GO_TEST_DIR} && go test -run ${RUN} -count=1 -v -timeout 600m
# run unit tests for golang projects
run-go-module-tests:
go test $$(go list ./... | grep -v /common-dev-assets/) -count=1 -v -timeout 5m
# go mod tidy
go-mod-tidy:
@cd ${GO_MOD_DIR}; go mod tidy -v ${GO_MOD_ARGS}
#
# netrc
#
# Create entry in ~/.netrc for cm.globalcatalog.cloud.ibm.com using IAM bearer token
netrc:
@if [ -z "$${TF_VAR_ibmcloud_api_key}" ]; then echo "Error: TF_VAR_ibmcloud_api_key is undefined"; exit 1; fi
ibmcloud login --no-region --apikey "$$TF_VAR_ibmcloud_api_key"
while true ; do \
ibmcloud catalog netrc ; \
echo "refreshed netrc" ; \
sleep 10m ; \
done & \
sleep 5
# Create entry in ~/.netrc for github.ibm.com using $GH_TOKEN (no action taken if github.com detected)
ghe-netrc:
ifdef GHE
ifndef GH_TOKEN
$(error Error: GH_TOKEN is undefined)
endif
@touch ~/.netrc
@if ! grep -q "machine github.ibm.com" ~/.netrc; then \
echo -e "machine github.ibm.com\n login $$GH_TOKEN" >> ~/.netrc; \
else \
echo "Found entry already exists in ~/.netrc for github.ibm.com. Taking no action."; \
fi
endif