-
Notifications
You must be signed in to change notification settings - Fork 4
80 lines (69 loc) · 2.95 KB
/
doc-ci.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
name: doc-sidebar
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [main]
paths:
- "docs/_sidebar.md"
pull_request:
branches: [main]
paths:
- "docs/_sidebar.md"
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
doc-update:
# The type of runner that the job will run on
runs-on: ubuntu-latest
defaults:
run:
shell: bash
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# If this is pull request, check out the head of PR branch so we can make changes to it
- name: Checkout Main
if: ${{ github.event_name == 'push' }}
uses: actions/checkout@v3
with:
token: ${{ secrets.GH_TOKEN }}
- name: Checkout PR Branch
if: ${{ github.event_name == 'pull_request' }}
uses: actions/checkout@v3
with:
token: ${{ secrets.GH_TOKEN }}
ref: ${{ github.head_ref }}
- name: setup git config
run: |
# setup the username and email. I tend to use 'GitHub Actions Bot' with no email by default
git config user.name "terraform-ibm-modules-ops"
git config user.email "[email protected]"
git config push.default current
- name: Make Changes to README
run: |
# make temporary copy of sidebar
cp docs/_sidebar.md /tmp/_sidebar.md
# add newline to end of file if needed
if ! [[ $(tail -c1 "/tmp/_sidebar.md" | wc -l) -gt 0 ]]
then
echo "" >> /tmp/_sidebar.md
fi
# prepend URLs with base path
# sed logic:
# general format: s/selector/replacement/g - "g" will do global replace
# selectors separated with parends so that they can be referenced later in replacement
# selector 1 format: "[anything](" - finds beginning of markdown URL format
# selector 2 format: "anything.md)" - the back end of the URL format
# replacement format: selector1 + new url domain + selector 2
sed -i 's/\(\[.*\](\)\(.*\.md\)/\1https:\/\/terraform-ibm-modules.github.io\/documentation\/#\/\2/g' /tmp/_sidebar.md
# substitute the temp sidebar content in between markers in readme
perl -i -ne 'if (/BEGIN TOC/../END TOC/) { print $_ if /BEGIN TOC/; print "I_WANT_TO_BE_REPLACED\n$_" if /END TOC/;} else { print $_ }' "README.md"
perl -i -e 'open(F, "'"/tmp/_sidebar.md"'"); $f = join "", <F>; while(<>){if (/I_WANT_TO_BE_REPLACED/) {print $f} else {print $_};}' "README.md"
- name: commit
run: |
# Stage the file, commit and push only if changed
if ! git diff --exit-code; then
git add README.md
git commit -m "docs: README ToC updated from sidebar [skip ci]"
git push
fi