This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
151 lines (148 loc) · 5.52 KB
/
copy-workflow.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# This workflow is triggered by the dispatch workflow.
name: Deploy
on:
workflow_dispatch:
inputs:
targets:
description: "List of repositories to deploy to"
required: true
jobs:
copy:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
cfg: ${{ fromJSON(github.event.inputs.targets) }}
max-parallel: 10
env:
TARGET_REPO_DIR: "target-repo"
TEMPLATE_REPO_DIR: "template-repo"
TEMPLATE_DIR: "templates"
NEEDS_UPDATE: 0
INITIAL_WORKFLOW_DEPLOYMENT: 0
GO_VERSION_BUMP: 0
FILES: ""
name: ${{ matrix.cfg.target }}
steps:
- name: Checkout ${{ matrix.cfg.target }}
uses: actions/checkout@v3
with:
path: ${{ env.TARGET_REPO_DIR }}
repository: ${{ matrix.cfg.target }}
token: ${{ secrets.WEB3_BOT_GITHUB_TOKEN }}
persist-credentials: true
submodules: recursive
- name: Checkout template repository
uses: actions/checkout@v3
with:
path: ${{ env.TEMPLATE_REPO_DIR }}
ref: ${{ matrix.cfg.source_ref || github.ref }}
- name: determine GitHub default branch
working-directory: ${{ env.TARGET_REPO_DIR }}
run: |
default_branch=$(git remote show origin | awk '/HEAD branch/ {print $NF}')
echo "DEFAULT_BRANCH=$default_branch" >> $GITHUB_ENV
- name: git config
working-directory: ${{ env.TARGET_REPO_DIR }}
run: |
git config user.name web3-bot
git config user.email web3-bot@users.noreply.github.com
- name: determine files to add
# By setting the environment variable, it's possible to programmatically add / modify this list.
# See https://github.com/protocol/.github/blob/38135c75e47839623bf9b2748275d8c6167a8fa8/.github/workflows/copy-workflow.yml#L163-L168 for an example, how we used to make use of this.
env:
FILES: |
${{ toJSON(matrix.cfg.files) }}
${{ toJSON(matrix.cfg.extra_files) }}
run: echo "FILES=$(jq -nc '[inputs] | add' <<< "$FILES")" >> $GITHUB_ENV
- name: is initial workflow deployment
# INITIAL_WORKFLOW_DEPLOYMENT=1 iff none of the files in the target repository exist yet
run: |
initial_workflow_deployment=1
for f in $(jq -r '.[]' <<< "$FILES"); do
if [[ -f $TARGET_REPO_DIR/$f ]]; then
initial_workflow_deployment=0
break
fi
done
echo "INITIAL_WORKFLOW_DEPLOYMENT=$initial_workflow_deployment" >> $GITHUB_ENV
- name: remove Travis (on initial workflow deployment)
if: env.INITIAL_WORKFLOW_DEPLOYMENT == 1
working-directory: ${{ env.TARGET_REPO_DIR }}
run: |
if [[ -f .travis.yml ]]; then
git rm .travis.yml
git commit -m "disable Travis"
fi
- name: remove CircleCI (on initial workflow deployment)
if: env.INITIAL_WORKFLOW_DEPLOYMENT == 1
working-directory: ${{ env.TARGET_REPO_DIR }}
run: |
if [[ -d .circleci ]]; then
git rm -r .circleci
git commit -m "disable CircleCI"
fi
- name: remove gx (on initial workflow deployment)
if: env.INITIAL_WORKFLOW_DEPLOYMENT == 1
working-directory: ${{ env.TARGET_REPO_DIR }}
run: |
if [[ -d .gx ]]; then
git rm -r .gx
git commit -m "remove .gx"
fi
- name: Run steps specific to go
if: matrix.cfg.deploy_go
# use of ${{ env.TEMPATE_REPO_DIR }} is not allowed here
uses: ./template-repo/.github/actions/copy-workflow-go
- name: Run steps specific to versioning
if: matrix.cfg.deploy_versioning
uses: ./template-repo/.github/actions/copy-workflow-versioning
- name: Add files
env:
CONTEXT: |
{
"config": ${{ toJSON(matrix.cfg) }},
"github": {
"default_branch": "${{ env.DEFAULT_BRANCH }}"
}
}
TEMPLATE_ENGINE: s#\$\{\{\{\s*(.*?)\s*\}\}\}#`jq -cjn 'env.CONTEXT | fromjson.$1'`#ge
run: |
for f in $(jq -r '.[]' <<< "$FILES"); do
echo -e "\nProcessing $f."
# add DO NOT EDIT header
tmp=$(mktemp)
cat $TEMPLATE_REPO_DIR/$TEMPLATE_DIR/header.yml $TEMPLATE_REPO_DIR/$TEMPLATE_DIR/$f > $tmp
# expand the template with values from the JSON context object
perl -pi -e "$TEMPLATE_ENGINE" $tmp
# create commit, if necessary
commit_msg=""
if [[ ! -f "$TARGET_REPO_DIR/$f" ]]; then
echo "First deployment.\n"
commit_msg="add $f"
else
status=$(cmp --silent $TARGET_REPO_DIR/$f $tmp; echo $?)
if [[ $status -ne 0 ]]; then
echo "Update needed."
commit_msg="update $f"
else
echo "File identical. Skipping."
continue
fi
fi
mkdir -p "$TARGET_REPO_DIR/$(dirname $f)"
mv $tmp $TARGET_REPO_DIR/$f
pushd $TARGET_REPO_DIR > /dev/null
git add $f
git commit -m "$commit_msg"
popd > /dev/null
done
- name: Check if we need to update the branch
working-directory: ${{ env.TARGET_REPO_DIR }}
run: echo "NEEDS_UPDATE=$(git rev-list HEAD...origin/$(git rev-parse --abbrev-ref HEAD) --ignore-submodules --count 2> /dev/null || echo 1)" >> $GITHUB_ENV
- name: Force push web3-bot/sync branch
if: env.NEEDS_UPDATE != 0
working-directory: ${{ env.TARGET_REPO_DIR }}
run: |
git checkout -B web3-bot/sync
git push origin web3-bot/sync -f