Skip to content

Commit

Permalink
ci: try to use cache when building plugin and untouched
Browse files Browse the repository at this point in the history
  • Loading branch information
matzeeable committed Jul 23, 2020
1 parent 831ca2d commit 6b644bf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
28 changes: 26 additions & 2 deletions .gitlab-ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion devops/.gitlab/stage-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type EsLintMacroArgs = MacroArgs & {

type PhpCsMacroArgs = EsLintMacroArgs;

type BuildPluginMacroArgs = PhpCsMacroArgs;

const extendConfig: ExtendConfigFunction = async (config) => {
// Generate technical documents
config.extends(
Expand Down Expand Up @@ -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',
Expand All @@ -94,6 +100,31 @@ const extendConfig: ExtendConfigFunction = async (config) => {
true
);

config.macro<BuildPluginMacroArgs>("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<EsLintMacroArgs>("lint eslint", (self, { prefix }) => {
const definingJob = `.${prefix} jobs`;
const packageName = self.getVariable(definingJob, "JOB_PACKAGE_NAME");
Expand Down Expand Up @@ -129,4 +160,4 @@ const extendConfig: ExtendConfigFunction = async (config) => {
});
};

export { extendConfig, EsLintMacroArgs, PhpCsMacroArgs };
export { extendConfig, EsLintMacroArgs, PhpCsMacroArgs, BuildPluginMacroArgs };
6 changes: 4 additions & 2 deletions plugins/wp-reactjs-starter/devops/.gitlab/stage-build.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -15,7 +15,9 @@ const extendConfig: ExtendConfigFunction = async (config) => {
config.from<PhpCsMacroArgs>("lint phpcs", { prefix });

// Create build files and run it through docker
config.extends([`.${prefix} jobs`, `.build plugin`], `${prefix} build`, {});
config.from<BuildPluginMacroArgs>("build plugin", {
prefix
});
};

export { extendConfig };

0 comments on commit 6b644bf

Please sign in to comment.