From f67108b724cc0de56edb3ae49c568cf72c9b90fd Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 17 Aug 2023 15:17:59 +1200 Subject: [PATCH] NEW Create gha-trigger-ci --- .github/LICENSE | 29 ++++++++++ .github/workflows/action-ci.yml | 11 ++++ .github/workflows/auto-tag.yml | 12 +++++ .github/workflows/keepalive.yml | 17 ++++++ .github/workflows/merge-up.yml | 17 ++++++ README.md | 35 +++++++++++- action.yml | 95 +++++++++++++++++++++++++++++++++ 7 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 .github/LICENSE create mode 100644 .github/workflows/action-ci.yml create mode 100644 .github/workflows/auto-tag.yml create mode 100644 .github/workflows/keepalive.yml create mode 100644 .github/workflows/merge-up.yml create mode 100644 action.yml diff --git a/.github/LICENSE b/.github/LICENSE new file mode 100644 index 0000000..c36440d --- /dev/null +++ b/.github/LICENSE @@ -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. diff --git a/.github/workflows/action-ci.yml b/.github/workflows/action-ci.yml new file mode 100644 index 0000000..3fd70a4 --- /dev/null +++ b/.github/workflows/action-ci.yml @@ -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 diff --git a/.github/workflows/auto-tag.yml b/.github/workflows/auto-tag.yml new file mode 100644 index 0000000..17712c8 --- /dev/null +++ b/.github/workflows/auto-tag.yml @@ -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 diff --git a/.github/workflows/keepalive.yml b/.github/workflows/keepalive.yml new file mode 100644 index 0000000..196ab39 --- /dev/null +++ b/.github/workflows/keepalive.yml @@ -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 diff --git a/.github/workflows/merge-up.yml b/.github/workflows/merge-up.yml new file mode 100644 index 0000000..9937444 --- /dev/null +++ b/.github/workflows/merge-up.yml @@ -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 diff --git a/README.md b/README.md index ffa459a..3313ffd 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..2581cab --- /dev/null +++ b/action.yml @@ -0,0 +1,95 @@ +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 + 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