-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
sz deploy website
command to Sharezone Repo CLI (#1356)
Closes #1348
- Loading branch information
1 parent
2aa6669
commit 1dbdaae
Showing
3 changed files
with
149 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,13 +91,15 @@ jobs: | |
flutter pub global activate --source path "$CI_CD_DART_SCRIPTS_PACKAGE_PATH" | ||
echo $(pwd)/bin >> $GITHUB_PATH | ||
- name: Build | ||
run: sz build website --flavor ${{ matrix.environment.flavor }} | ||
- name: Install Firebase CLI | ||
run: npm i -g [email protected] | ||
|
||
- uses: FirebaseExtended/action-hosting-deploy@v0 | ||
with: | ||
repoToken: "${{ secrets.GITHUB_TOKEN }}" | ||
firebaseServiceAccount: "${{ secrets[matrix.environment.serviceAccountSecret] }}" | ||
channelId: live | ||
entryPoint: "./website" | ||
projectId: ${{ matrix.environment.projectId }} | ||
- name: Build and deploy website | ||
env: | ||
FIREBASE_HOSTING_KEY: ${{ secrets[matrix.environment.serviceAccountSecret] }} | ||
run: | | ||
echo $FIREBASE_HOSTING_KEY > firebase-hosting-key.json | ||
export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/firebase-hosting-key.json | ||
sz deploy website \ | ||
--message "Workflow $GITHUB_JOB, commit $GITHUB_SHA" \ | ||
--flavor ${{ matrix.environment.flavor }} |
135 changes: 135 additions & 0 deletions
135
tools/sz_repo_cli/lib/src/commands/src/deploy_website_command.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright (c) 2022 Sharezone UG (haftungsbeschränkt) | ||
// Licensed under the EUPL-1.2-or-later. | ||
// | ||
// You may obtain a copy of the Licence at: | ||
// https://joinup.ec.europa.eu/software/page/eupl | ||
// | ||
// SPDX-License-Identifier: EUPL-1.2 | ||
|
||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:args/args.dart'; | ||
import 'package:process_runner/process_runner.dart'; | ||
import 'package:sz_repo_cli/src/common/common.dart'; | ||
|
||
/// Maps the different flavors to the corresponding Firebase project ID. | ||
final _flavorToProjectId = { | ||
'prod': 'sharezone-c2bd8', | ||
'dev': 'sharezone-debug', | ||
}; | ||
|
||
/// The different flavors of the web app that support deployment. | ||
final _webFlavors = [ | ||
'prod', | ||
'dev', | ||
]; | ||
|
||
/// Deploy the Sharezone web app to one of the several deploy sites (e.g. alpha | ||
/// or production). | ||
/// | ||
/// The command will automatically use the right firebase config as configured | ||
/// inside [_stageToTarget]. | ||
class DeployWebsiteCommand extends CommandBase { | ||
DeployWebsiteCommand(super.context) { | ||
argParser | ||
..addOption( | ||
firebaseDeployMessageOptionName, | ||
help: | ||
'(Optional) The message given to "firebase deploy --only:hosting" via the "--message" flag. Will default to the current commit hash.', | ||
) | ||
..addOption( | ||
flavorOptionName, | ||
allowed: _webFlavors, | ||
help: 'The flavor to build for.', | ||
defaultsTo: 'prod', | ||
); | ||
} | ||
|
||
static const firebaseDeployMessageOptionName = 'message'; | ||
static const flavorOptionName = 'flavor'; | ||
|
||
@override | ||
String get description => | ||
'Deploy the Sharezone website in the given environment'; | ||
|
||
@override | ||
String get name => 'website'; | ||
|
||
@override | ||
Future<void> run() async { | ||
// Its less work to just print everything right now instead of selectively | ||
// print and add custom print statements for non-verbose output. | ||
// One might add non-verbose output in the future but right now this is | ||
// easier. | ||
isVerbose = true; | ||
|
||
await _build(); | ||
await _deploy(); | ||
} | ||
|
||
Future<void> _build() async { | ||
final flavor = argResults![flavorOptionName] as String; | ||
await processRunner.runCommand([ | ||
'fvm', | ||
'dart', | ||
'run', | ||
'sz_repo_cli', | ||
'build', | ||
'website', | ||
'--flavor', | ||
flavor, | ||
], workingDirectory: repo.sharezoneCiCdTool.location); | ||
} | ||
|
||
Future<void> _deploy() async { | ||
final flavor = argResults![flavorOptionName] as String; | ||
final firebaseProjectId = _flavorToProjectId[flavor]!; | ||
|
||
final deployMessage = await _getDeployMessage(); | ||
|
||
await processRunner.run( | ||
[ | ||
'firebase', | ||
'deploy', | ||
'--project', | ||
firebaseProjectId, | ||
'--message', | ||
deployMessage, | ||
], | ||
workingDirectory: repo.sharezoneWebsite.location, | ||
); | ||
} | ||
|
||
Future<String> _getDeployMessage() async { | ||
final overriddenDeployMessage = _parseDeployMessage(argResults!); | ||
|
||
String? deployMessage; | ||
if (overriddenDeployMessage == null) { | ||
final currentCommit = await _getCurrentCommitHash(processRunner); | ||
deployMessage = 'Commit: $currentCommit'; | ||
} | ||
|
||
return deployMessage ?? overriddenDeployMessage!; | ||
} | ||
|
||
String? _parseDeployMessage(ArgResults argResults) { | ||
final overriddenDeployMessageOrNull = | ||
argResults[firebaseDeployMessageOptionName] as String?; | ||
return overriddenDeployMessageOrNull; | ||
} | ||
|
||
Future<String> _getCurrentCommitHash(ProcessRunner processRunner) async { | ||
final res = await processRunner.run(['git', 'rev-parse', 'HEAD']); | ||
if (res.stdout.isEmpty) { | ||
stderr.writeln( | ||
'Could not receive the current commit hash: (${res.exitCode}) ${res.stderr}.'); | ||
throw ToolExit(15); | ||
} | ||
final currentCommit = res.stdout; | ||
if (isVerbose) { | ||
stdout.writeln('Got current commit hash: $currentCommit'); | ||
} | ||
return currentCommit; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters