forked from 9sako6/imgcmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
170 lines (165 loc) · 7.55 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
name: "imgoptimizer"
description: "Optimize images in your repository. Supported image file formats: JPEG, PNG, GIF, SVG, WEBP"
author: "mthsmb"
branding:
icon: "image"
color: "blue"
inputs:
token:
description: "A Personal Access Token. See https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token"
required: true
paths-ignore-regexp:
description: "A regular expression for images' paths you don't want to compress."
required: false
default: ""
outputs:
pull-request-number:
description: "A pull request number created by imgoptimizer."
value: ${{ steps.set-outputs.outputs.pull-request-number }}
runs:
using: "composite"
steps:
- uses: denoland/setup-deno@v1.1.0
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true
with:
deno-version: "1.19.2"
- name: Install cwebp, gifsicle, jpegoptim, optipng, svgo
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true
shell: bash
run: |
sudo apt-get install -y webp gifsicle jpegoptim optipng
npm install -g svgo
cwebp -version
gifsicle --version
jpegoptim --version
optipng -v
svgo -v
- name: Branch information
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true
shell: bash
run: |
# NOTE: `head_ref` is available when an action is triggered by pull request event.
echo ::set-output name=base_branch::${{ github.head_ref || github.ref_name }}
id: branch
- uses: actions/checkout@v3
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true
with:
ref: ${{ steps.branch.outputs.base_branch }}
- name: Get actions information
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true
shell: bash
run: |
if [ '.' = $(basename ${{ github.action_path }}) ]; then
tag=${{ steps.branch.outputs.base_branch }}
else
tag=$(basename ${{ github.action_path }})
fi
echo ::set-output name=tag::${tag}
id: actions-info
- name: Git reset
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true
shell: bash
run: |
# NOTE: If the trigger event is 'pull_request', `git fetch` is necessary.
git fetch
git reset --hard origin/${{ steps.branch.outputs.base_branch }}
- name: Export images' statistics in json before optimization
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true
shell: bash
run: |
deno run \
--allow-read \
--allow-write \
https://raw.githubusercontent.com/mthsmb/imgoptimizer/${{ steps.actions-info.outputs.tag }}/src/export_stat.ts \
'${{ inputs.paths-ignore-regexp }}' \
'/tmp/imgoptimizer-stat.json'
- name: Optimize images
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true
shell: bash
run: |
deno run \
--allow-read \
--allow-run \
https://raw.githubusercontent.com/mthsmb/imgoptimizer/${{ steps.actions-info.outputs.tag }}/src/optimize.ts \
'${{ inputs.paths-ignore-regexp }}'
- name: Export images' statistics in json after optimization
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true
shell: bash
run: |
deno run \
--allow-read \
--allow-write \
https://raw.githubusercontent.com/mthsmb/imgoptimizer/${{ steps.actions-info.outputs.tag }}/src/export_stat.ts \
'${{ inputs.paths-ignore-regexp }}' \
'/tmp/imgoptimizer-stat.json'
- name: Export images' statistics in json after optimization
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true
shell: bash
run: |
message=$(deno run \
--allow-read \
https://raw.githubusercontent.com/mthsmb/imgoptimizer/${{ steps.actions-info.outputs.tag }}/src/show_stat_message.ts \
'/tmp/imgoptimizer-stat.json'
)
title=$(deno run \
--allow-read \
https://raw.githubusercontent.com/mthsmb/imgoptimizer/${{ steps.actions-info.outputs.tag }}/src/show_stat_title.ts \
'/tmp/imgoptimizer-stat.json'
)
echo ::set-output name=message::${message}
echo ::set-output name=title::${title}
id: template
- name: Check if images were compressed
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true && steps.template.outputs.message != ''
shell: bash
run: |
if [ -z "$(git diff --name-only)" ]; then
echo 'There was no change in all the images.'
echo "::set-output name=no_change::true"
else
echo "::group::git diff"
git diff --name-only
echo "::endgroup::"
fi
id: diff
- name: Commit images
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true && steps.template.outputs.message != ''
shell: bash
run: |
if [ -n "${{ steps.diff.outputs.no_change }}" ]; then
echo 'This step is skipped.'
exit 0
fi
git config --local user.name "GitHub"
git config --local user.email "noreply@github.com"
git add .
git commit -m "Optimize images by imgoptimizer action"
sha=$(git rev-parse --short HEAD)
new_branch="actions/imgoptimizer/${sha}"
echo ::set-output name=sha::${sha}
echo ::set-output name=new_branch::${new_branch}
id: commit
- name: Create a pull request
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true && steps.template.outputs.message != ''
uses: peter-evans/create-pull-request@v3.14.0
with:
token: ${{ inputs.token }}
committer: GitHub <noreply@github.com>
author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
branch: ${{ steps.commit.outputs.new_branch }}
base: ${{ steps.branch.outputs.base_branch }}
title: ${{ steps.template.outputs.title }}
body: ${{ steps.template.outputs.message }}
draft: false
delete-branch: true
- name: Set outputs
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true && steps.template.outputs.message != ''
shell: bash
id: set-outputs
run: |
echo ::set-output name=pull-request-number::$PULL_REQUEST_NUMBER
- name: Check outputs
if: startsWith(github.head_ref, 'actions/imgoptimizer/') != true && startsWith(github.ref_name, 'actions/imgoptimizer/') != true && steps.template.outputs.message != ''
shell: bash
run: |
echo "pull-request-number: ${{ steps.set-outputs.outputs.pull-request-number }}"