|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# |
| 4 | +# Update node Docker image and npm dependencies and run tests |
| 5 | +# |
| 6 | + |
| 7 | +set -o errexit # Exit immediately if any command or pipeline of commands fails |
| 8 | +set -o errtrace # Inherit ERR trap in functions, subshells and substitutions |
| 9 | +set -o nounset # Treat unset variables and parameters as an error |
| 10 | +set -o pipefail # Exit when command before pipe fails |
| 11 | +set -o xtrace # Debug mode expand everything and print it before execution |
| 12 | + |
| 13 | +cd "$(dirname "$0")" # Always run from script location |
| 14 | + |
| 15 | +test_image() { |
| 16 | + docker build -t zemanlx/remark-lint . |
| 17 | + docker run --rm -i -v "${PWD}:/lint/input:ro" zemanlx/remark-lint --frail . |
| 18 | +} |
| 19 | + |
| 20 | +branch="update/$(date --utc '+%Y-%m-%d')" |
| 21 | + |
| 22 | +# Make sure we branch from the latest master |
| 23 | +git reset --quiet --hard HEAD |
| 24 | +git checkout --quiet master |
| 25 | +git pull --quiet |
| 26 | + |
| 27 | +# Delete that local branch if it already exist and create it again to start |
| 28 | +# from scratch |
| 29 | +if git show-ref --quiet --verify "refs/heads/${branch}"; then |
| 30 | + git branch --quiet --delete --force "${branch}" |
| 31 | +fi |
| 32 | +git checkout --quiet -b "${branch}" |
| 33 | + |
| 34 | +# |
| 35 | +# Update docker image |
| 36 | +# |
| 37 | + |
| 38 | +# Get latest tag of node LTS (10, 12, 14, etc.) version of node Docker image |
| 39 | +# based on alpine |
| 40 | +image_tag=$(curl 'https://hub.docker.com/v2/repositories/library/node/tags/?page_size=50&page=1&name=alpine' \ |
| 41 | + | jq -r '.results[].name' \ |
| 42 | + | grep -E "^($(seq --separator "|" 10 2 200))\.[0-9]+\.[0-9]-alpine[0-9]+\.[0-9]+$" \ |
| 43 | + | sort -t "." -k1n -k2n -k3n -k4n \ |
| 44 | + | tail -n 1) |
| 45 | + |
| 46 | +# Get major and minor version of git package for image_tag |
| 47 | +git_version=$(docker run -i --rm "node:${image_tag}" apk --no-cache --quiet list 'git' | sed -r 's/^git-([0-9]+\.[0-9]+).*/\1/') |
| 48 | + |
| 49 | +sed -i -r "s/^(FROM node:).+$/\1${image_tag}/" Dockerfile |
| 50 | +sed -i -r "s/(git~=)[0-9]+.[0-9]+/\1${git_version}/" Dockerfile |
| 51 | + |
| 52 | +# If there were any changes to the Dockerfile, test it and commit them |
| 53 | +if ! git diff --quiet Dockerfile; then |
| 54 | + test_image |
| 55 | + git add Dockerfile |
| 56 | + git commit --message "Update Docker image: ${image_tag} and git: ${git_version}" |
| 57 | +fi |
| 58 | + |
| 59 | +# |
| 60 | +# Update npm dependencies |
| 61 | +# |
| 62 | + |
| 63 | +# Install current dependencies to do not duplicated packages in 'npm outdated' |
| 64 | +docker run --rm -i --volume "${PWD}:${PWD}" --workdir "${PWD}" "node:${image_tag}" \ |
| 65 | + ash -c 'npm install && chown '"$(id -u):$(id -g)"' -R .' |
| 66 | + |
| 67 | +# Update package.json to latest dependencies |
| 68 | +if ! docker run --rm -i --volume "${PWD}:${PWD}" --workdir "${PWD}" "node:${image_tag}" \ |
| 69 | + npm outdated; then |
| 70 | + |
| 71 | + docker run --rm -i --volume "${PWD}:${PWD}" --workdir "${PWD}" "node:${image_tag}" \ |
| 72 | + ash -c 'for pkg in $(sed -nr '\''s/.*"(.*)": ".*",*/\1/p'\'' package.json);do npm install --save-exact "${pkg}@latest";done && chown '"$(id -u):$(id -g)"' -R .' |
| 73 | +fi |
| 74 | + |
| 75 | +# Update package-lock.json |
| 76 | +docker run --rm -i --volume "${PWD}:${PWD}" --workdir "${PWD}" "node:${image_tag}" \ |
| 77 | + ash -c 'npm update && chown '"$(id -u):$(id -g)"' -R .' |
| 78 | + |
| 79 | +# If there were any changes to the Dockerfile, test it and commit them |
| 80 | +if ! git diff --quiet package.json package-lock.json; then |
| 81 | + test_image |
| 82 | + git add package.json package-lock.json |
| 83 | + git commit --message "Update npm packages and its dependencies" |
| 84 | +fi |
0 commit comments