Skip to content

Commit

Permalink
going action compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
chabad360 committed Nov 26, 2019
1 parent 17ead42 commit 2a73f84
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ jobs:
name: calibreapp/image-actions
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: calibreapp/image-actions
uses: docker://calibreapp/github-image-actions
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout Repo
uses: actions/checkout@master

- name: Compress Images
uses: calibreapp/image-actions@master
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}
```
The `GITHUB_TOKEN` secret is [automatically generated by GitHub](https://help.github.com/en/articles/virtual-environments-for-github-actions#github_token-secret). This automatic token is [scoped only to the repository that is currently running the action.](https://help.github.com/en/articles/virtual-environments-for-github-actions#token-permissions)
Expand All @@ -36,21 +38,20 @@ The `GITHUB_TOKEN` secret is [automatically generated by GitHub](https://help.gi

By default, you probably don’t need to configure `image-actions`. However, if you’d like to ignore specific file paths, or change image compression options, read on.

Change the configuration options by adding a `.github/calibre/image-actions.yml` file:
Change the configuration options by adding arguments to the action:

```yml
jpeg:
quality: 80
png:
quality: 80
webp:
quality: 80
ignorePaths:
- "node_modules/**"
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}
jpegQuality: 80
pngQuality: 80
webpQuality: 80
ignorePaths: "node_modules/**,build"
# No spaces allowed
```

- The above configuation is what `image-actions` uses by default
- The `jpeg`, `png` and `webp` config keys will be delivered directly into [sharp’s](http://sharp.pixelplumbing.com) `toFormat`. ([JPEG options](http://sharp.pixelplumbing.com/en/stable/api-output/#jpeg), [PNG options](http://sharp.pixelplumbing.com/en/stable/api-output/#png), [Webp options](http://sharp.pixelplumbing.com/en/stable/api-output/#webp))
- The `jpegQuality`, `pngQuality` and `webpQuality` config keys will be delivered (basicly) directly into [sharp’s](http://sharp.pixelplumbing.com) `toFormat`. ([JPEG options](http://sharp.pixelplumbing.com/en/stable/api-output/#jpeg), [PNG options](http://sharp.pixelplumbing.com/en/stable/api-output/#png), [Webp options](http://sharp.pixelplumbing.com/en/stable/api-output/#webp))
- `ignorePaths` allows for path globbing [see the glob package for more details](https://www.npmjs.com/package/glob)

## Links and Resources
Expand Down
36 changes: 36 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: 'Image Compressor'
author: 'Calibre'
description: 'Compress Images'

inputs:
githubToken:
description: 'GitHub Token'
required: true

jpegQuality:
description: 'JPEG quality level'
required: false
default: 80

pngQuality:
description: 'PNG quality level'
required: false
default: 80

webpQuality:
description: 'WEBP quality level'
required: false
default: 80

ignorePaths:
description: 'Paths to ignore during search'
required: false
default: "nodemodules/**"

runs:
using: 'docker'
image: 'docker://calibreapp/github-image-actions'

branding:
icon: crop
color: green
15 changes: 10 additions & 5 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ const fs = require("fs").promises;

const yaml = require("js-yaml");

const { CONFIG_PATH } = require("./constants");
const { CONFIG_PATH,
JPEG_QUALITY,
PNG_QUALITY,
WEBP_QUALITY,
IGNORE_PATHS
} = require("./constants");

const getYamlConfig = async () => {
try {
Expand All @@ -16,10 +21,10 @@ const getYamlConfig = async () => {

const getConfig = async () => {
const defaultConfig = {
jpeg: { quality: 80 },
png: { quality: 80 },
webp: { quality: 80 },
ignorePaths: ["node_modules/**"]
jpeg: { quality: JPEG_QUALITY },
png: { quality: PNG_QUALITY },
webp: { quality: WEBP_QUALITY },
ignorePaths: IGNORE_PATHS
};

const ymlConfig = await getYamlConfig();
Expand Down
17 changes: 13 additions & 4 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require("path");

const GITHUB_TOKEN = process.env["GITHUB_TOKEN"];
const GITHUB_TOKEN = process.env["INPUT_GITHUBTOKEN"] ? process.env["INPUT_GITHUBTOKEN"] : process.env["GITHUB_TOKEN"];
const GITHUB_EVENT_NAME = process.env["GITHUB_EVENT_NAME"];
const GITHUB_EVENT_PATH = process.env["GITHUB_EVENT_PATH"];
const GITHUB_SHA = process.env["GITHUB_SHA"];
Expand All @@ -9,15 +9,20 @@ const GITHUB_REPOSITORY = process.env["GITHUB_REPOSITORY"];

const REPO_DIRECTORY = process.env["GITHUB_WORKSPACE"];

const JPEG_QUALITY = process.env["INPUT_JPEGQUALITY"] ? process.env["INPUT_JPEGQUALITY"] : 80;
const PNG_QUALITY = process.env["INPUT_PNGQUALITY"] ? process.env["INPUT_PNGQUALITY"] : 80;
const WEBP_QUALITY = process.env["INPUT_WEBPQUALITY"] ? process.env["INPUT_WEBPQUALITY"] : 80;
const IGNORE_PATHS = process.env["INPUT_IGNOREPATHS"] ? process.env["INPUT_IGNOREPATHS"].split(",") : ["nodemodules/**"];

const COMMITTER = {
name: "Calibre",
email: "hello@calibreapp.com"
};

if (!REPO_DIRECTORY) {
console.log("There is no GITHUB_WORKSPACE environment variable");
console.log("::error::There is no GITHUB_WORKSPACE environment variable");
process.exit(1);
}
};

const CONFIG_PATH = path.join(
REPO_DIRECTORY,
Expand All @@ -41,5 +46,9 @@ module.exports = {
REPO_DIRECTORY,
CONFIG_PATH,
EXTENSION_TO_SHARP_FORMAT_MAPPING,
COMMITTER
COMMITTER,
JPEG_QUALITY,
PNG_QUALITY,
WEBP_QUALITY,
IGNORE_PATHS
};

0 comments on commit 2a73f84

Please sign in to comment.