Skip to content

Commit

Permalink
Merge pull request #1 from emteknetnz/pulls/main/dispatch-ci
Browse files Browse the repository at this point in the history
NEW Create dispatch-ci
  • Loading branch information
GuySartorelli authored Mar 20, 2023
2 parents 489291a + 2ccbc49 commit 49acaec
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
# gha-dispatch-ci

Used to trigger CI workflows on a schedule. This workflow allows multiple branches to have CI workflows run on a schedule, rather than just the default branch.

For modules that are to be scheduled once per week, the cron must be run two times per week on consecutive days. This is done because there is logic in action.yml to run one job on the CMS 4 branches on "even" days of the week and CMS 5 branches on "odd" days of the week.

For builds that are to be scheduled once per day, specifically `silverstripe/installer` and `silverstripe/recipe-kitchen-sink`, the cron must be run two times per day on consecutive hours, because for these modules CMS 4 branches are run on "even" hours and CMS 5 branches are run on "odd" hours.

### Usage

**.github/workflows/dispatch-ci.yml**
```yml
name: Dispatch CI

on:
# Every Tuesday,Wednesday at 1:00pm UTC
schedule:
- cron: '0 13 * * 2,3'

jobs:
dispatch-ci:
name: Dispatch CI
# 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: Dispatch CI
uses: silverstripe/gha-dispatch-ci@v1
```
105 changes: 105 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
name: Dispatch CI
description: GitHub Action to trigger ci.yml workflow_dispatch event via GitHub API

runs:
using: composite
steps:

- name: Get type
id: gettype
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
# repository that has a twice-weekly schedule run on consequtive days - use day of week
date_part=$(date +%u)
type=""
if [[ $GITHUB_REPOSITORY == "silverstripe/installer" ]] || \
[[ $GITHUB_REPOSITORY == "silverstripe/recipe-kitchen-sink" ]]
then
# respository that has daily schedule - use hour
date_part=$(date +%H)
fi
if (( $date_part % 2 == 1 )); then
# latest major, next-minor e.g. "5"
type=latest,next-minor
else
# previous major, next-patch e.g. "4.13"
type=previous,next-patch
fi
echo "type=$type" >> $GITHUB_OUTPUT
- name: Get branch
id: getbranch
shell: bash
env:
TYPE: ${{ steps.gettype.outputs.type }}
run: |
major_type=$(echo $TYPE | cut -d "," -f 1)
minor_type=$(echo $TYPE | cut -d "," -f 2)
# https://docs.github.com/en/rest/branches/branches#list-branches
RESP_CODE=$(curl -w %{http_code} -s -L -o __branches.json \
-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 \
)
if [[ $RESP_CODE != "200" ]]; then
echo "Failed to list branches - HTTP response code was $RESP_CODE"
exit 1
fi
major_branch=""
minor_branch=""
major_branches=$(jq -r '.[] | select(.name | test("^[0-9]+$")) | .name' __branches.json | sort -V -r)
# "major_branches" needs to be quoted to preserve line-breaks
major_branches_count=$(echo "$major_branches" | wc -l)
if [[ $major_type == "latest" ]]; then
if (( $major_branches_count < 1 )); then
echo "No major branches found"
exit 1
fi
major_branch=$(echo "$major_branches" | head -1)
elif [[ $major_type == "previous" ]]; then
if (( $major_branches_count < 2 )); then
echo "Only one major branch found"
exit 1
fi
major_branch=$(echo "$major_branches" | head -2 | tail -1)
fi
if [[ $minor_type == "next-patch" ]]; then
# not escaping rx . as \. because jq will say it is a compile error
minor_branches=$(jq -r ".[] | select(.name | test(\"^$major_branch.[0-9]+$\")) | .name" __branches.json | sort -V -r)
minor_branches_count=$(echo "$minor_branches" | wc -l)
if (( $minor_branches_count < 1 )); then
echo "No minor branches found"
exit 1
fi
minor_branch=$(echo "$minor_branches" | head -1)
fi
branch=$major_branch
if [[ $minor_branch != "" ]]; then
branch="$minor_branch"
fi
rm __branches.json
echo "branch is $branch"
echo "branch=$branch" >> $GITHUB_OUTPUT
- name: Send API request
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
REF: ${{ steps.getbranch.outputs.branch }}
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/ci.yml/dispatches \
-d "{\"ref\":\"$REF\"}"
)
if [[ $RESP_CODE != "204" ]]; then
echo "Failed to dispatch workflow - HTTP response code was $RESP_CODE"
exit 1
fi

0 comments on commit 49acaec

Please sign in to comment.