Skip to content

Commit 5279c41

Browse files
authored
Добавляет кэш изображений в Github Actions (#395)
1 parent 4495613 commit 5279c41

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Check Image Cache
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: '0 0 * * 4'
7+
8+
jobs:
9+
check-images-cache:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Cache images
14+
id: images-cache
15+
uses: actions/cache@v4
16+
env:
17+
cache-name: cache-images
18+
with:
19+
path: '.cache/@11ty/_images'
20+
enableCrossOsArchive: true
21+
key: images-${{ hashFiles('src/articles/**/*.png', 'src/articles/**/*.jpg', 'src/articles/**/*.svg', 'src/articles/**/*.gif', 'src/people/**/*.jpg') }}
22+
- name: check cache
23+
if: steps.images-cache.outputs.cache-hit != 'true'
24+
shell: bash
25+
run: echo "Кэш изображений не найден

.github/workflows/deploy.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ jobs:
1414
- uses: actions/setup-node@v4
1515
with:
1616
node-version-file: package.json
17+
- name: Images cache
18+
id: images-cache
19+
uses: actions/cache@v4
20+
with:
21+
path: '.cache/@11ty/_images'
22+
enableCrossOsArchive: true
23+
key: images-${{ hashFiles('src/articles/**/*.png', 'src/articles/**/*.jpg', 'src/articles/**/*.svg', 'src/articles/**/*.gif', 'src/people/**/*.jpg') }}
1724
# Setup key
1825
- name: Setup key
1926
run: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
dist
3+
.cache

src/eleventy-config/transforms.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path';
22
import os from 'node:os';
3+
import fs from 'node:fs';
34
import htmlmin from 'html-minifier-terser';
45
import minifyXml from 'minify-xml';
56
import { parseHTML } from 'linkedom';
@@ -11,12 +12,9 @@ const isProdMode = process.env.NODE_ENV === 'production';
1112

1213
async function processImage({ imageElement, inputPath, options, attributes }) {
1314
const imageStats = await Image(inputPath, {
14-
filenameFormat: (hash, src, width, format) => {
15-
const extension = path.extname(src);
16-
const name = path.basename(src, extension);
17-
return `${hash}-${name}-${width}.${format}`;
18-
},
19-
...options,
15+
outputDir: '.cache/@11ty/_images',
16+
urlPath: '/_images/',
17+
...options
2018
});
2119

2220
const imageAttributes = Object.assign(
@@ -53,7 +51,6 @@ export default function(eleventyConfig) {
5351
}
5452

5553
const articleSourceFolder = path.dirname(this.page.inputPath);
56-
const outputArticleImagesFolder = path.join(path.dirname(this.page.outputPath), 'images');
5754

5855
await Promise.all(images.map(async(image) => {
5956
const fullImagePath = path.join(articleSourceFolder, image.src);
@@ -69,8 +66,6 @@ export default function(eleventyConfig) {
6966
formats: isProdMode && !isGif
7067
? ['svg', 'avif', 'webp', 'auto']
7168
: ['svg', 'webp', 'auto'],
72-
outputDir: outputArticleImagesFolder,
73-
urlPath: 'images/',
7469
svgShortCircuit: true,
7570
sharpOptions: {
7671
animated: true,
@@ -108,7 +103,6 @@ export default function(eleventyConfig) {
108103

109104
await Promise.all(images.map(async(image) => {
110105
const fullImagePath = path.join(eleventyConfig.dir.input, image.src);
111-
const avatarsOutputFolder = path.dirname(path.join(eleventyConfig.dir.output, image.src));
112106

113107
await processImage({
114108
imageElement: image,
@@ -124,8 +118,6 @@ export default function(eleventyConfig) {
124118
formats: isProdMode
125119
? ['svg', 'avif', 'webp', 'auto']
126120
: ['svg', 'webp', 'auto'],
127-
outputDir: avatarsOutputFolder,
128-
urlPath: image.src.split('/').slice(0, -1).join('/'),
129121
svgShortCircuit: true,
130122
},
131123
});
@@ -134,6 +126,12 @@ export default function(eleventyConfig) {
134126
return document.toString();
135127
});
136128

129+
eleventyConfig.on('eleventy.after', () => {
130+
const from = '.cache/@11ty/_images/';
131+
const to = path.join(eleventyConfig.directories.output, '_images');
132+
fs.cpSync(from, to, { recursive: true });
133+
});
134+
137135
eleventyConfig.addTransform('lazyYouTube', (content, outputPath) => {
138136
let articles = /articles\/([a-zA-Z0-9_-]+)\/index\.html/i;
139137
let iframes = /<iframe src="https:\/\/www\.youtube\.com\/embed\/([a-zA-Z0-9_-]+)"(.*?)><\/iframe>/ig;

0 commit comments

Comments
 (0)