-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·286 lines (230 loc) · 6.29 KB
/
run
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
#!/usr/bin/env bash
set -o errexit
set -o pipefail
DC="${DC:-exec}"
# If we're running in CI we need to disable TTY allocation for docker compose
# commands that enable it by default, such as exec and run.
TTY=""
if [[ ! -t 1 ]]; then
TTY="-T"
fi
# -----------------------------------------------------------------------------
# Helper functions start with _ and aren't listed in this script's help menu.
# -----------------------------------------------------------------------------
function _dc {
docker compose "${DC}" ${TTY} "${@}"
}
function _build_run_down {
docker compose build
docker compose run ${TTY} "${@}"
docker compose down
}
# -----------------------------------------------------------------------------
function cmd {
# Run any command you want in the web container
_dc web "${@}"
}
function mix {
# Run any mix commands
echo "Running mix as user: $(id)"
echo "From directory: $(pwd)"
echo "Command: mix $*"
cmd mix "${@}"
}
function iex {
# Connect to an IEx session
cmd iex -S mix
}
function secret {
# Generate a random secret that can be used for secret keys and more
mix phx.gen.secret
}
function lint:dockerfile {
# Lint Dockerfile
docker container run --rm -i \
hadolint/hadolint hadolint --ignore DL3008 -t style "${@}" - < Dockerfile
}
function lint {
# Lint Elixir code
mix credo
}
function format {
# Run the mix formatter
mix format
}
function format:check {
# Check to see if there's an unformatted code
format --check-formatted
}
function security:check {
# Run Sobelow security checks
mix sobelow --config --exit
}
function test {
# Run test suite
_dc -e "MIX_ENV=test" web mix test "${@}"
}
function test:file {
# Run test suite and log to file
_dc -e "MIX_ENV=test" web mix test "${@}" > test_output.log 2>&1
}
function test:coverage {
echo "GITHUB_EVENT_PATH: $GITHUB_EVENT_PATH"
if [ -z "$GITHUB_EVENT_PATH" ]; then
echo "Running coverage locally without GitHub Actions environment"
_dc -e "MIX_ENV=test" web mix coveralls "${@}"
else
echo "Running coverage in GitHub Actions environment"
if [ -z "$COVERALLS_REPO_TOKEN" ]; then
echo "Error: COVERALLS_REPO_TOKEN is not set"
exit 1
fi
BRANCH=${GITHUB_REF#refs/heads/}
SHA=$GITHUB_SHA
COMMITTER=$GITHUB_ACTOR
_dc -e "MIX_ENV=test" \
-e "GITHUB_TOKEN=${GITHUB_TOKEN}" \
web mix coveralls.post \
--token "$COVERALLS_REPO_TOKEN" \
--branch "$BRANCH" \
--sha "$SHA" \
--committer "$COMMITTER" \
--name "GitHub Actions" \
"${@}"
fi
}
function test:coverage:details {
# Get test coverage details
_dc -e "MIX_ENV=test" web mix coveralls.detail "${@}"
}
function dialyzer {
# Run Dialyzer static code analysis
mix dialyzer "${@}"
}
function dialyzer:ci {
# Run Dialyzer in CI environment
_dc -e "MIX_ENV=test" web mix dialyzer --format short
}
function dialyzer:explain {
# Explain a Dialyzer warning
mix dialyzer.explain "${@}"
}
function shell {
# Start a shell session in the web container
cmd bash "${@}"
}
function psql {
# Connect to PostgreSQL
# shellcheck disable=SC1091
. .env
_dc postgres psql -U "${POSTGRES_USER}" "${@}"
}
function mix:install {
# Install mix dependencies and write lock file
_build_run_down web mix deps.get
}
function mix:outdated {
# List any installed packages that are outdated
mix hex.outdated
}
function yarn:install {
# Install yarn dependencies and write lock file
_build_run_down js yarn install
}
function yarn:remove {
# Install yarn dependencies and write lock file
_build_run_down js yarn remove "$@"
}
function yarn:outdated {
# List any installed packages that are outdated
_dc js yarn outdated
}
function yarn:build:js {
# Build JS assets, this is meant to be run from within the assets container
mkdir -p ../priv/static/js
node esbuild.config.mjs
}
function yarn:build:css {
# Build CSS assets, this is meant to be run from within the assets container
local args=()
if [ "${NODE_ENV:-}" == "production" ]; then
args=(--minify)
else
args=(--watch)
fi
mkdir -p ../priv/static/css
tailwindcss --postcss -i css/app.css -o ../priv/static/css/app.css "${args[@]}"
}
function prod:migrate {
# Run database migrations in production
cmd bin/portfolio eval "Portfolio.Release.migrate"
}
function prod:remote {
# Connect an IEx session to your production system
cmd bin/portfolio remote
}
function release {
# Build and tag the Docker image
docker build --no-cache -t portfolio:"$(git rev-parse --short HEAD) ."
# Push to Docker registry (uncomment and modify as needed)
# docker push your-registry/portfolio:$(git rev-parse --short HEAD)
echo "Release $(git rev-parse --short HEAD) built and ready for deployment"
}
function clean {
# Remove cache and other machine generates files
rm -rf priv/static/*.* priv/static/js priv/static/css priv/static/images priv/static/fonts
touch priv/static/.keep
}
function ci:install-deps {
# Unchanged
sudo apt-get install -y curl shellcheck
sudo curl \
-L https://raw.githubusercontent.com/nickjj/wait-until/v0.2.0/wait-until \
-o /usr/local/bin/wait-until && sudo chmod +x /usr/local/bin/wait-until
}
function ci:setup-env {
cp --no-clobber .env.example .env
docker compose build
docker compose up -d
# shellcheck disable=SC1091
. .env
wait-until "docker compose exec -T \
-e PGPASSWORD=${POSTGRES_PASSWORD} postgres \
psql -U ${POSTGRES_USER} ${POSTGRES_USER} -c 'SELECT 1'"
docker compose logs
}
function ci:lint {
shellcheck run bin/*
lint:dockerfile "${@}"
lint
format:check
}
function ci:security-check {
security:check
}
function ci:setup-db {
mix ecto.setup
}
function ci:test {
test:coverage "${@}"
}
function ci:static-analysis {
dialyzer:ci || true
}
function ci:run-all {
ci:install-deps
ci:setup-env
ci:lint "$@"
ci:security-check
# ci:setup-db
ci:test "$@"
ci:static-analysis
}
function help {
printf "%s <task> [args]\n\nTasks:\n" "${0}"
compgen -A function | grep -v "^_" | cat -n
printf "\nExtended help:\n Each task has comments for general usage\n"
}
# This idea is heavily inspired by: https://github.com/adriancooney/Taskfile
TIMEFORMAT=$'\nTask completed in %3lR'
time "${@:-help}"