From 6b644bff1eaaaa2bb167e05bd8b6eba5f69b9b57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20G=C3=BCnter?= Date: Thu, 23 Jul 2020 12:12:42 +0200 Subject: [PATCH] ci: try to use cache when building plugin and untouched --- .gitlab-ci.ts | 28 ++++++++++++++-- devops/.gitlab/stage-build.ts | 33 ++++++++++++++++++- .../devops/.gitlab/stage-build.ts | 6 ++-- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/.gitlab-ci.ts b/.gitlab-ci.ts index c19ad4f..dc5d28e 100644 --- a/.gitlab-ci.ts +++ b/.gitlab-ci.ts @@ -85,11 +85,21 @@ const createConfig: CreateConfigFunction = async () => { true ); + const commonFiles = ["common/**/*", "devops/**/*", "*.{yml,ts,json,lock}"]; config.job( "common files changed", { only: { - changes: ["common/**/*", "devops/**/*", "*.{yml,ts,json,lock}"] + changes: commonFiles + } + }, + true + ); + config.job( + "no common files changed", + { + except: { + changes: commonFiles } }, true @@ -194,13 +204,27 @@ function createPackageJobs(config: Config, cwd: string, type: "packages" | "plug true ); + const changes = [`${type}/${prefix}/**/*`].concat(dependencies.map((dep) => `packages/${dep}/**/*`)); + // Create only changes expression so a job is only called when there is a relevant change config.extends( ".common files changed", `${prefix} only changes`, { only: { - changes: [`${type}/${prefix}/**/*`].concat(dependencies.map((dep) => `packages/${dep}/**/*`)) + changes + } + }, + true + ); + + // Create except changes expression so a job is only called when there is no relevant change + config.extends( + ".no common files changed", + `${prefix} except changes`, + { + except: { + changes } }, true diff --git a/devops/.gitlab/stage-build.ts b/devops/.gitlab/stage-build.ts index 14b3956..234d29b 100644 --- a/devops/.gitlab/stage-build.ts +++ b/devops/.gitlab/stage-build.ts @@ -6,6 +6,8 @@ type EsLintMacroArgs = MacroArgs & { type PhpCsMacroArgs = EsLintMacroArgs; +type BuildPluginMacroArgs = PhpCsMacroArgs; + const extendConfig: ExtendConfigFunction = async (config) => { // Generate technical documents config.extends( @@ -72,6 +74,10 @@ const extendConfig: ExtendConfigFunction = async (config) => { "build plugin", { stage: "build", + before_script: [ + // Check if build already exists so we can safely skip it + "test -d plugins/$JOB_PACKAGE_NAME/build && exit 0 || :" + ], script: [ // If we are in production build check if it is necessary '[ $CI_JOB_STAGE == "build production" ] && [ ! -f plugins/$JOB_PACKAGE_NAME/.publish ] && echo $LERNA_SKIP_MESSAGE && exit 0', @@ -94,6 +100,31 @@ const extendConfig: ExtendConfigFunction = async (config) => { true ); + config.macro("build plugin", (self, { prefix }) => { + const jobName = `${prefix} build`; + const cache = { + key: `${jobName}-$CI_COMMIT_REF_SLUG`, + untracked: true, + paths: ["plugins/$JOB_PACKAGE_NAME/build/"] + }; + + // The plugin is changed, build and only push to cache + config.extends([`.${prefix} jobs`, `.${prefix} only changes`, `.build plugin`], jobName, { + cache: { + ...cache, + policy: "push" + } + }); + + // The plugin is not changed, pull from cache and rebuild if not found + config.extends([`.${prefix} jobs`, `.${prefix} except changes`, `.build plugin`], `${jobName} from cache`, { + cache: { + ...cache, + policy: "pull-push" + } + }); + }); + config.macro("lint eslint", (self, { prefix }) => { const definingJob = `.${prefix} jobs`; const packageName = self.getVariable(definingJob, "JOB_PACKAGE_NAME"); @@ -129,4 +160,4 @@ const extendConfig: ExtendConfigFunction = async (config) => { }); }; -export { extendConfig, EsLintMacroArgs, PhpCsMacroArgs }; +export { extendConfig, EsLintMacroArgs, PhpCsMacroArgs, BuildPluginMacroArgs }; diff --git a/plugins/wp-reactjs-starter/devops/.gitlab/stage-build.ts b/plugins/wp-reactjs-starter/devops/.gitlab/stage-build.ts index 4afdcf2..b9c1f70 100644 --- a/plugins/wp-reactjs-starter/devops/.gitlab/stage-build.ts +++ b/plugins/wp-reactjs-starter/devops/.gitlab/stage-build.ts @@ -1,6 +1,6 @@ import { ExtendConfigFunction } from "node-gitlab-ci"; import { createPackageJobs } from "../../../../.gitlab-ci"; -import { EsLintMacroArgs, PhpCsMacroArgs } from "../../../../devops/.gitlab/stage-build"; +import { EsLintMacroArgs, PhpCsMacroArgs, BuildPluginMacroArgs } from "../../../../devops/.gitlab/stage-build"; const extendConfig: ExtendConfigFunction = async (config) => { const { prefix } = createPackageJobs(config, __dirname, "plugins"); @@ -15,7 +15,9 @@ const extendConfig: ExtendConfigFunction = async (config) => { config.from("lint phpcs", { prefix }); // Create build files and run it through docker - config.extends([`.${prefix} jobs`, `.build plugin`], `${prefix} build`, {}); + config.from("build plugin", { + prefix + }); }; export { extendConfig };