Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ci): isolate smoke tests, introduce caching #8449

Merged
merged 11 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions .github/actions/actionsLib.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* eslint-env node */
// @ts-check

import { fileURLToPath } from 'node:url'

import { getExecOutput } from '@actions/exec'
import { hashFiles } from '@actions/glob'

/**
* @typedef {import('@actions/exec').ExecOptions} ExecOptions
*/

export const REDWOOD_FRAMEWORK_PATH = fileURLToPath(new URL('../../', import.meta.url))

/**
* @param {string} command
* @param {ExecOptions} options
*/
function execWithEnv(command, { env = {}, ...rest } = {}) {
return getExecOutput(
command,
undefined,
{
// @ts-expect-error TS doesn't like spreading process.env here but it's fine for our purposes.
env: {
...process.env,
...env
},
...rest
}
)
}

/**
* @param {string} cwd
*/
export function createExecWithEnvInCwd(cwd) {
/**
* @param {string} command
* @param {Omit<ExecOptions, 'cwd'>} options
*/
return function (command, options = {}) {
return execWithEnv(command, { cwd, ...options })
}
}

export const execInFramework = createExecWithEnvInCwd(REDWOOD_FRAMEWORK_PATH)

/**
* @param {string} redwoodProjectCwd
*/
export function projectDeps(redwoodProjectCwd) {
return execInFramework('yarn project:deps', { env: { RWJS_CWD: redwoodProjectCwd } })
}

/**
* @param {string} redwoodProjectCwd
*/
export function projectCopy(redwoodProjectCwd) {
return execInFramework('yarn project:copy', { env: { RWJS_CWD: redwoodProjectCwd } })
}

/**
* @param {string} prefix
*/
export async function createCacheKeys(prefix) {
const baseKey = [
prefix,
process.env.RUNNER_OS,
].join('-')

const dependenciesKey = [
baseKey,
'dependencies',
await hashFiles(['yarn.lock', '.yarnrc.yml'].join('\n')),
].join('-')

const distKey = [
dependenciesKey,
'dist',
await hashFiles([
'package.json',
'babel.config.js',
'tsconfig.json',
'tsconfig.compilerOption.json',
'nx.json',
'lerna.json',
'packages',
].join('\n'))
].join('-')

return {
baseKey,
dependenciesKey,
distKey
}
}
11 changes: 11 additions & 0 deletions .github/actions/set-up-test-project/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Set up test project
description: Sets up the test project fixture in CI for smoke tests and CLI checks

runs:
# `node18` isn't supported yet
using: node16
main: 'setUpTestProject.mjs'

outputs:
test-project-path:
description: Path to the test project
111 changes: 111 additions & 0 deletions .github/actions/set-up-test-project/setUpTestProject.mjs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of trying to add caching to the test-project script, I pulled out the relevant steps here. This isn't ideal as we don't want things to get out of sync, but I don't want to make that script any more complicated than it already is. Plus all we ever do here in CI is copy and link the fixture

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* eslint-env node */
// @ts-check

import path from 'node:path'

import cache from '@actions/cache'
import core from '@actions/core'

import fs from 'fs-extra'

import {
createCacheKeys,
createExecWithEnvInCwd,
projectCopy,
projectDeps,
REDWOOD_FRAMEWORK_PATH,
} from '../actionsLib.mjs'

const TEST_PROJECT_PATH = path.join(
path.dirname(process.cwd()),
'test-project'
)

core.setOutput('test-project-path', TEST_PROJECT_PATH)

const {
dependenciesKey,
distKey
} = await createCacheKeys('test-project')

/**
* @returns {Promise<void>}
*/
async function main() {
const distCacheKey = await cache.restoreCache([TEST_PROJECT_PATH], distKey)

if (distCacheKey) {
console.log(`Cache restored from key: ${distKey}`)
return
}

const dependenciesCacheKey = await cache.restoreCache([TEST_PROJECT_PATH], dependenciesKey)

if (dependenciesCacheKey) {
console.log(`Cache restored from key: ${dependenciesKey}`)
await sharedTasks()
} else {
console.log(`Cache not found for input keys: ${distKey}, ${dependenciesKey}`)
await setUpTestProject()
}

await cache.saveCache([TEST_PROJECT_PATH], distKey)
console.log(`Cache saved with key: ${distKey}`)
}

/**
* @returns {Promise<void>}
*/
async function setUpTestProject() {
const TEST_PROJECT_FIXTURE_PATH = path.join(
REDWOOD_FRAMEWORK_PATH,
'__fixtures__',
'test-project'
)

console.log(`Creating project at ${TEST_PROJECT_PATH}`)
console.log()
await fs.copy(TEST_PROJECT_FIXTURE_PATH, TEST_PROJECT_PATH)

console.log(`Adding framework dependencies to ${TEST_PROJECT_PATH}`)
await projectDeps(TEST_PROJECT_PATH)
console.log()

console.log(`Installing node_modules in ${TEST_PROJECT_PATH}`)
await execInProject('yarn install')
console.log()

await cache.saveCache([TEST_PROJECT_PATH], dependenciesKey)
console.log(`Cache saved with key: ${dependenciesKey}`)

await sharedTasks()
}

const execInProject = createExecWithEnvInCwd(TEST_PROJECT_PATH)

/**
* @returns {Promise<void>}
*/
async function sharedTasks() {
console.log('Copying framework packages to project')
await projectCopy(TEST_PROJECT_PATH)
console.log()

console.log('Generating dbAuth secret')
const { stdout } = await execInProject(
'yarn rw g secret --raw',
{ silent: true }
)
fs.appendFileSync(
path.join(TEST_PROJECT_PATH, '.env'),
`SESSION_SECRET='${stdout}'`
)
console.log()

console.log('Running prisma migrate reset')
await execInProject(
'yarn rw prisma migrate reset --force',
)
}

main()
37 changes: 37 additions & 0 deletions .github/actions/set-up-yarn-cache/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Set up yarn cache
description: |
Sets up caching for yarn install steps.
Caches yarn's cache directory, install state, and node_modules.

runs:
using: composite

steps:
# We try to cache and restore yarn's cache directory and install state to speed up the yarn install step.
# Caching yarn's cache directory avoids its fetch step.
- name: 📁 Get yarn cache directory
id: get-yarn-cache-directory
run: echo "CACHE_DIRECTORY=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT
shell: bash

# If the primary key doesn't match, the cache will probably be stale or incomplete,
# but still worth restoring for the yarn install step.
- name: ♻️ Restore yarn cache
uses: actions/cache@v3
with:
path: ${{ steps.get-yarn-cache-directory.outputs.CACHE_DIRECTORY }}
key: yarn-cache-${{ runner.os }}-${{ hashFiles('yarn.lock', '.yarnrc.yml') }}
restore-keys: yarn-cache-${{ runner.os }}

# We avoid restore-keys for these steps because it's important to just start from scratch for new PRs.
- name: ♻️ Restore yarn install state
uses: actions/cache@v3
with:
path: .yarn/install-state.gz
key: yarn-install-state-${{ runner.os }}-${{ hashFiles('yarn.lock', '.yarnrc.yml') }}

- name: ♻️ Restore node_modules
uses: actions/cache@v3
with:
path: '**/node_modules'
key: yarn-node-modules-${{ runner.os }}-${{ hashFiles('yarn.lock', '.yarnrc.yml') }}
9 changes: 0 additions & 9 deletions .github/actions/setup_test_project/action.yaml

This file was deleted.

35 changes: 0 additions & 35 deletions .github/actions/setup_test_project/setup_test_project.mjs

This file was deleted.

Loading