Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
feat: generalise copy-workflow to cover update-go and delete-template…
Browse files Browse the repository at this point in the history
…s too
  • Loading branch information
galargh committed Jul 27, 2023
1 parent f5bf45e commit d6b8808
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 174 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/copy-templates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Copy Templates

on:
workflow_dispatch:
inputs:
targets:
description: "List of repository names to deploy to"
required: true

jobs:
copy:
uses: protocol/.github/workflows/process.yml@v1.0.0
with:
targets: ${{ github.event.inputs.targets }}
branch: web3-bot/copy-templates
script: copy-templates.sh
defaults: |
{
"Go": {
"files": [
".github/workflows/go-test.yml",
".github/workflows/go-check.yml",
".github/workflows/releaser.yml",
".github/workflows/release-check.yml",
".github/workflows/tagpush.yml",
"version.json"
]
},
"JavaScript": {
"files": [
".github/workflows/js-test-and-release.yml"
]
},
"TypeScript": {
"files": [
".github/workflows/js-test-and-release.yml"
]
}
}
5 changes: 2 additions & 3 deletions .github/workflows/create-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ on:
inputs:
branch:
description: "Branch from which to merge PRs"
required: false
default: 'web3-bot/sync'
required: true
schedule:
- cron: "0 0 * * *" # https://crontab.guru/every-day

Expand All @@ -17,7 +16,7 @@ jobs:
steps:
- name: Create PRs
env:
BRANCH: ${{ github.event.inputs.branch || 'web3-bot/sync' }}
BRANCH: ${{ github.event.inputs.branch || 'web3-bot/copy-templates' }}
uses: actions/github-script@v6
with:
github-token: ${{ secrets.WEB3_BOT_GITHUB_TOKEN }}
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/delete-templates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Delete Templates

on:
workflow_dispatch:
inputs:
targets:
description: "List of repository names to deploy to"
required: true
files:
description: "List of files to delete"
required: true

jobs:
copy:
uses: protocol/.github/workflows/process.yml@v1.0.0
with:
targets: ${{ github.event.inputs.targets }}
branch: web3-bot/delete-templates
script: delete-templates.sh
override: |
{
"files": ${{ github.event.inputs.files }}
}
106 changes: 0 additions & 106 deletions .github/workflows/delete-workflow.yml

This file was deleted.

34 changes: 14 additions & 20 deletions .github/workflows/dispatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ name: Dispatch

on:
workflow_dispatch:
override:
description: "Defaults override to use for each target"
required: false
default: '{}'
branch:
description: "Branch to deploy to in each target"
required: false
default: 'web3-bot/sync'
script:
description: "Script to run in each target"
required: false
default: 'copy-templates.sh'
inputs:
workflow:
description: "Workflow to dispatch"
required: true
inputs:
description: "Workflow inputs"
required: false
default: '{}'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down Expand Up @@ -119,19 +115,17 @@ jobs:
max-parallel: 1
env:
GITHUB_TOKEN: ${{ secrets.WEB3_BOT_GITHUB_TOKEN }}
WORKFLOW_YML: copy-workflow.yml
WORKFLOW_REPO: protocol/.github
WORKFLOW: ${{ github.event.inputs.workflow }}
steps:
- id: dispatch
name: Dispatch copy workflow
env:
TARGETS: ${{ toJSON(matrix.cfg.value) }}
OVERRIDE: ${{ github.event.inputs.override }}
BRANCH: ${{ github.event.inputs.branch }}
SCRIPT: ${{ github.event.inputs.script }}
INPUTS: ${{ github.event.inputs.inputs }}
run: |
start_date="$(date +%s)"
gh workflow run "$WORKFLOW_YML" --ref "$GITHUB_REF" --repo "$WORKFLOW_REPO" --field "targets=$TARGETS" --field "override=$OVERRIDE" --field "branch=$BRANCH" --field "script=$SCRIPT
args="$(jq -r 'to_entries | map("--field \(.key)=\(.value|tostring)") | join(" ")' <<< "$INPUTS")"
gh workflow run "$WORKFLOW" --ref "$GITHUB_REF" --repo "protocol/.github" --field "targets=$TARGETS" $args
echo "start_date=$start_date" >> $GITHUB_OUTPUT
- id: run
name: Wait for copy workflow run to start
Expand All @@ -140,7 +134,7 @@ jobs:
run: |
# checks every 3 seconds until the most recent copy workflow run's created_at is later than this job's start_date
while sleep 3; do
run="$(gh api "/repos/$WORKFLOW_REPO/actions/workflows/$WORKFLOW_YML/runs?per_page=1" --jq '.workflow_runs[0]')"
run="$(gh api "/repos/protocol/.github/actions/workflows/$WORKFLOW/runs?per_page=1" --jq '.workflow_runs[0]')"
# nothing to check if no copy workflow run was returned
if [[ ! -z "$run" ]]; then
run_start_date="$(date --date="$(jq -r '.created_at' <<< "$run")" +%s)"
Expand All @@ -159,4 +153,4 @@ jobs:
# checks every 3 seconds until the copy workflow run's status is completed
# redirects the stdout to /dev/null because it is very chatty
gh run watch "$RUN_ID" --repo "$WORKFLOW_REPO" > /dev/null
gh run watch "$RUN_ID" --repo "protocol/.github" > /dev/null
5 changes: 2 additions & 3 deletions .github/workflows/merge-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ on:
inputs:
branch:
description: "Branch from which to merge PRs"
required: false
default: 'web3-bot/sync'
required: true
schedule:
- cron: "0 0 * * *" # https://crontab.guru/every-day

Expand All @@ -17,7 +16,7 @@ jobs:
steps:
- name: Merge PRs
env:
QUERY: is:pr author:web3-bot state:open head:${{ github.event.inputs.branch || 'web3-bot/sync' }} archived:false
QUERY: is:pr author:web3-bot state:open head:${{ github.event.inputs.branch || 'web3-bot/copy-templates' }} archived:false
uses: actions/github-script@v6
with:
github-token: ${{ secrets.WEB3_BOT_GITHUB_TOKEN }}
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/update-go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Update Go

on:
workflow_dispatch:
inputs:
targets:
description: "List of repository names to deploy to"
required: true
go:
description: "Go version to use"
required: true

jobs:
copy:
uses: protocol/.github/workflows/process.yml@v1.0.0
with:
targets: ${{ github.event.inputs.targets }}
branch: web3-bot/update-go
script: copy-templates.sh
defaults: |
{
"Go": {
"version": "${{ github.event.inputs.go }}"
}
}
23 changes: 23 additions & 0 deletions scripts/delete-templates.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

set -euo pipefail -o nounset

for f in $(jq -r '.config.files[]' <<< "$CONTEXT"); do
echo -e "\nProcessing $f."
if [[ ! -f "$REPO/$f" ]]; then
echo "$f does not exist. Skipping.\n"
continue
fi

rm -rf "$REPO/$f"
done

pushd $REPO > /dev/null

git add .

if ! git diff-index --quiet HEAD; then
git commit -m "chore: delete templates"
fi

popd > /dev/null
Loading

0 comments on commit d6b8808

Please sign in to comment.