forked from 9sako6/imgcmp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
173 lines (168 loc) · 6.33 KB
/
action.yml
File metadata and controls
173 lines (168 loc) · 6.33 KB
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
name: "imgcmp"
description: "Optimize images in your repository. Supported image file formats: JPEG, PNG, GIF, SVG, WEBP"
author: "9sako6"
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 imgcmp."
value: ${{ steps.set-outputs.outputs.pull-request-number }}
runs:
using: "composite"
steps:
- name: Get branch name
uses: tj-actions/branch-names@v6
id: branch-name
- name: Check if the action should be run
shell: bash
run: |
echo "run=${{ steps.branch-name.outputs.is_tag != 'true' && startsWith(github.head_ref, 'actions/imgcmp/') != true && startsWith(github.ref_name, 'actions/imgcmp/') != true }}" >> $GITHUB_OUTPUT
id: check
- uses: denoland/setup-deno@v1.1.0
if: steps.check.outputs.run == 'true'
with:
deno-version: "1.19.2"
- name: Install cwebp, gifsicle, jpegoptim, optipng, svgo
if: steps.check.outputs.run == '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
- uses: actions/checkout@v3
if: steps.check.outputs.run == 'true'
with:
ref: ${{ steps.branch-name.outputs.current_branch }}
- name: Get actions information
if: steps.check.outputs.run == 'true'
shell: bash
run: |
if [ '.' = $(basename ${{ github.action_path }}) ]; then
tag=${{ steps.branch-name.outputs.current_branch }}
else
tag=$(basename ${{ github.action_path }})
fi
echo "tag=${tag}" >> $GITHUB_OUTPUT
id: actions-info
- name: Git reset
if: steps.check.outputs.run == 'true'
shell: bash
run: |
# NOTE: If the trigger event is 'pull_request', `git fetch` is necessary.
git fetch
git reset --hard origin/${{ steps.branch-name.outputs.current_branch }}
- name: Export images' statistics in json before optimization
if: steps.check.outputs.run == 'true'
shell: bash
run: |
deno run \
--allow-read \
--allow-write \
https://raw.githubusercontent.com/9sako6/imgcmp/${{ steps.actions-info.outputs.tag }}/src/export_stat.ts \
'${{ inputs.paths-ignore-regexp }}' \
'/tmp/imgcmp-stat.json'
- name: Optimize images
if: steps.check.outputs.run == 'true'
shell: bash
run: |
deno run \
--allow-read \
--allow-run \
https://raw.githubusercontent.com/9sako6/imgcmp/${{ steps.actions-info.outputs.tag }}/src/optimize.ts \
'${{ inputs.paths-ignore-regexp }}'
- name: Export images' statistics in json after optimization
if: steps.check.outputs.run == 'true'
shell: bash
run: |
deno run \
--allow-read \
--allow-write \
https://raw.githubusercontent.com/9sako6/imgcmp/${{ steps.actions-info.outputs.tag }}/src/export_stat.ts \
'${{ inputs.paths-ignore-regexp }}' \
'/tmp/imgcmp-stat.json'
- name: Export images' statistics in json after optimization
if: steps.check.outputs.run == 'true'
shell: bash
run: |
message=$(deno run \
--allow-read \
https://raw.githubusercontent.com/9sako6/imgcmp/${{ steps.actions-info.outputs.tag }}/src/show_stat_message.ts \
'/tmp/imgcmp-stat.json'
)
title=$(deno run \
--allow-read \
https://raw.githubusercontent.com/9sako6/imgcmp/${{ steps.actions-info.outputs.tag }}/src/show_stat_title.ts \
'/tmp/imgcmp-stat.json'
)
echo "message<<EOF" >> $GITHUB_OUTPUT
echo "${message}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "title=${title}" >> $GITHUB_OUTPUT
id: template
- name: Check if images were compressed
if: steps.check.outputs.run == '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 "no_change=true" >> $GITHUB_OUTPUT
else
echo "::group::git diff"
git diff --name-only
echo "::endgroup::"
fi
id: diff
- name: Commit images
if: steps.check.outputs.run == '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 imgcmp action"
sha=$(git rev-parse --short HEAD)
new_branch="actions/imgcmp/${sha}"
echo "sha=${sha}" >> $GITHUB_OUTPUT
echo "new_branch=${new_branch}" >> $GITHUB_OUTPUT
id: commit
- name: Create a pull request
if: steps.check.outputs.run == '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-name.outputs.current_branch }}
title: ${{ steps.template.outputs.title }}
body: ${{ steps.template.outputs.message }}
draft: false
delete-branch: true
- name: Set outputs
if: steps.check.outputs.run == 'true' && steps.template.outputs.message != ''
shell: bash
id: set-outputs
run: |
echo "pull-request-number=$PULL_REQUEST_NUMBER" >> $GITHUB_OUTPUT
- name: Check outputs
if: steps.check.outputs.run == 'true' && steps.template.outputs.message != ''
shell: bash
run: |
echo "pull-request-number: ${{ steps.set-outputs.outputs.pull-request-number }}"