Skip to content

Commit

Permalink
Merge pull request #1 from creative-commoners/pulls/1/trigger-ci
Browse files Browse the repository at this point in the history
NEW Create gha-trigger-ci
  • Loading branch information
GuySartorelli authored Aug 17, 2023
2 parents ebc1dda + 3153700 commit d347433
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 1 deletion.
29 changes: 29 additions & 0 deletions .github/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2023, Silverstripe Limited - www.silverstripe.com
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11 changes: 11 additions & 0 deletions .github/workflows/action-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Action CI

on:
push:
pull_request:
workflow_dispatch:

jobs:
actionci:
name: Action CI
uses: silverstripe/gha-action-ci/.github/workflows/action-ci.yml@v1
12 changes: 12 additions & 0 deletions .github/workflows/auto-tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Auto-tag
on:
push:
tags:
- '*.*.*'
jobs:
auto-tag:
name: Auto-tag
runs-on: ubuntu-latest
steps:
- name: Auto-tag
uses: silverstripe/gha-auto-tag@v1
17 changes: 17 additions & 0 deletions .github/workflows/keepalive.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Keepalive

on:
# At 3:15 PM UTC, on day 20 of the month
schedule:
- cron: '15 15 20 * *'
workflow_dispatch:

jobs:
keepalive:
name: Keepalive
# Only run cron on the silverstripe account
if: (github.event_name == 'schedule' && github.repository_owner == 'silverstripe') || (github.event_name != 'schedule')
runs-on: ubuntu-latest
steps:
- name: Keepalive
uses: silverstripe/gha-keepalive@v1
17 changes: 17 additions & 0 deletions .github/workflows/merge-up.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Merge-up

on:
# At 3:15 PM UTC, only on Thursday
schedule:
- cron: '15 15 * * 4'
workflow_dispatch:

jobs:
merge-up:
name: Merge-up
# Only run cron on the silverstripe account
if: (github.event_name == 'schedule' && github.repository_owner == 'silverstripe') || (github.event_name != 'schedule')
runs-on: ubuntu-latest
steps:
- name: Merge-up
uses: silverstripe/gha-merge-up@v1
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
# gha-trigger-ci
GitHub Actions Workflow - Trigger a CI workflow

Trigger a Silverstripe CI workflow

For repositories on the Silverstripe account there are currently 3 different filenames that a CI workflow can be:
- ci.yml -- used for regular modules
- action-ci.yml -- used on gha-* repositories
- action-ci-self.yml -- used only on gha-action-ci

This action will find and trigger the appropriate CI workflow.

Note this is similarly named but distinct from [gha-dispatch-ci](https://github.com/silverstripe/gha-dispatch-ci) which
is used on a schedule event to trigger a CI workflow on two different major versions of supported Silverstripe modules.

## Usage

**workflow.yml**
```yml
steps:
- name: Trigger CI
uses: silverstripe/gha-trigger-ci@v1
with:
branch: 4.13
```
### Inputs:
#### Branch
The branch to run the workflow on
`branch: pulls/4/my-branch`

#### Validate branch
Whether to validate if the branch exists via GitHub API. You may wish to disable this if the branch was only just created
and the GitHub API hasn't caught up yet. Defaults is `true`
`validate-branch: false`
98 changes: 98 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Trigger CI
description: GitHub Action to find and trigger the appropriate CI workflow

inputs:
branch:
type: string
required: true
validate_branch:
type: boolean
required: false
default: true

runs:
using: composite
steps:

- name: Validate branch exists
shell: bash
if: inputs.validate_branch
env:
GITHUB_REPOSITORY: ${{ github.repository }}
BRANCH: ${{ inputs.branch }}
run: |
# https://docs.github.com/en/free-pro-team@latest/rest/branches/branches?apiVersion=2022-11-28#list-branches
RESP_CODE=$(curl -w %{http_code} -s -L -o __branches.json \
-X GET \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$GITHUB_REPOSITORY/branches?per_page=100 \
)
if [[ $RESP_CODE != "200" ]]; then
echo "Failed to read branches - HTTP response code was $RESP_CODE"
exit 1
fi
if [[ $(cat __branches.json | jq "any(.[]; .name == \"$BRANCH\")") == "false" ]]; then
echo "Could not find branch $BRANCH"
exit 1
fi
- name: Find workflow
id: find-workflow
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
BRANCH: ${{ inputs.branch }}
run: |
FOUND=0
# Need to loop specifically in this order to find the right file, this is because:
# - gha-ci has both a ci.yml file (shared with other workflows) and an action-ci.yml file (the one we want to run)
# - gha-action-ci has both an action-ci.yml file (shared with other workflows) and an action-ci-self.yml file (the one we want to run)
FILENAMES='action-ci-self.yml action-ci.yml ci.yml'
for FILENAME in $FILENAMES; do
URL=https://raw.githubusercontent.com/$GITHUB_REPOSITORY/$BRANCH/.github/workflows/$FILENAME
RESP_CODE=$(curl -w %{http_code} -s -L -o /dev/null $URL)
if [[ $RESP_CODE == "200" ]]; then
FOUND=1
echo "FILENAME is $FILENAME"
echo "filename=$FILENAME" >> $GITHUB_OUTPUT
break
elif [[ $RESP_CODE != "404" ]]; then
echo "Failed to check file $FILENAME - HTTP response code was $RESP_CODE"
exit 1
fi
done
if [[ $FOUND == 0 ]]; then
echo "Could not workflow file for branch $BRANCH - tried $FILENAMES"
exit 1
fi
- name: Send API request
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
BRANCH: ${{ inputs.branch }}
FILENAME: ${{ steps.find-workflow.outputs.filename }}
run: |
# https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event
RESP_CODE=$(curl -w %{http_code} -s -L -o /dev/null \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$GITHUB_REPOSITORY/actions/workflows/$FILENAME/dispatches \
-d "{\"ref\":\"$BRANCH\"}"
)
if [[ $RESP_CODE != "204" ]]; then
echo "Failed to dispatch workflow - HTTP response code was $RESP_CODE"
exit 1
fi
- name: Delete temporary files
shell: bash
if: always()
run: |
if [[ -f __branches.json ]]; then
rm __branches.json
fi

0 comments on commit d347433

Please sign in to comment.