Skip to content

Commit

Permalink
Automatically run integration test on PR merge (#1182)
Browse files Browse the repository at this point in the history
Setup Github Workflow to automatically run integration test on commits to master branch.

The test can be triggered manually by visiting https://github.com/firebase/firebase-functions/actions/workflows/postmerge.yaml. You can also manually trigger the test via:

```
GCLOUD_PROJECT=<my project> npm run test:postmerge
```

Make sure your test project has all necessary resources enambled (i.e. enable Firestore, Storage, RTDB, Auth, etc.)

Only one test can run at a time, so I've enforced/disabled concurrency on the workflow.
  • Loading branch information
taeold committed Jul 29, 2022
1 parent 1877e98 commit 6edbb95
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/postmerge.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2022 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Post-merge tests

on:
workflow_dispatch:
workflow_run:
workflows: ['CI Tests']
types: [completed]
branches: [master]

concurrency:
group: postmerge-${{ github.ref }}
cancel-in-progress: true

env:
CI: true

jobs:
postmerge:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16

- uses: google-github-actions/auth@v0
with:
credentials_json: '${{ secrets.CF3_INTEGRATION_TEST_GOOGLE_CREDENTIALS }}'
create_credentials_file: true

- name: 'Set up Cloud SDK'
uses: google-github-actions/setup-gcloud@v0

- name: 'Setup Firebase CLI'
run: npm i -g firebase-tools

- name: 'Run integration test'
run: npm run test:postmerge

- name: Print debug logs
if: failure()
run: find . -type f -name "*debug.log" | xargs cat
34 changes: 7 additions & 27 deletions integration_test/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,14 @@
# Exit immediately if a command exits with a non-zero status.
set -e

function usage {
echo "Usage: ${0} <project_id> [<token>]"
exit 1
}
PROJECT_ID="${GCLOUD_PROJECT}"
TIMESTAMP=$(date +%s)

# This script takes in one required argument specifying a project_id and an
# optional arguement for a CI token that can be obtained by running
# `firebase login:ci`
# Example usage (from root dir) without token:
# ./integration_test/run_tests.sh chenky-test-proj
# Example usage (from root dir) with token:
# ./integration_test/run_tests.sh chenky-test-proj $TOKEN
if [[ "${1}" == "" ]]; then
usage
if [[ "${PROJECT_ID}" == "" ]]; then
echo "process.env.GCLOUD_PROJECT cannot be empty"
exit 1
fi

PROJECT_ID="${1}"
TIMESTAMP=$(date +%s)
TOKEN="${2}"

# Directory where this script lives.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Expand Down Expand Up @@ -64,22 +52,14 @@ function delete_all_functions {
cd "${DIR}"
# Try to delete, if there are errors it is because the project is already empty,
# in that case do nothing.
if [[ "${TOKEN}" == "" ]]; then
firebase functions:delete integrationTests v1 v2 --force --project=$PROJECT_ID || : &
else
firebase functions:delete integrationTests v1 v2 --force --project=$PROJECT_ID --token=$TOKEN || : &
fi
firebase functions:delete integrationTests v1 v2 --force --project=$PROJECT_ID || : &
wait
announce "Project emptied."
}

function deploy {
# Deploy functions, and security rules for database and Firestore. If the deploy fails, retry twice
if [[ "${TOKEN}" == "" ]]; then
for i in 1 2 3; do firebase deploy --project="${PROJECT_ID}" --only functions,database,firestore && break; done
else
for i in 1 2 3; do firebase deploy --project="${PROJECT_ID}" --token="${TOKEN}" --only functions,database,firestore && break; done
fi
for i in 1 2; do firebase deploy --project="${PROJECT_ID}" --only functions,database,firestore && break; done
}

function run_tests {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@
"lint": "tslint --config tslint.json --project tsconfig.json ",
"lint:fix": "tslint --config tslint.json --fix --project tsconfig.json",
"test": "mocha --file ./mocha/setup.ts \"spec/**/*.spec.ts\"",
"test:bin": "./scripts/bin-test/run.sh"
"test:bin": "./scripts/bin-test/run.sh",
"test:postmerge": "./integration_test/run_tests.sh"
},
"dependencies": {
"@types/cors": "^2.8.5",
Expand Down

0 comments on commit 6edbb95

Please sign in to comment.