Skip to content

Commit 53b9ec6

Browse files
committed
Prevent duplicate builds
1 parent 90428ac commit 53b9ec6

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

.github/workflows/deploy.yml

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,48 @@ on:
99
required: false
1010
default: ''
1111
jobs:
12+
verify:
13+
name: Verify if a rebuild is needed
14+
runs-on: ubuntu-latest
15+
outputs:
16+
version: ${{ steps.get-latest-version.outputs.version }}
17+
rebuild: ${{ steps.check-if-built.outputs.rebuild }}
18+
steps:
19+
- name: Checkout repo
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
- name: Fetch latest release version if not provided
24+
id: get-latest-version
25+
env:
26+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
INPUT_VERSION: ${{ github.event.inputs.version }}
28+
run: |
29+
if [ -z "$INPUT_VERSION" ]; then
30+
VERSION=$(gh release view --repo "$GITHUB_REPOSITORY_OWNER/woocommerce" --json tagName -q .tagName)
31+
else
32+
VERSION="$INPUT_VERSION"
33+
fi
34+
35+
echo "version=$VERSION" >> $GITHUB_OUTPUT
36+
- name: Check if build is necessary
37+
id: check-if-built
38+
env:
39+
VERSION: ${{ steps.get-latest-version.outputs.version }}
40+
run: |
41+
last_built_version=$(git log -1 --pretty=%B 'origin/gh-pages' | grep -oP '\d+\.\d+\.\d+')
42+
43+
if [ "$last_built_version" = "$VERSION" ]; then
44+
echo "rebuild=false" >> $GITHUB_OUTPUT
45+
else
46+
echo "rebuild=true" >> $GITHUB_OUTPUT
47+
fi
48+
1249
build-and-deploy:
1350
name: Build and deploy
14-
runs-on: [ubuntu-latest]
51+
runs-on: ubuntu-latest
52+
needs: verify
53+
if: ${{ needs.verify.outputs.rebuild == 'true' }}
1554
steps:
1655
- name: Checkout code
1756
uses: actions/checkout@v4
@@ -31,22 +70,9 @@ jobs:
3170
restore-keys: ${{ runner.os }}-composer-
3271
- name: Install dependencies
3372
run: composer install --prefer-dist
34-
- name: Fetch latest release version if not provided
35-
id: get-latest-version
36-
env:
37-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
38-
INPUT_VERSION: ${{ github.event.inputs.version }}
39-
run: |
40-
if [ -z "$INPUT_VERSION" ]; then
41-
VERSION=$(gh release view --repo "$GITHUB_REPOSITORY_OWNER/woocommerce" --json tagName -q .tagName)
42-
else
43-
VERSION="$INPUT_VERSION"
44-
fi
45-
46-
echo "version=$VERSION" >> $GITHUB_OUTPUT
4773
- name: Build
4874
run: |
49-
./deploy.sh --build-only -s ${{ steps.get-latest-version.outputs.version }} -r "$GITHUB_REPOSITORY_OWNER/woocommerce"
75+
./deploy.sh --build-only -s ${{ needs.verify.outputs.version }} -r "$GITHUB_REPOSITORY_OWNER/woocommerce"
5076
- name: Deploy to GitHub Pages
5177
if: success()
5278
uses: crazy-max/ghaction-github-pages@59173cb633d9a3514f5f4552a6a3e62c6710355c # v2
@@ -55,4 +81,4 @@ jobs:
5581
with:
5682
target_branch: gh-pages
5783
build_dir: build/api
58-
commit_message: "${{ format('Build: {0}', steps.get-latest-version.outputs.version) }}"
84+
commit_message: "${{ format('Build: {0}', needs.verify.outputs.version) }}"

0 commit comments

Comments
 (0)