Let TravisCI automatically rebuild README.md when pushing

pull/1/merge
Michele Locati 2018-04-12 09:17:21 +02:00
parent e0965d93e3
commit 830d462bd8
No known key found for this signature in database
GPG Key ID: 98B7CE2E7234E28B
3 changed files with 120 additions and 0 deletions

17
.travis.yml Normal file
View File

@ -0,0 +1,17 @@
sudo: false
services:
- docker
env:
global:
- secure: "52OvxqyWxE2xRP+6+QqbYBJD8qMqgid4SPAWM70GOaksDMOtwuA7eJ+wY4qX69d8m8OwG5UTr7PUuYSYoCjKspgO9Uj5e1jX/u/LpsT+/C4WnwC/7mmQJ5LEOFEULDRzV0YarArsI3R9hfi1Dfs8Aym9wQ4fzL5L/b8gNLpwB9BYXd7F0vaKi0w+aDXCFGOVrD8+TrSFtRL3jN+ssx6iiBDISZOhcNnJwEKwe8SJryWttYHs6xPZrqCZ8pcev2K1UHt5Gyq5Rd5Nqz8ha1M73X0y2SuCxeAVPzg+wejU/KazPUkQpUCaftxOv2ilB6LlYLYpp/hr/bhX86tGkzi3Vx+lXyxXXzYS4vwDLXRjIX/EExG07rPM+tKU/tZGeQM3mxyK1pGMSvxHmUqjD/21WBze+oCChqhnZdPMsH4/XaKqZL2rDFMiEiQy1qyAkZajw5AUvfRuXxQhAzBmEzoep5p+6fgnmUTjqvNY0ateSOQLr3QHG2fQcIurKjSNjNHfhPfNeA1mi+Aulk1LR8eKaE4o7Q88TqG0QhCdRUfoNHgi8vQ+G3Vsaz9JM5wP3aUrjMMLOTR/PgY97HgqJvbwZM0efu1+kNfDm1ihYmHwR/6IhLu0yrZ7x8JAhD3uLMfhPxeuwY5Q7nzIH9JI7JnAwR4St997SdZY44xMluF1y0k="
install:
- true
script:
- if test "${TRAVIS_EVENT_TYPE:-}" = 'push'; then ./scripts/travisci-update-readme; fi
notifications:
email: false

View File

@ -26,3 +26,29 @@ stringInList () {
done done
return 1 return 1
} }
# Return the strings that are present in two space-separated strings
#
# Arguments:
# $1: the first space-separated string
# $2: the first second-separated string
#
# Output:
# The space-separated list of strings that are present in both lists
commonElements () {
commonElements_result=''
resetIFS
for commonElements_inA in ${1}
do
if stringInList "${commonElements_inA}" "${2}"
then
if test -z "${commonElements_result}"
then
commonElements_result="${commonElements_inA}"
else
commonElements_result="${commonElements_result} ${commonElements_inA}"
fi
fi
done
echo "${commonElements_result}"
}

77
scripts/travisci-update-readme Executable file
View File

@ -0,0 +1,77 @@
#!/bin/sh
# Let's set a sane environment
set -o errexit
set -o nounset
UPDATE_README_COMMIT_AUTHOR_NAME='Travis CI'
UPDATE_README_COMMIT_AUTHOR_EMAIL='michele@locati.it'
UPDATE_README_COMMIT_MESSAGE='Automatically update README.md'
WATCHED_FILES='data/special-requirements data/supported-extensions scripts/common scripts/update-readme scripts/travisci-update-readme'
echo 'Checking environment'
if test -z "${TRAVIS_BUILD_DIR:-}" -o -z "${TRAVIS_BRANCH:-}" -o -z "${TRAVIS_EVENT_TYPE:-}" -o -z "${TRAVIS_REPO_SLUG:-}"
then
echo 'Not in a TravisCI environment' >&2
exit 1
fi
if test -z "${GUTHUB_ACCESS_TOKEN:-}"
then
echo 'GitHub access token not set' >&2
# To create it:
# - go to https://github.com/settings/tokens/new?scopes=repo
# - create a new token
# - sudo apt install -y build-essential ruby ruby-dev
# - sudo gem install travis
# - travis encrypt -r <owner>/<repo> GUTHUB_ACCESS_TOKEN=<TOKEN>
# - Add to .travis.yml:
# env:
# global:
# - secure: "encrypted string"
exit 1
fi
if test "${TRAVIS_EVENT_TYPE}" != 'push'
then
echo 'This script should only be run in push builds' >&2
exit 1
fi
LAST_AUTO_UPDATED_IN="$(git -C "${TRAVIS_BUILD_DIR}" rev-list --max-count 1 --author "<${UPDATE_README_COMMIT_AUTHOR_EMAIL}>" --grep "${UPDATE_README_COMMIT_MESSAGE}" "${TRAVIS_BRANCH}")"
if test -z "${LAST_AUTO_UPDATED_IN}"
then
printf 'Rebuild of README.md enabled (no previous automatic refresh of it in branch %s)\n' "${TRAVIS_BRANCH}"
else
MODIFIED_FILES_SINCE="$(git -C "${TRAVIS_BUILD_DIR}" diff --name-only "${LAST_AUTO_UPDATED_IN}...${TRAVIS_BRANCH}")"
if test -z "$(commonElements "${WATCHED_FILES}" "${MODIFIED_FILES_SINCE}")"
then
printf 'Rebuild of README.md not needed (no relevant files changed in branch %s since commit %s)\n' "${TRAVIS_BRANCH}" "${LAST_AUTO_UPDATED_IN}"
exit 0
fi
printf 'Rebuild of README.md enabled (relevant files changed in branch %s since commit %s)\n' "${TRAVIS_BRANCH}" "${LAST_AUTO_UPDATED_IN}"
fi
echo 'Updating README.md'
"${TRAVIS_BUILD_DIR}/scripts/update-readme"
set -x
if test -z "$(git -C "${TRAVIS_BUILD_DIR}" ls-files -m | grep -E '^README\.md$')"
then
echo 'README.md has not been changed'
exit 0
fi
echo 'Committing changes to README.md'
git -C "${TRAVIS_BUILD_DIR}" config user.name "${UPDATE_README_COMMIT_AUTHOR_NAME}"
git -C "${TRAVIS_BUILD_DIR}" config user.email "${UPDATE_README_COMMIT_AUTHOR_EMAIL}"
git -C "${TRAVIS_BUILD_DIR}" add README.md
git -C "${TRAVIS_BUILD_DIR}" commit -m "${UPDATE_README_COMMIT_MESSAGE}"
printf 'Adding %s remote\n' "${TRAVIS_REPO_SLUG}"
git remote add deploy "https://${GUTHUB_ACCESS_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git"
printf 'Pushing to %s\n' "${TRAVIS_REPO_SLUG}"
git push deploy "${TARGET_BRANCH}"
git -C "${TRAVIS_BUILD_DIR}" push add deploy "https://${GUTHUB_ACCESS_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git"
echo 'Done.'