Skip to content

Commit

Permalink
v0.5.0-beta (#29)
Browse files Browse the repository at this point in the history
* fix(deploy): cleanup wording of logs to clarify which Firebase project is being used
  • Loading branch information
prescottprue authored Aug 16, 2018
1 parent 4313d72 commit eb3f62a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@

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
```

For instance within a `travis.yml`:

```yaml
after_success:
- npm i firebase-ci
- npm i firebase-tools
- $(npm bin)/firebase-ci deploy
```

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/actions/copyVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`))
Expand Down
39 changes: 25 additions & 14 deletions src/actions/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
getBranch,
isPullRequest,
getDeployMessage,
getProjectName,
getFallbackProjectName
getProjectKey,
getFallbackProjectKey
} from '../utils/ci'
import { to } from '../utils/async'

Expand All @@ -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({})
}

Expand Down Expand Up @@ -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.')
Expand All @@ -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',
Expand All @@ -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
}
11 changes: 7 additions & 4 deletions src/utils/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit eb3f62a

Please sign in to comment.