-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.yml
152 lines (134 loc) · 4.86 KB
/
action.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
name: GitHub Pages URL Shortener Action
author: Pierre Nicolas Durette
description: Ultra-lightweight GitHub Pages URL Shortener
branding:
color: purple
icon: link
inputs:
urls_config:
description: |
The path to a YAML file associating redirect keys to URLs, e.g.:
```yaml
---
test1: https://www.bookcity.ca/
test2: https://www.gladdaybookshop.com
```
required: false
default: .github/urls.yml
default_redirect:
description: |
Default behaviour for `/` or any 404, can be either:
* a URL (absolute) to redirect to
(e.g. `https://www.aol.com/`)
* a URL (relative) to redirect to from the domain
(e.g. `/blog`)
* a message to display
(e.g. `Nothing here!`)
required: false
default: "Nothing here!"
runs:
using: "composite"
steps:
# Generate Jekyll Configuration
# * Copy over the Jekyll _config.yml
# (https://jekyllrb.com/docs/configuration/)
# * Create the _redirects/ collection directory
# (https://jekyllrb.com/docs/collections/)
- shell: bash
run: |
# Generate Jekyll Configuration
cp ${GITHUB_ACTION_PATH}/.github/_config.yml .
mkdir _redirects
# Generate Jekyll Documents
# * Each <key>:<url> entry is generated as a Markdown document
# in the 'redirects' (i.e. in _redirects/) Jekyll collection
# * Generate the front matter (https://jekyllrb.com/docs/front-matter/)
# using the 'redirect_to' key provided by the jekyll-redirect-from
# Jekyll plugin (https://github.com/jekyll/jekyll-redirect-from)
- shell: ruby {0}
env:
URLS_CONFIG: ${{ inputs.urls_config }}
run: |
# Generate Jekyll Documents
require 'fileutils'
require 'yaml'
begin
urls = YAML.load_file(ENV['URLS_CONFIG'])
urls.each do |key, url|
puts "key: #{key}, url: #{url}"
file_path = "_redirects/#{key}.md"
FileUtils.mkdir_p(File.dirname(file_path))
File.open(file_path, 'w') do |doc|
doc << "---\n"
doc << "redirect_to: #{url}\n"
doc << "---\n"
end
end
rescue => e
puts "::warning::#{ENV['URLS_CONFIG']} doesn't exist or is invalid"
end
# Generate Jekyll Default (Redirect URL)
# * If <default_redirect> starts with 'http', consider it a URL
# * Generate an index Markdown redirect (like above)
# * Generate a 404 Markdown redirect (like above)
# Sets permalink key to 404.html as per GitHub docs
- shell: bash
id: url_redirect
if: |
startsWith(inputs.default_redirect, 'http') ||
startsWith(inputs.default_redirect, '/')
env:
REDIRECT_URL: ${{ inputs.default_redirect }}
run: |
# Generate Jekyll Default (Redirect URL)
echo "::notice::Index/404 will redirect to ${REDIRECT_URL}"
echo "---" > index.md
echo "redirect_to: ${REDIRECT_URL}" >> index.md
echo "---" >> index.md
echo "---" > 404.md
echo "permalink: /404.html" >> 404.md
echo "redirect_to: ${REDIRECT_URL}" >> 404.md
echo "---" >> 404.md
# Generate Jekyll Default (Message)
# * If <default_redirect> does not starts with 'http', consider it a message
# * Generate a plain index.html with the message
# * Generate a plain 404.html with the message
- shell: bash
if: steps.url_redirect.conclusion == 'skipped'
env:
MESSAGE: ${{ inputs.default_redirect }}
run: |
# Generate Jekyll Default (Message)
echo "::notice::Index/404 will display '${MESSAGE}'"
echo "${MESSAGE}" > index.html
echo "${MESSAGE}" > 404.html
# Build with Jekyll
# GitHub's official Jekyll builder for GitHub Pages
# Requires write permissions for id-token, pages
- uses: actions/jekyll-build-pages@v1
with:
source: ./
destination: ./_site
# Show Generated URLs
# * Builds a Markdown redirects table as job summary
# Uses the 'redirects.json' generated by the
# jekyll-redirect-from Jekyll plugin (in _site/)
- shell: ruby {0}
run: |
# Show Generated URLs
require 'json'
generated = JSON.load(File.read('_site/redirects.json'))
File.open(ENV['GITHUB_STEP_SUMMARY'], 'a') do |summary|
summary << "# Generated URLs\n"
summary << "|Key|URL|\n"
summary << "|---|---|\n"
generated.each do |key, url|
summary << "|`#{key}`|#{url}|\n"
end
end
# Notes:
# * Each step starts with a comment ('#') to give context
# to the run as GitHub Actions will display the 1st line
# of a step as the 'step name' for composite Actions
# * Ruby is used because it is always pre-installed and
# includes both YAML and JSON support in its standard library