Skip to content

Commit f159587

Browse files
authored
Merge pull request #576 from reactioncommerce/feat-docker-uid-match-3
feat: get host uid:gid and use in docker
2 parents be9e748 + 124aeaa commit f159587

File tree

9 files changed

+1259
-1305
lines changed

9 files changed

+1259
-1305
lines changed

.circleci/config.yml

Lines changed: 28 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,9 @@ defaults: &defaults
1717
- GLOBAL_CACHE_VERSION: “v3”
1818

1919
docker:
20-
- image: circleci/node:8-stretch
20+
- image: circleci/node:10-stretch
2121

2222
jobs:
23-
docker-build-nonprod-and-lint:
24-
<<: *defaults
25-
steps:
26-
- checkout
27-
- setup_remote_docker
28-
- run:
29-
name: Docker build non-prod and run lint
30-
command: |
31-
docker build \
32-
--build-arg "BUILD_COMPARE_URL=$CIRCLE_COMPARE_URL" \
33-
--build-arg "BUILD_DATE=$(date -u '+%Y-%m-%dT%H:%M:%SZ')" \
34-
--build-arg "BUILD_ENV=test" \
35-
--build-arg "BUILD_NUMBER=$CIRCLE_BUILD_NUM" \
36-
--build-arg "BUILD_PLATFORM=circleci" \
37-
--build-arg "BUILD_PLATFORM_PROJECT_REPONAME=$CIRCLE_PROJECT_REPONAME" \
38-
--build-arg "BUILD_PLATFORM_PROJECT_USERNAME=$CIRCLE_PROJECT_USERNAME" \
39-
--build-arg "BUILD_PULL_REQUESTS=$CI_PULL_REQUESTS" \
40-
--build-arg "BUILD_TRIGGERED_BY_TAG=$CIRCLE_TAG" \
41-
--build-arg "BUILD_URL=$CIRCLE_BUILD_URL" \
42-
--build-arg "CIRCLE_WORKFLOW_ID=$CIRCLE_WORKFLOW_ID" \
43-
--build-arg "CIRCLE_WORKFLOW_JOB_ID=$CIRCLE_WORKFLOW_JOB_ID" \
44-
--build-arg "CIRCLE_WORKFLOW_UPSTREAM_JOB_IDS=$CIRCLE_WORKFLOW_UPSTREAM_JOB_IDS" \
45-
--build-arg "CIRCLE_WORKSPACE_ID=$CIRCLE_WORKSPACE_ID" \
46-
--build-arg "GIT_REPOSITORY_URL=$CIRCLE_REPOSITORY_URL" \
47-
--build-arg "GIT_SHA1=$CIRCLE_SHA1" \
48-
--build-arg "LICENSE=GPL-3.0-only" \
49-
--build-arg "VCS_REF=$CIRCLE_SHA1" \
50-
--build-arg "VENDOR=Reaction Commerce" \
51-
-t "$DOCKER_REPOSITORY:non-prod" .
52-
docker run --rm $DOCKER_REPOSITORY:non-prod yarn run lint
53-
5423
docker-build:
5524
<<: *defaults
5625
steps:
@@ -59,62 +28,35 @@ jobs:
5928
- run:
6029
name: Discover Docker Tags
6130
command: |
31+
(cd /tmp && npm install --no-progress @reactioncommerce/[email protected] >/dev/null)
6232
mkdir -p docker-cache
63-
.circleci/bin/docker-tags "$CIRCLE_SHA1" "$CIRCLE_BRANCH" \
64-
> docker-cache/docker-tags.txt
65-
cat docker-cache/docker-tags.txt
33+
/tmp/node_modules/.bin/docker-tags "$CIRCLE_SHA1" "$CIRCLE_BRANCH" \
34+
| tee docker-cache/docker-tags.txt
6635
- run:
6736
name: Docker Build
6837
command: |
69-
docker build \
70-
--build-arg "BUILD_COMPARE_URL=$CIRCLE_COMPARE_URL" \
71-
--build-arg "BUILD_DATE=$(date -u '+%Y-%m-%dT%H:%M:%SZ')" \
72-
--build-arg "BUILD_ENV=test" \
73-
--build-arg "BUILD_NUMBER=$CIRCLE_BUILD_NUM" \
74-
--build-arg "BUILD_PLATFORM=circleci" \
75-
--build-arg "BUILD_PLATFORM_PROJECT_REPONAME=$CIRCLE_PROJECT_REPONAME" \
76-
--build-arg "BUILD_PLATFORM_PROJECT_USERNAME=$CIRCLE_PROJECT_USERNAME" \
77-
--build-arg "BUILD_PULL_REQUESTS=$CI_PULL_REQUESTS" \
78-
--build-arg "BUILD_TRIGGERED_BY_TAG=$CIRCLE_TAG" \
79-
--build-arg "BUILD_URL=$CIRCLE_BUILD_URL" \
80-
--build-arg "CIRCLE_WORKFLOW_ID=$CIRCLE_WORKFLOW_ID" \
81-
--build-arg "CIRCLE_WORKFLOW_JOB_ID=$CIRCLE_WORKFLOW_JOB_ID" \
82-
--build-arg "CIRCLE_WORKFLOW_UPSTREAM_JOB_IDS=$CIRCLE_WORKFLOW_UPSTREAM_JOB_IDS" \
83-
--build-arg "CIRCLE_WORKSPACE_ID=$CIRCLE_WORKSPACE_ID" \
84-
--build-arg "GIT_REPOSITORY_URL=$CIRCLE_REPOSITORY_URL" \
85-
--build-arg "GIT_SHA1=$CIRCLE_SHA1" \
86-
--build-arg "LICENSE=GPL-3.0-only" \
87-
--build-arg "VCS_REF=$CIRCLE_SHA1" \
88-
--build-arg "VENDOR=Reaction Commerce" \
89-
--build-arg BUILD_ENV=production \
90-
-t "$DOCKER_REPOSITORY:$CIRCLE_SHA1" .
38+
(cd /tmp && npm install --no-progress @reactioncommerce/[email protected] >/dev/null)
39+
/tmp/node_modules/.bin/docker-labels >> Dockerfile
40+
docker build --tag "$DOCKER_REPOSITORY:$CIRCLE_SHA1" .
9141
mkdir -p docker-cache
9242
docker save \
9343
-o docker-cache/docker-image.tar \
9444
"$DOCKER_REPOSITORY:$CIRCLE_SHA1"
95-
- run:
96-
name: Save Test .env for Workspace Jobs
97-
command: cp .env.example docker-cache/.env
9845
- persist_to_workspace:
9946
root: docker-cache
10047
paths:
10148
- docker-image.tar
10249
- docker-tags.txt
103-
- .env
104-
10550
docker-push:
10651
<<: *defaults
10752
steps:
10853
- setup_remote_docker
10954
- attach_workspace:
11055
at: docker-cache
11156
- run:
112-
name: Load Docker Image
57+
name: Load and Tag Docker Image
11358
command: |
11459
docker load < docker-cache/docker-image.tar
115-
- run:
116-
name: Tag Docker Image
117-
command: |
11860
cat docker-cache/docker-tags.txt \
11961
| xargs -t -I % \
12062
docker tag \
@@ -161,43 +103,41 @@ jobs:
161103
command: |
162104
.circleci/bin/ecs-deploy.sh
163105
164-
lint:
106+
lint-javascript:
165107
<<: *defaults
166108
steps:
167-
- setup_remote_docker
168-
- attach_workspace:
169-
at: docker-cache
170-
- run:
171-
name: Load Docker Image
172-
command: |
173-
docker load < docker-cache/docker-image.tar
109+
- checkout
174110
- run:
175-
name: Lint
111+
name: Lint JavaScript
176112
command: |
177-
docker run \
178-
--env-file docker-cache/.env \
179-
--name reactionapp_next_starterkit \
180-
"$DOCKER_REPOSITORY:$CIRCLE_SHA1" \
181-
yarn run lint
113+
yarn install
114+
yarn lint
182115
183116
test:
184117
<<: *defaults
185118
steps:
119+
- checkout
186120
- setup_remote_docker
187121
- attach_workspace:
188122
at: docker-cache
189123
- run:
190-
name: Load Docker Image
124+
name: Load and Tag Docker Image
191125
command: |
192126
docker load < docker-cache/docker-image.tar
127+
cat docker-cache/docker-tags.txt \
128+
| xargs -t -I % \
129+
docker tag \
130+
"$DOCKER_REPOSITORY:$CIRCLE_SHA1" \
131+
"$DOCKER_REPOSITORY:%"
193132
- run:
194133
name: Test
195134
command: |
196135
docker run \
197-
--env-file docker-cache/.env \
136+
--env-file .env.example \
137+
--env NODE_ENV=test \
198138
--name reactionapp_next_starterkit \
199139
"$DOCKER_REPOSITORY:$CIRCLE_SHA1" \
200-
yarn run test:ci
140+
yarn test:ci
201141
- run:
202142
name: Copy test artifacts from Remote Docker
203143
command: |
@@ -222,7 +162,7 @@ jobs:
222162
- run:
223163
name: Integration Test
224164
command: |
225-
docker run -e URL=${STAGING_URL} --env-file docker-cache/.env --name reaction_app_next_starterkit "$DOCKER_REPOSITORY:$CIRCLE_SHA1" yarn run test:integration
165+
docker run -e URL=${STAGING_URL} --env-file .env.example --name reaction_app_next_starterkit "$DOCKER_REPOSITORY:$CIRCLE_SHA1" yarn run test:integration
226166
- run:
227167
name: Install Broken Link Checker
228168
command: |
@@ -274,7 +214,7 @@ jobs:
274214
answer=$(./.circleci/bin/should-run-snyk.sh)
275215
if [[ "${answer}" =~ "^YES" ]] ; then
276216
docker run \
277-
--env-file docker-cache/.env \
217+
--env-file .env.example \
278218
--env "SNYK_TOKEN" \
279219
--name reactionapp_next_starterkit \
280220
--workdir /usr/local/src \
@@ -287,19 +227,16 @@ workflows:
287227
version: 2
288228
build_and_test:
289229
jobs:
290-
- docker-build-nonprod-and-lint:
230+
- lint-javascript:
291231
context: reaction-build-read
292232
- docker-build:
293233
context: reaction-build-read
234+
requires:
235+
- lint-javascript
294236
- docker-push:
295237
context: reaction-publish-docker
296238
requires:
297239
- docker-build
298-
- docker-build-nonprod-and-lint
299-
# - lint:
300-
# context: reaction-validation
301-
# requires:
302-
# - docker-build
303240
- test:
304241
context: reaction-validation
305242
requires:

.reaction/entrypoint.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
# Please Use Google Shell Style: https://google.github.io/styleguide/shell.xml
4+
5+
# ---- Start unofficial bash strict mode boilerplate
6+
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
7+
set -o errexit # always exit on error
8+
set -o errtrace # trap errors in functions as well
9+
set -o pipefail # don't ignore exit codes when piping output
10+
set -o posix # more strict failures in subshells
11+
# set -x # enable debugging
12+
13+
IFS=$'\n\t'
14+
# ---- End unofficial bash strict mode boilerplate
15+
16+
cd "$(dirname "${BASH_SOURCE[0]}")/.."
17+
# change the node user's uid:gid to match the repo root directory's
18+
usermod --uid "$(stat -c "%u" .)" --non-unique node |& grep -v "no changes" || true
19+
./.reaction/fix-volumes.sh
20+
export NODE_ENV="${NODE_ENV:-production}"
21+
command=(node .)
22+
if [[ $# -gt 0 ]]; then
23+
command=($@)
24+
fi
25+
unset IFS
26+
exec su-exec node ${command[*]}

.reaction/fix-volumes.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
3+
# Please Use Google Shell Style: https://google.github.io/styleguide/shell.xml
4+
5+
# ---- Start unofficial bash strict mode boilerplate
6+
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
7+
set -o errexit # always exit on error
8+
set -o errtrace # trap errors in functions as well
9+
set -o pipefail # don't ignore exit codes when piping output
10+
set -o posix # more strict failures in subshells
11+
# set -x # enable debugging
12+
13+
IFS=$'\n\t'
14+
# ---- End unofficial bash strict mode boilerplate
15+
16+
cd "$(dirname "${BASH_SOURCE[0]}")/.."
17+
owner=$(stat -c "%u:%g" .)
18+
volumes=(
19+
./node_modules
20+
./build
21+
/home/node/.cache/yarn
22+
/home/node/.cache/yarn-offline-mirror
23+
)
24+
for dir in ${volumes[*]}; do
25+
mkdir -p "${dir}"
26+
old_owner=$(stat -c "%u:%g" "${dir}")
27+
if [[ "$1" != "--force" && "${old_owner}" == "${owner}" ]]; then
28+
continue
29+
fi
30+
printf "Fixing volume ${dir} (before=${old_owner} after=${owner})…"
31+
chown -R "${owner}" "${dir}"
32+
chmod -R a+r,u+rw "${dir}"
33+
echo ""
34+
done

0 commit comments

Comments
 (0)