forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
72 lines (58 loc) · 2.21 KB
/
optimize-images.yml
File metadata and controls
72 lines (58 loc) · 2.21 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
name: Optimize images
# **What it does**: Optimize images.
# **Why we have it**: Reduce bandwidth needs.
# **Who does it impact**: Docs engineering.
on:
workflow_dispatch:
pull_request:
paths:
- '**/*.png'
permissions:
contents: write
pull-requests: write
jobs:
optimize-images-on-pr:
# We can't make commits on forks
if: github.repository == 'github/docs-internal'
runs-on: ubuntu-latest
steps:
- name: Check out repo on head ref
uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
with:
ref: ${{ github.head_ref }}
# Need to specify a PAT here because otherwise GITHUB_TOKEN is used
# by default. Workflows won't trigger in that case because actions
# performed with GITHUB_TOKEN don't trigger other workflows.
token: ${{ secrets.DOCS_BOT_PAT_READPUBLICKEY }}
- name: Check out base ref
run: git fetch --no-tags --depth=1 origin $GITHUB_BASE_REF
- name: Install the Optipng package
run: sudo apt-get update && sudo apt-get -y install optipng
- name: Run optipng on new or changed images
run: |
set -e # exit when any command fails
echo "Ensure we can view $GITHUB_BASE_REF"
git checkout $GITHUB_BASE_REF
echo "Ensure we can view $GITHUB_HEAD_REF"
git checkout $GITHUB_HEAD_REF
echo "List the files that changed"
git diff --name-only --diff-filter=d $GITHUB_BASE_REF $GITHUB_HEAD_REF
echo "Run optipng on pngs in from the diff"
git diff --name-only -z --diff-filter=d $GITHUB_BASE_REF $GITHUB_HEAD_REF -- '*.png' | xargs -0 optipng -nx
- name: Make a commit and a push
run: |
echo "If there's no changes, exit"
if [[ ! `git status --porcelain` ]]
then
echo "No changes found"
exit 0
fi
echo "Make a commit"
git config user.name github-actions
git config user.email github-actions@github.com
git add "*.png"
git commit --message="Optimize images"
echo "Push up changes"
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}