From 90a89c3ce27d69741cd1b0692395665a2c4f6c21 Mon Sep 17 00:00:00 2001 From: Edie Lemoine Date: Thu, 19 Sep 2024 11:51:05 +0200 Subject: [PATCH] ci: throttle the push flows for slow builds --- .github/actions/check-artifact/action.yml | 30 +++++++++ .github/workflows/build-prestashop.yml | 7 --- .github/workflows/build-shopware.yml | 10 +-- .github/workflows/build-wordpress.yml | 7 --- .github/workflows/push.yml | 67 ++++++++++++++++++++ .github/workflows/schedule-trigger.yml | 77 +++++++++++++++++++++++ 6 files changed, 175 insertions(+), 23 deletions(-) create mode 100644 .github/actions/check-artifact/action.yml create mode 100644 .github/workflows/push.yml create mode 100644 .github/workflows/schedule-trigger.yml diff --git a/.github/actions/check-artifact/action.yml b/.github/actions/check-artifact/action.yml new file mode 100644 index 0000000..7a0e9c1 --- /dev/null +++ b/.github/actions/check-artifact/action.yml @@ -0,0 +1,30 @@ +name: 'Check if artifact exists' + +inputs: + name: + description: 'Name of the artifact to check' + required: true + +outputs: + exists: + description: 'Whether the artifact exists' + value: ${{ steps.check.outputs.exists }} + +runs: + using: composite + steps: + - name: 'Check if artifact exists' + id: check + env: + ARTIFACT_NAME: ${{ inputs.name }} + REPO: ${{ github.repository }} + shell: bash + #language=bash + run: | + file=$(gh api "repos/$REPO/actions/artifacts" | jq ".artifacts[] | select(.name == \"$ARTIFACT_NAME\")") + + if [ -n "$file" ]; then + echo "exists=true" >> $GITHUB_OUTPUT + else + echo "exists=false" >> $GITHUB_OUTPUT + fi diff --git a/.github/workflows/build-prestashop.yml b/.github/workflows/build-prestashop.yml index 28aec5b..5a320cd 100644 --- a/.github/workflows/build-prestashop.yml +++ b/.github/workflows/build-prestashop.yml @@ -1,13 +1,6 @@ name: 'Build prestashop images' on: - push: - branches: - - main - - paths: - - images/prestashop/**/* - workflow_dispatch: permissions: diff --git a/.github/workflows/build-shopware.yml b/.github/workflows/build-shopware.yml index 43d1303..c4ab935 100644 --- a/.github/workflows/build-shopware.yml +++ b/.github/workflows/build-shopware.yml @@ -1,13 +1,6 @@ name: 'Build shopware images' on: - push: - branches: - - main - - paths: - - images/shopware/**/* - workflow_dispatch: permissions: @@ -24,11 +17,10 @@ jobs: with: min-version: '8.1' - build: runs-on: ubuntu-22.04 needs: - - prepare-wp + - prepare-php strategy: fail-fast: false matrix: diff --git a/.github/workflows/build-wordpress.yml b/.github/workflows/build-wordpress.yml index 486c736..b462940 100644 --- a/.github/workflows/build-wordpress.yml +++ b/.github/workflows/build-wordpress.yml @@ -1,13 +1,6 @@ name: 'Build wordpress images' on: - push: - branches: - - main - - paths: - - images/wordpress/**/* - workflow_dispatch: permissions: diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml new file mode 100644 index 0000000..845dceb --- /dev/null +++ b/.github/workflows/push.yml @@ -0,0 +1,67 @@ +name: 'Schedule workflow run' + +on: + push: + branches: + - main + + paths: + - images/prestashop/** + - images/shopware/** + - images/wordpress/** + +jobs: + prepare: + runs-on: ubuntu-22.04 + strategy: + matrix: + image: + - prestashop + - shopware + - wordpress + steps: + - uses: tj-actions/changed-files@v45 + id: changed-files + with: + files: 'images/${{ matrix.image }}/**' + + - name: 'Prepare' + if: steps.changed-files.outputs.test_any_changed == 'true' + id: prepare + env: + IMAGE: ${{ matrix.image }} + shell: bash + #language=bash + run: | + echo "filename=.lock-$IMAGE.txt" >> $GITHUB_OUTPUT + echo "artifact-name=workflow-lock-$IMAGE" >> $GITHUB_OUTPUT + + - name: 'Check if lock file exists' + if: steps.changed-files.outputs.test_any_changed == 'true' + uses: ./.github/actions/check-artifact + id: check-lock + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + name: ${{ steps.prepare.outputs.artifact-name }} + + - if: steps.changed-files.outputs.test_any_changed == 'true' && steps.check-lock.outputs.exists == 'true' + shell: bash + #language=bash + run: echo "Lock file already exists." + + - name: 'Create lock file' + if: steps.changed-files.outputs.test_any_changed == 'true' && steps.check-lock.outputs.exists == 'false' + env: + FILENAME: ${{ steps.prepare.outputs.filename }} + shell: bash + #language=bash + run: | + echo "$(date)" > $FILENAME + + - name: 'Upload lock artifact' + uses: actions/upload-artifact@v4 + if: steps.changed-files.outputs.test_any_changed == 'true' && steps.check-lock.outputs.exists == 'false' + with: + name: ${{ steps.prepare.outputs.artifact-name}} + path: ${{ steps.prepare.outputs.filename }} diff --git a/.github/workflows/schedule-trigger.yml b/.github/workflows/schedule-trigger.yml new file mode 100644 index 0000000..c671e86 --- /dev/null +++ b/.github/workflows/schedule-trigger.yml @@ -0,0 +1,77 @@ +name: 'Trigger scheduled workflows' + +on: + # Run every day at 18:00 UTC + schedule: + - cron: '0 18 * * *' + + # Or trigger manually + workflow_dispatch: + +jobs: + run: + runs-on: ubuntu-22.04 + strategy: + matrix: + image: + - prestashop + - shopware + - wordpress + steps: + - name: 'Prepare' + id: prepare + env: + IMAGE: ${{ matrix.image }} + shell: bash + #language=bash + run: | + echo "filename=.lock-$IMAGE.txt" >> $GITHUB_OUTPUT + echo "artifact-name=workflow-lock-$IMAGE" >> $GITHUB_OUTPUT + + - name: 'Check if workflow is already pending' + id: check-lock + env: + ARTIFACT_NAME: ${{ steps.prepare.outputs.artifact-name }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + shell: bash + #language=bash + run: | + file=$(gh api "repos/$REPO/actions/artifacts" | jq ".artifacts[] | select(.name == \"$ARTIFACT_NAME\")") + + if [ -n "$file" ]; then + echo "has-lock=true" >> $GITHUB_OUTPUT + else + echo "has-lock=false" >> $GITHUB_OUTPUT + fi + + - name: Run actual job + if: steps.check-lock.outputs.has-lock == 'true' + env: + IMAGE: ${{ matrix.image }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + shell: bash + #language=bash + run: | + gh workflow run "build-$IMAGE.yml" + + echo "Job 'build-$IMAGE' dispatched." + + - name: 'Release the lock' + if: steps.check-lock.outputs.has-lock == 'true' + shell: bash + env: + ARTIFACT_NAME: ${{ steps.prepare.outputs.artifact-name }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + #language=bash + run: | + artifactId=$(gh api "/repos/$REPO/actions/artifacts" --jq ".artifacts[] | select(.name == \"$ARTIFACT_NAME\") | .id") + + if [ -n "$artifactId" ]; then + gh api --method DELETE "/repos/$REPO/actions/artifacts/$artifactId" + echo "Lock artifact deleted successfully." + else + echo "No lock artifact found." + exit 1 + fi