diff --git a/README.md b/README.md index 77536de..b35c4f1 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,11 @@ 1. Generate a CI token through `firebase-tools` by running `firebase login:ci` 1. Place this token within your CI environment under the variable `FIREBASE_TOKEN` -1. Install `firebase-ci` into your project (so it is available on your CI): `npm install --save-dev firebase-ci` +1. Install `firebase-ci` into your project (so it is available on your CI): `npm install --save-dev firebase-ci`. You can also install `firebase-tools` locally so that the version is stored within your package file. 1. Add the following scripts to your CI config: ```bash - npm i -g firebase-ci@latest # install firebase-ci tool + npm i firebase-tools # install firebase-ci tool and firebase-tools firebase-ci deploy # deploys only on branches that have a matching project name in .firebaserc ``` @@ -32,7 +32,7 @@ ```yaml after_success: - - npm i firebase-ci + - npm i firebase-tools - $(npm bin)/firebase-ci deploy ``` diff --git a/package-lock.json b/package-lock.json index e94c98f..fbbc88c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "firebase-ci", - "version": "0.5.0-alpha.5", + "version": "0.5.0-beta", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7eadc0a..274ca98 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "firebase-ci", - "version": "0.5.0-alpha.5", + "version": "0.5.0-beta", "description": "Simplified Firebase interaction for continuous integration including deploying hosting, functions, and database/storage rules.", "main": "lib/index.js", "bin": { diff --git a/src/actions/copyVersion.js b/src/actions/copyVersion.js index de588f5..5a3a2ab 100644 --- a/src/actions/copyVersion.js +++ b/src/actions/copyVersion.js @@ -20,7 +20,7 @@ export default function copyVersion(config = { silence: false }) { warn('Functions folder does not exist. Exiting...') return } - info('Copying version into functions package.json...') + info('Copying version from package.json to functions/package.json...') const pkg = JSON.parse(fs.readFileSync(createPath('package.json'))) const functionsPkg = JSON.parse( fs.readFileSync(createPath(`functions/package.json`)) diff --git a/src/actions/deploy.js b/src/actions/deploy.js index de02dd4..9d40869 100644 --- a/src/actions/deploy.js +++ b/src/actions/deploy.js @@ -9,8 +9,8 @@ import { getBranch, isPullRequest, getDeployMessage, - getProjectName, - getFallbackProjectName + getProjectKey, + getFallbackProjectKey } from '../utils/ci' import { to } from '../utils/async' @@ -34,7 +34,7 @@ export function runActions() { return Promise.reject(err) }) } - info('No ci action settings found in .firebaserc. Skipping actions.') + info('No ci action settings found in .firebaserc. Skipping action phase.') return Promise.resolve({}) } @@ -71,27 +71,30 @@ export default async function deploy(opts) { throw new Error('firebase.json file is required') } - const fallbackProjectName = getFallbackProjectName() + const fallbackProjectName = getFallbackProjectKey() // Get project from passed options, falling back to branch name - const projectName = getProjectName(opts) + const projectKey = getProjectKey(opts) // Get project setting from settings file based on branchName falling back // to fallbackProjectName - const projectSetting = get(settings, `projects.${projectName}`) + const projectName = get(settings, `projects.${projectKey}`) const fallbackProjectSetting = get( settings, `projects.${fallbackProjectName}` ) + // Handle project option - if (!projectSetting) { - const nonProjectBranch = `${skipPrefix} - "${projectName}" not an Alias, checking for fallback...` + if (!projectName) { + const nonProjectBranch = `${skipPrefix} - Project "${projectKey}" is not an alias, checking for fallback...` info(nonProjectBranch) if (!fallbackProjectSetting) { - const nonFallbackBranch = `${skipPrefix} - Fallback Project: "${fallbackProjectName}" is a not an Alias, exiting...` + const nonFallbackBranch = `${skipPrefix} - Fallback Project: "${fallbackProjectName}" is a not an alias, exiting...` info(nonFallbackBranch) return nonProjectBranch } return nonProjectBranch } + + // Handle FIREBASE_TOKEN not existing within environment variables const { FIREBASE_TOKEN } = process.env if (!FIREBASE_TOKEN) { error('Error: FIREBASE_TOKEN env variable not found.') @@ -101,20 +104,25 @@ export default async function deploy(opts) { ) throw new Error('Error: FIREBASE_TOKEN env variable not found.') } + const onlyString = opts && opts.only ? `--only ${opts.only}` : '' const message = getDeployMessage() - // Install firebase-tools and functions dependencies if enabled + + // Install firebase-tools and functions dependencies unless skipped by config if (!settings.skipDependencyInstall) { await installDeps(opts, settings) } else { - info('Dependency install skipped') + info('firebase-tools and functions dependencies installs skipped') } + // Run CI actions if enabled (i.e. copyVersion, createConfig) if (!opts.simple) { runActions(opts.actions) } else { info('Simple mode enabled. Skipping CI actions') } + + // Run deploy command const [deployErr] = await to( runCommand({ command: 'npx', @@ -125,18 +133,21 @@ export default async function deploy(opts) { '--token', FIREBASE_TOKEN || 'Invalid.Token', '--project', - projectName, + projectKey, '--message', message ]), - beforeMsg: `Deploying to ${branchName} branch to ${projectName} Firebase project`, + beforeMsg: `Deploying to ${branchName} branch to ${projectKey} Firebase project "${projectName}"`, errorMsg: 'Error deploying to firebase.', - successMsg: `Successfully Deployed ${branchName} branch to ${projectName} Firebase project` + successMsg: `Successfully Deployed ${branchName} branch to ${projectKey} Firebase project "${projectName}"` }) ) + + // Handle errors within the deploy command if (deployErr) { error('Error in firebase-ci:\n ', deployErr) throw deployErr } + return null } diff --git a/src/utils/ci.js b/src/utils/ci.js index 1755add..cfdc17c 100644 --- a/src/utils/ci.js +++ b/src/utils/ci.js @@ -23,12 +23,14 @@ export function getBranch() { } /** - * Get the name of the project from environment variables + * Get the key of the project matching the branch name which is gathered from + * from environment variables. This key is used to get the project settings + * from .firebaserc. * @param {Object} opts - Options object * @param {Object} opts.project - Project name from options * @return {String} Name of project */ -export function getProjectName(opts) { +export function getProjectKey(opts) { const branchName = getBranch() const { FIREBASE_CI_PROJECT } = process.env // Get project from passed options, falling back to branch name @@ -39,10 +41,11 @@ export function getProjectName(opts) { } /** - * Get the name of the fallback project from environment variables + * Get the key of the fallback project from environment variables. This key + * is used to get the project settings from .firebaserc. * @return {String} Name of fallback Project */ -export function getFallbackProjectName() { +export function getFallbackProjectKey() { const { CI_ENVIRONMENT_SLUG } = process.env return CI_ENVIRONMENT_SLUG }