From 98bf595324169387e4da66ac113615c3b4ebcf2c Mon Sep 17 00:00:00 2001 From: Arthur Meyre Date: Wed, 22 Sep 2021 12:46:51 +0200 Subject: [PATCH] chore(build): get last completed run to check whether to rebuild docker --- .github/workflows/continuous-integration.yaml | 27 +++++++- script/actions_utils/get_latest_run.sh | 61 +++++++++++++++++++ 2 files changed, 85 insertions(+), 3 deletions(-) create mode 100755 script/actions_utils/get_latest_run.sh diff --git a/.github/workflows/continuous-integration.yaml b/.github/workflows/continuous-integration.yaml index ee2253e26..dc84d3074 100644 --- a/.github/workflows/continuous-integration.yaml +++ b/.github/workflows/continuous-integration.yaml @@ -45,12 +45,33 @@ jobs: steps: - uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f + with: + fetch-depth: 0 - name: Get changed files if: ${{ (github.event_name == 'push' && !startsWith(github.ref, 'refs/tags/')) || github.event_name == 'pull_request' }} - uses: Ana06/get-changed-files@ea75ed777daf24d6e95f43957bd26b1eab20806c id: files - with: - format: 'space-delimited' + env: + IS_PR: ${{ github.event_name == 'pull_request' }} + run: | + CURRENT_SHA="${{ github.sha }}" + if [[ "${IS_PR}" == "true" ]]; then + BEFORE_SHA="${{ github.event.pull_request.base.sha }}" + else + BEFORE_SHA="$(./script/actions_utils/get_latest_run.sh \ + --token ${{ secrets.BOT_TOKEN }} \ + --org-repo ${{ github.repository }} \ + --event-types 'push workflow_dispatch repository_dispatch')" + fi + ALL_CHANGED_FILES=$(git diff --name-only "${CURRENT_SHA}" "${BEFORE_SHA}") + + echo "Before sha1 ${BEFORE_SHA}" + echo "Current sha1 ${CURRENT_SHA}" + + echo "Changed files:" + echo "${ALL_CHANGED_FILES}" + + echo "::set-output name=all::${ALL_CHANGED_FILES//$'\n'/ }" + - name: Should rebuild docker check run: | set +e diff --git a/script/actions_utils/get_latest_run.sh b/script/actions_utils/get_latest_run.sh new file mode 100755 index 000000000..add01cd14 --- /dev/null +++ b/script/actions_utils/get_latest_run.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +TOKEN= +ORG_REPO= +EVENTS_TO_CHECK= + +while [ -n "$1" ] +do + case "$1" in + "--token" ) + shift + TOKEN="$1" + ;; + + "--org-repo" ) + shift + ORG_REPO="$1" + ;; + + "--event-types" ) + shift + EVENTS_TO_CHECK="$1" + ;; + + *) + echo "Unknown param : $1" + exit 1 + ;; + esac + shift +done + +# Store the workflows that come in jsons in a file per event type +declare -a JSON_FILES_ARRAY=() +for EVENT in $EVENTS_TO_CHECK; do + CURR_FILE="$(mktemp --suffix=.json)" + curl \ + -X GET \ + -H "Accept: application/vnd.github.v3+json" \ + -H "Authorization: token ${TOKEN}" \ + "https://api.github.com/repos/${ORG_REPO}/actions/runs?branch=main&event=${EVENT}&status=success" | \ + jq -rc '.workflow_runs | sort_by(.updated_at)[-1]' > "${CURR_FILE}" + JSON_FILES_ARRAY+=("${CURR_FILE}") +done + +# Put all the workflows in the same json and dump that +CONCAT_FILE="$(mktemp --suffix=.json)" +jq -sr '.' "${JSON_FILES_ARRAY[@]}" > "${CONCAT_FILE}" + +# Sort by updated_at, get the last and get the sha1 for this last one +BEFORE_SHA=$(jq -rc 'sort_by(.updated_at)[-1].head_sha' "${CONCAT_FILE}") + +# Remove files +rm "${CONCAT_FILE}" + +for FILE_TO_RM in "${JSON_FILES_ARRAY[@]}"; do + rm "${FILE_TO_RM}" +done + +# Echo for the outside world +echo "${BEFORE_SHA}"