Combine update-readme and tag-and-release GitHub Actions

This commit is contained in:
Michele Locati 2020-12-09 10:43:12 +01:00
parent bf047ffdbd
commit 8f3ff464da
No known key found for this signature in database
GPG Key ID: 98B7CE2E7234E28B
4 changed files with 42 additions and 109 deletions

View File

@ -1,4 +1,4 @@
name: Create tag and release name: Update README and create release
on: on:
push: push:
@ -7,6 +7,9 @@ on:
create: create:
tags: tags:
- "*" - "*"
repository_dispatch:
types:
- readme-release
jobs: jobs:
create-and-release: create-and-release:
@ -14,19 +17,36 @@ jobs:
env: env:
VERSIONTAG_THIS: "" VERSIONTAG_THIS: ""
steps: steps:
- name: Sleep for a while after a push
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
uses: juliangruber/sleep-action@v1
with:
time: 30s
- name: Checkout - name: Checkout
if: (github.event_name == 'push' && github.ref == 'refs/heads/master') || (github.event_name == 'create' && github.event.ref_type == 'tag') if: >
(github.event_name == 'push' && github.ref == 'refs/heads/master')
|| (github.event_name == 'create' && github.event.ref_type == 'tag')
|| github.event_name == 'repository_dispatch'
uses: actions/checkout@v2 uses: actions/checkout@v2
with: with:
ref: master ref: master
fetch-depth: 0 fetch-depth: 0
- name: Update README
if: >
(github.event_name == 'push' && github.ref == 'refs/heads/master')
|| github.event_name == 'repository_dispatch'
run: |
./scripts/update-readme
if ! git ls-files -m | grep -qE '^README\.md$'; then
echo 'README.md has not been changed'
else
echo 'README.md has been updated'
git config user.name 'Michele Locati'
git config user.email 'michele@locati.it'
git add README.md
git commit -m '[skip ci] Automatically update README.md'
git push
fi
- name: Looking for the recent version tags - name: Looking for the recent version tags
if: (github.event_name == 'push' && github.ref == 'refs/heads/master') || (github.event_name == 'create' && github.event.ref_type == 'tag') if: >
(github.event_name == 'push' && github.ref == 'refs/heads/master')
|| (github.event_name == 'create' && github.event.ref_type == 'tag')
|| github.event_name == 'repository_dispatch'
run: | run: |
TAGS="$(git tag --list --sort=-version:refname)" TAGS="$(git tag --list --sort=-version:refname)"
VERSIONTAG_LAST= VERSIONTAG_LAST=
@ -51,12 +71,12 @@ jobs:
- name: Checking if we need to create a version tag after a push - name: Checking if we need to create a version tag after a push
if: github.event_name == 'push' && github.ref == 'refs/heads/master' if: github.event_name == 'push' && github.ref == 'refs/heads/master'
run: | run: |
VERSIONTAG_THIS=
if test -z "$VERSIONTAG_LAST"; then if test -z "$VERSIONTAG_LAST"; then
printf 'We need to create the first version tag\n' printf 'We need to create the first version tag\n'
VERSIONTAG_THIS=1.0.0 VERSIONTAG_THIS=1.0.0
elif test "$GITHUB_SHA" = "$(git rev-list -n 1 "refs/tags/$VERSIONTAG_LAST")"; then elif test "$(git rev-parse HEAD)" = "$(git rev-list -n 1 "refs/tags/$VERSIONTAG_LAST")"; then
printf 'Another action should already take care of creating the release\n' printf 'Another action should already take care of creating the release\n'
VERSIONTAG_THIS=
else else
CREATE_TAG=n CREATE_TAG=n
for CHANGED_FILE in $(git diff --name-only "refs/tags/$VERSIONTAG_LAST" HEAD); do for CHANGED_FILE in $(git diff --name-only "refs/tags/$VERSIONTAG_LAST" HEAD); do
@ -70,17 +90,24 @@ jobs:
VERSIONTAG_THIS="${VERSIONTAG_LAST%.*}.$((${VERSIONTAG_LAST##*.}+1))" VERSIONTAG_THIS="${VERSIONTAG_LAST%.*}.$((${VERSIONTAG_LAST##*.}+1))"
printf 'We need to create new version tag %s since relevant files changed\n' "$VERSIONTAG_THIS" printf 'We need to create new version tag %s since relevant files changed\n' "$VERSIONTAG_THIS"
else else
VERSIONTAG_THIS=
printf 'We do not need to create a new version tag since no relevant files changed\n' printf 'We do not need to create a new version tag since no relevant files changed\n'
fi fi
fi fi
printf 'VERSIONTAG_THIS=%s\n' "$VERSIONTAG_THIS" >> "$GITHUB_ENV" printf 'VERSIONTAG_THIS=%s\n' "$VERSIONTAG_THIS" >> "$GITHUB_ENV"
printf 'VERSIONTAG_PREVIOUS=%s\n' "$VERSIONTAG_LAST" >> "$GITHUB_ENV" printf 'VERSIONTAG_PREVIOUS=%s\n' "$VERSIONTAG_LAST" >> "$GITHUB_ENV"
- name: Sleep for a while before creating a tag
if: env.VERSIONTAG_THIS != ''
uses: juliangruber/sleep-action@v1
with:
time: 30s
- name: Create version tag after a push in the local repository - name: Create version tag after a push in the local repository
if: github.event_name == 'push' && github.ref == 'refs/heads/master' && env.VERSIONTAG_THIS != '' id: create_tag
run: git tag -- "$VERSIONTAG_THIS" if: env.VERSIONTAG_THIS != ''
run: |
git tag -- "$VERSIONTAG_THIS"
echo "::set-output name=tag_sha::$(git rev-parse HEAD)"
- name: Create version tag after a push in the remote repository - name: Create version tag after a push in the remote repository
if: github.event_name == 'push' && github.ref == 'refs/heads/master' && env.VERSIONTAG_THIS != '' if: env.VERSIONTAG_THIS != ''
uses: actions/github-script@v3 uses: actions/github-script@v3
with: with:
github-token: ${{ secrets.GITHUB_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }}
@ -89,7 +116,7 @@ jobs:
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
ref: `refs/tags/${process.env.VERSIONTAG_THIS}`, ref: `refs/tags/${process.env.VERSIONTAG_THIS}`,
sha: context.sha sha: steps.create_tag.outputs.tag_sha
}) })
- name: Check format of received tag - name: Check format of received tag
if: github.event_name == 'create' && github.event.ref_type == 'tag' if: github.event_name == 'create' && github.event.ref_type == 'tag'

View File

@ -1,23 +0,0 @@
name: Update README
on:
push:
branches:
- master
repository_dispatch:
types:
- update-readme
jobs:
update_readme:
name: Update README
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[skip ci]')"
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Update README
env:
DEPLOY_KEY: "${{ secrets.DEPLOY_KEY }}"
GIT_SSH_COMMAND: "ssh -o StrictHostKeyChecking=no"
run: ./scripts/ci-update-readme

View File

@ -1,70 +0,0 @@
#!/bin/sh
# Let's set a sane environment
set -o errexit
set -o nounset
UPDATE_README_COMMIT_AUTHOR_NAME='CI'
UPDATE_README_COMMIT_AUTHOR_EMAIL='michele@locati.it'
UPDATE_README_COMMIT_MESSAGE='[skip ci] Automatically update README.md'
WATCHED_FILES='data/special-requirements data/supported-extensions scripts/common scripts/update-readme scripts/ci-update-readme'
UPDATE_BRANCH='master'
echo 'Checking environment'
if test -z "${GITHUB_WORKSPACE:-}" || test -z "${GITHUB_REF:-}" || test -z "${GITHUB_EVENT_NAME:-}" || test -z "${GITHUB_REPOSITORY:-}"; then
echo 'Not in a GitHub Actions environment' >&2
exit 1
fi
if test "$GITHUB_EVENT_NAME" != 'push' || test "$GITHUB_REF" != "refs/heads/$UPDATE_BRANCH"; then
echo 'This script should only be run in push builds to %s' "$UPDATE_BRANCH" >&2
exit 1
fi
if test -z "${DEPLOY_KEY:-}"; then
echo 'GitHub deploy key not set' >&2
exit 1
fi
SCRIPTS_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
. "$SCRIPTS_DIR/common"
cd "$GITHUB_WORKSPACE"
printf 'Checking-out branch %s\n' "$UPDATE_BRANCH"
git checkout --force "$UPDATE_BRANCH"
LAST_AUTO_UPDATED_IN="$(git rev-list --max-count 1 --author "<$UPDATE_README_COMMIT_AUTHOR_EMAIL>" --grep "$UPDATE_README_COMMIT_MESSAGE" "$UPDATE_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' "$UPDATE_BRANCH"
else
MODIFIED_FILES_SINCE="$(git diff --name-only "$LAST_AUTO_UPDATED_IN...$UPDATE_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' "$UPDATE_BRANCH" "$LAST_AUTO_UPDATED_IN"
exit 0
fi
printf 'Rebuild of README.md enabled (relevant files changed in branch %s since commit %s)\n' "$UPDATE_BRANCH" "$LAST_AUTO_UPDATED_IN"
fi
echo 'Updating README.md'
"$SCRIPTS_DIR/update-readme"
if test -z "$(git ls-files -m | grep -E '^README\.md$')"; then
echo 'README.md has not been changed'
exit 0
fi
printf 'Initializing SSH'
eval "$(ssh-agent)"
printf '%s' "$DEPLOY_KEY" | tr -d '\r' | ssh-add - >/dev/null
printf 'Adding %s remote\n' "$GITHUB_REPOSITORY"
git remote add deploy "git@github.com:$GITHUB_REPOSITORY.git"
echo 'Committing changes to README.md'
git config user.name "$UPDATE_README_COMMIT_AUTHOR_NAME"
git config user.email "$UPDATE_README_COMMIT_AUTHOR_EMAIL"
git add README.md
git commit -m "$UPDATE_README_COMMIT_MESSAGE"
printf 'Pushing to %s\n' "$GITHUB_REPOSITORY"
git push deploy "$UPDATE_BRANCH:$UPDATE_BRANCH"
echo 'Done.'

View File

@ -23,7 +23,6 @@ shfmt $PARAMS \
scripts/ci-filter-supported-extensions \ scripts/ci-filter-supported-extensions \
scripts/ci-retrieve-recent-extensions \ scripts/ci-retrieve-recent-extensions \
scripts/ci-test-extensions \ scripts/ci-test-extensions \
scripts/ci-update-readme \
scripts/common \ scripts/common \
scripts/invoke-shfmt \ scripts/invoke-shfmt \
scripts/lint \ scripts/lint \