Skip to content

Instantly share code, notes, and snippets.

@simonLeary42
Last active August 1, 2023 22:37
Show Gist options
  • Save simonLeary42/3b037318aa73b100bcf9a5658e7e8e0f to your computer and use it in GitHub Desktop.
Save simonLeary42/3b037318aa73b100bcf9a5658e7e8e0f to your computer and use it in GitHub Desktop.
general purpose GitLab CI style check with prettier, plus local shell script to format with prettier. This ensures that the local formatting and the remote style check use the same version, and little effort is required on the local end
prettier_style_check:
image: node:latest
stage: test
script:
- npm install -g prettier
- prettier --version
- prettier . --check || { echo "did you run \`format.sh\`?"; exit 1; }
#!/bin/bash
set -e
CONTAINER_NAME="gitlab-prettier-ci"
echo "I will build a docker container on your machine (about 1G), bind mount your current working directory, and format the files in that directory with \`prettier\`. If I have already built a container, I won't build another."
echo "If that's ok with you, press enter. Else, press ctrl+C."
read confirm_ok
if [ ! -z "$(docker images -q $CONTAINER_NAME)" ]; then
docker run -v "$PWD:/app" $CONTAINER_NAME
exit 0
fi
docker build --tag $CONTAINER_NAME --file - <<- DOCKERFILE
FROM node:latest
WORKDIR /app
RUN npm install -g prettier
CMD ["prettier", "/app", "--write"]
DOCKERFILE
docker run -v "$PWD:/app" $CONTAINER_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment