forked from ipdxco/unified-github-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
179 lines (177 loc) · 6.67 KB
/
process.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
name: Process
on:
workflow_call:
inputs:
targets:
required: true
type: string
branch:
required: true
type: string
script:
required: true
type: string
defaults:
required: false
default: '{}'
type: string
override:
required: false
default: '{}'
type: string
dry-run:
required: false
default: false
type: boolean
reset:
required: false
default: true
type: boolean
rebase:
required: false
default: true
type: boolean
secrets:
UCI_GITHUB_TOKEN:
required: true
jobs:
copy:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
repository: ${{ fromJSON(inputs.targets) }}
max-parallel: 10
name: ${{ matrix.repository }}
steps:
- name: Show inputs
env:
TARGETS: ${{ inputs.targets }}
BRANCH: ${{ inputs.branch }}
SCRIPT: ${{ inputs.script }}
DEFAULTS: ${{ inputs.defaults }}
OVERRIDE: ${{ inputs.override }}
run: |
echo "TARGETS=$TARGETS"
echo "BRANCH=$BRANCH"
echo "SCRIPT=$SCRIPT"
echo "DEFAULTS=$DEFAULTS"
echo "OVERRIDE=$OVERRIDE"
- name: Install dependencies
run: |
pip install yq
- name: Checkout ${{ matrix.repository }}
uses: actions/checkout@v4
with:
path: ${{ matrix.repository }}
repository: ${{ matrix.repository }}
token: ${{ secrets.UCI_GITHUB_TOKEN }}
persist-credentials: true
submodules: recursive
fetch-depth: 0
- name: Checkout ${{ github.repository }}
uses: actions/checkout@v4
with:
path: ${{ github.repository }}
ref: ${{ github.ref }}
fetch-depth: 0
- id: github
name: Get ${{ matrix.repository }} GitHub info
working-directory: ${{ matrix.repository }}
env:
REPO: ${{ matrix.repository }}
GITHUB_TOKEN: ${{ secrets.UCI_GITHUB_TOKEN }}
run: |
eof="EOF$RANDOM"
default_branch="$(git remote show origin | awk '/HEAD branch/ {print $NF}')"
protected_branches="$(gh api -X GET "/repos/$REPO/branches" -f protected=true | jq -c 'map(.name)')"
languages="$(gh api -X GET "/repos/$REPO/languages" | jq -c 'to_entries | sort_by(.value) | reverse | map(.key)')"
tag="$(git tag --points-at $(git describe --tags --abbrev=0 || true) | sort -V | tail -n-1)"
echo "json<<$eof" >> $GITHUB_OUTPUT
echo '{' >> $GITHUB_OUTPUT
echo '"default_branch":"'"$default_branch"'",' >> $GITHUB_OUTPUT
echo '"protected_branches":'"$protected_branches"',' >> $GITHUB_OUTPUT
echo '"languages":'"$languages"',' >> $GITHUB_OUTPUT
echo '"tag":"'"$tag"'"' >> $GITHUB_OUTPUT
echo '}' >> $GITHUB_OUTPUT
echo "$eof" >> $GITHUB_OUTPUT
- id: source
name: Get ${{ github.repository }} GitHub info
working-directory: ${{ github.repository }}
run: |
eof="EOF$RANDOM"
tag="$(git tag --points-at $(git describe --tags --abbrev=0 || true) | sort -V | tail -n-1)"
echo "json<<$eof" >> $GITHUB_OUTPUT
echo '{' >> $GITHUB_OUTPUT
echo '"tag":"'"$tag"'"' >> $GITHUB_OUTPUT
echo '}' >> $GITHUB_OUTPUT
echo "$eof" >> $GITHUB_OUTPUT
- id: config
name: Get ${{ matrix.repository }} config
working-directory: ${{ matrix.repository }}
env:
REPO: ${{ matrix.repository }}
DEFAULTS: ${{ inputs.defaults }}
OVERRIDE: ${{ inputs.override }}
LANGUAGE: ${{ fromJSON(steps.github.outputs.json).languages[0] }}
run: |
defaults="$(yq "del(.. | nulls) | .language[env.LANGUAGE] // {} + .common // {}" <<< "$DEFAULTS")"
override="$(yq "del(.. | nulls) | .language[env.LANGUAGE] // {} + .common // {}" <<< "$OVERRIDE")"
if [[ -f .github/uci.yml ]]; then
echo "json=$(yq -c --argjson defaults "$defaults" --argjson override "$override" '$defaults + . + $override' .github/uci.yml)" >> $GITHUB_OUTPUT
else
echo "json=$(jq -c --argjson defaults "$defaults" --argjson override "$override" -n '$defaults + $override')" >> $GITHUB_OUTPUT
fi
- id: before
name: Prepare ${{ matrix.repository }} for deployment
working-directory: ${{ matrix.repository }}
env:
BRANCH: ${{ inputs.branch }}
DEFAULT_BRANCH: ${{ fromJSON(steps.github.outputs.json).default_branch }}
RESET: ${{ inputs.reset }}
REBASE: ${{ inputs.rebase }}
# If the branch already exists, check it out; otherwise, create it.
# Then, try rebasing the uci/* branch onto the default branch.
# If that fails, reset the uci/* branch to the default branch.
run: |
git config user.name web3-bot
git config user.email [email protected]
git checkout "$BRANCH" || git checkout -B "$BRANCH"
# Registering SHA before rebase to push even if only the default branch moved
git rev-parse HEAD | xargs -I{} echo "sha={}" | tee -a $GITHUB_OUTPUT
if [[ "$REBASE" == 'true' && "$RESET" == 'true' ]]; then
git rebase "$DEFAULT_BRANCH" || (git rebase --abort && git reset --hard "$DEFAULT_BRANCH")
elif [[ "$REBASE" == 'true' ]]; then
git rebase "$DEFAULT_BRANCH" || git rebase --abort
elif [[ "$RESET" == 'true' ]]; then
git reset --hard "$DEFAULT_BRANCH"
fi
- name: Configure global git user
run: |
git config --global user.name web3-bot
git config --global user.email [email protected]
- env:
SOURCE: ${{ github.repository }}
TARGET: ${{ matrix.repository }}
CONTEXT: |
{
"config": ${{ toJSON(fromJSON(steps.config.outputs.json)) }},
"github": ${{ toJSON(fromJSON(steps.github.outputs.json)) }},
"source": ${{ toJSON(fromJSON(steps.source.outputs.json)) }}
}
SCRIPT: ${{ inputs.script }}
GITHUB_TOKEN: ${{ secrets.UCI_GITHUB_TOKEN }}
run: if [[ -f "$SOURCE/scripts/$SCRIPT" ]]; then $SOURCE/scripts/$SCRIPT; else $SCRIPT; fi
- id: after
name: Check if push is needed
working-directory: ${{ matrix.repository }}
env:
BRANCH: ${{ inputs.branch }}
DEFAULT_BRANCH: ${{ fromJSON(steps.github.outputs.json).default_branch }}
run: git rev-parse HEAD | xargs -I{} echo "sha={}" | tee -a $GITHUB_OUTPUT
- name: Push changes
if: steps.after.outputs.sha != steps.before.outputs.sha && !inputs.dry-run
working-directory: ${{ matrix.repository }}
env:
BRANCH: ${{ inputs.branch }}
run: git push origin "$BRANCH" -f