Skip to content

Commit

Permalink
ci: throttle the push flows for slow builds
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 19, 2024
1 parent f22a0b1 commit 90a89c3
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 23 deletions.
30 changes: 30 additions & 0 deletions .github/actions/check-artifact/action.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 0 additions & 7 deletions .github/workflows/build-prestashop.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
name: 'Build prestashop images'

on:
push:
branches:
- main

paths:
- images/prestashop/**/*

workflow_dispatch:

permissions:
Expand Down
10 changes: 1 addition & 9 deletions .github/workflows/build-shopware.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
name: 'Build shopware images'

on:
push:
branches:
- main

paths:
- images/shopware/**/*

workflow_dispatch:

permissions:
Expand All @@ -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:
Expand Down
7 changes: 0 additions & 7 deletions .github/workflows/build-wordpress.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
name: 'Build wordpress images'

on:
push:
branches:
- main

paths:
- images/wordpress/**/*

workflow_dispatch:

permissions:
Expand Down
67 changes: 67 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -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 }}
77 changes: 77 additions & 0 deletions .github/workflows/schedule-trigger.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 90a89c3

Please sign in to comment.