Skip to content

Commit

Permalink
Merge pull request #7 from mathrix-education/dev
Browse files Browse the repository at this point in the history
chore(release): release version 1.0.0 - stable
  • Loading branch information
matbour authored Jan 27, 2020
2 parents 8958d7a + db19ff0 commit 0297024
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 115 deletions.
33 changes: 29 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
name: Tests
on: push
on:
push:
branches: ['**']
jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
os: [ubuntu-latest, macos-latest, windows-latest]
version: [latest, 245.0.0]
project: [auto, matrix-education]
exclude:
- os: ubuntu-latest
version: 245.0.0
project: matrix-education
- os: macos-latest
version: latest
project: matrix-education
- os: macos-latest
version: 245.0.0
project: auto
- os: macos-latest
version: 245.0.0
project: matrix-education
- os: windows-latest
version: latest
project: matrix-education
- os: windows-latest
version: 245.0.0
project: auto
- os: windows-latest
version: 245.0.0
project: matrix-education
fail-fast: false
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repository
uses: actions/checkout@v1
uses: actions/checkout@v2
- name: Setup Node.js 12
uses: actions/setup-node@v1.2.0
with:
Expand All @@ -19,7 +44,7 @@ jobs:
run: node dist/index.js
env:
INPUT_VERSION: ${{ matrix.version }}
INPUT_PROJECT: ${{ secrets.GCLOUD_PROJECT }}
INPUT_PROJECT: ${{ matrix.project }}
INPUT_COMPONENTS: beta
INPUT_SERVICE-ACCOUNT-KEY: ${{ secrets.GCLOUD_AUTH }}
- name: Test Google Cloud SDK CLI
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

## Version 1.0.0 - Stable release
- **feat**: added support for Windows
- **fixed**: inputs are now compared to strings
- **fixed**: project guess using the `service-account-key` input
- **changed**: new install directories
- `/usr/lib/google-cloud-sdk` on Ubuntu runners (overrides the already installed one)
- `$HOME/opt/google-cloud-sdk` on MacOS runners
- `C:\Program Files\google-cloud-sdk` on Windows runners

## Version 0.1.x - Pre-releases
The versions are now deprecated and should not be used.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# @mathrix-education/setup-gcloud
# mathrix-education/setup-gcloud

![Workflow status][workflow]
![Latest release][latest-release]

[workflow]: https://img.shields.io/github/workflow/status/mathrix-education/setup-gcloud/Tests?style=flat-square
[latest-release]: https://img.shields.io/github/v/release/mathrix-education/setup-gcloud?label=latest%20release&style=flat-square

Install the Google Cloud SDK in your GitHub Actions workflow.

**This action is not supported by Google Cloud.**
Expand All @@ -18,27 +25,29 @@ If the official [@actions/gcloud][2.1] action works perfectly, the fact that it
So we chose to create a JavaScript action to fix this problem, also adding additional features, such as Docker
authentication with Google Cloud Container Registry.

Update [2019/01/27]: The [GoogleCloudPlatform official GitHub organization][2.4] has released an official
[setup-gcloud][2.5] action. Compared to Mathrix's one, we provide some additional automation tasks, such as project
guessing and automatic Docker Configuration.

[2.1]: https://github.com/actions/gcloud
[2.2]: https://github.com/actions/gcloud/tree/master/auth
[2.3]: https://github.com/actions/gcloud/tree/master/cli
[2.4]: https://github.com/GoogleCloudPlatform
[2.5]: https://github.com/GoogleCloudPlatform/github-actions/tree/master/setup-gcloud


## Usage
### Supported operating systems
This action currently supports only Mac-OS and Ubuntu based systems.
Indeed, despite our efforts, we have not been able to launch the
installation of the Google Cloud SDK on Windows.

The supported operating systems matrix is the following:
This action currently supports Ubuntu, Mac-OS and Windows based systems. The supported operating systems matrix is
the following:

| Operating system | Status |
|------------------|-------|
| `ubuntu-latest` | ![3.1] |
| `macos-latest` | ![3.1] |
| `windows-latest` | ![3.2] |
| `windows-latest` | ![3.1] |

[3.1]: https://img.shields.io/badge/status-supported-brightgreen
[3.2]: https://img.shields.io/badge/status-unsupported-red

### Inputs
| Name | Type | Default value |
Expand Down
8 changes: 4 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ inputs:
version:
description: Google Cloud SDK to use. If left null, will use the latest one.
required: false
default: latest
default: 'latest'
service-account-key:
description: The service account key to use, base64-encoded.
required: false
project:
description: The default Google Cloud project. You can change it later.
required: false
default: auto
default: 'auto'
components:
description: Install additional components (e.g. beta, gsutils, etc.)
description: Install additional components (e.g. beta, gsutil, etc.)
required: false
configure-docker:
description: Configure Docker to interact with Google Cloud Container Registry.
required: false
default: false
default: 'false'
137 changes: 94 additions & 43 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3903,36 +3903,53 @@ var external_path_ = __webpack_require__(622);
var exec = __webpack_require__(986);

// CONCATENATED MODULE: ./src/constants.ts
var _a;

const INSTALL_DIRECTORY = 'google-cloud-sdk';
const WINDOWS_INSTALL_PATH = `C:\\${INSTALL_DIRECTORY}`;
const UBUNTU_INSTALL_PATH = `/home/runner/${INSTALL_DIRECTORY}`;
const UBUNTU_INSTALL_PATH = `/usr/lib/${INSTALL_DIRECTORY}`;
const MACOS_INSTALL_PATH = Object(external_path_.resolve)((_a = process.env.HOME, (_a !== null && _a !== void 0 ? _a : process.cwd())), INSTALL_DIRECTORY);
const WINDOWS_INSTALL_PATH = `C:\\Program Files\\${INSTALL_DIRECTORY}`;

// CONCATENATED MODULE: ./src/utils.ts




/**
* Check if the runner is Windows-based.
*/
function isWindows() {
return process.platform === 'win32';
}
/**
* Check if the runner is MacOS-based.
*/
function isMacOS() {
return process.platform === 'darwin';
}
/**
* Check if the runner is Ubuntu-based.
*/
function isUbuntu() {
return process.platform === 'linux';
}
function getCloudSDKFolder() {
/**
* Get the Google Cloud SDK installation directory.
*/
function getCloudSDKDirectory() {
if (isWindows()) {
return WINDOWS_INSTALL_PATH;
}
else if (isUbuntu()) {
return UBUNTU_INSTALL_PATH;
}
else {
const home = process.env.HOME ? process.env.HOME : process.cwd();
return Object(external_path_.resolve)(home, INSTALL_DIRECTORY);
return MACOS_INSTALL_PATH;
}
}
/**
* Get the Google Cloud SDK download link
*/
function getDownloadLink() {
const baseUrl = 'https://dl.google.com/dl/cloudsdk/channels/rapid';
const version = Object(core.getInput)('version');
Expand All @@ -3954,9 +3971,18 @@ function getDownloadLink() {
return `${baseUrl}/downloads/google-cloud-sdk-${version}-linux-x86_64.tar.gz`;
}
}
/**
* Execute a gcloud command
* @param args The gcloud args
* @param options The command options
*/
async function gcloud(args, options = undefined) {
const gcloudPath = Object(external_path_.resolve)(getCloudSDKFolder(), 'bin', 'gcloud' + (isWindows() ? '.cmd' : ''));
args.unshift('--quiet');
let gcloudPath = Object(external_path_.resolve)(getCloudSDKDirectory(), 'bin', 'gcloud' + (isWindows() ? '.cmd' : ''));
if (isWindows()) {
// Windows installation directory is C:\Program Files and thus need to be escaped
gcloudPath = gcloudPath.replace(getCloudSDKDirectory(), `"${getCloudSDKDirectory()}"`);
}
args.unshift('--quiet'); // Add quiet to all commands
await Object(exec.exec)(gcloudPath, args, options);
}

Expand All @@ -3971,13 +3997,12 @@ async function gcloud(args, options = undefined) {
async function authenticate() {
// If service account key is not provided, skip the authentication
if (!Object(core.getInput)('service-account-key')) {
Object(core.warning)('No service-account-key input was passed.' +
'If it is intentional, you can safely ignore this warning.');
Object(core.warning)('No service-account-key input was passed. If it is intentional, you can safely ignore this warning.');
return;
}
// Write the service account key
const serviceAccountKeyBase64 = Object(core.getInput)('service-account-key');
const serviceAccountKeyJson = Buffer.from(serviceAccountKeyBase64, 'base64');
const serviceAccountKeyJson = Buffer.from(serviceAccountKeyBase64, 'base64').toString();
const serviceAccountKeyPath = Object(external_path_.resolve)(process.cwd(), 'gcloud.json');
Object(external_fs_.writeFileSync)(serviceAccountKeyPath, serviceAccountKeyJson);
// Activate the service account
Expand All @@ -3986,38 +4011,67 @@ async function authenticate() {
'activate-service-account',
`--key-file=${serviceAccountKeyPath}`,
]);
// Remove the service account key
Object(external_fs_.unlinkSync)(serviceAccountKeyPath);
// Configure the default project
if (Object(core.getInput)('project') === 'auto' &&
Object(core.getInput)('service-account-key') !== '') {
// Project will be read from the service account key
const serviceAccountKey = JSON.parse(serviceAccountKeyJson.toString());
if (serviceAccountKey.hasOwnProperty('project_id')) {
// If key has a project_id field, use it to set the default project
await gcloud(['config', 'set', 'project', serviceAccountKey.project_id]);
}
else {
Object(core.warning)('You gave a service account key, but it does not have the "project_id" key. Thus, the default project ' +
'cannot be configured. Your service account key might malformed.');
}
}
else if (!['', 'none', 'auto'].includes(Object(core.getInput)('project'))) {
// Project was passed as input
await gcloud(['config', 'set', 'project', Object(core.getInput)('project')]);
}
// Configure Docker if necessary
if (Object(core.getInput)('configure-docker')) {
if (Object(core.getInput)('configure-docker') === 'true') {
await gcloud(['--quiet', 'auth', 'configure-docker']);
}
// Remove the service account key
Object(external_fs_.unlinkSync)(serviceAccountKeyPath);
}

// EXTERNAL MODULE: ./node_modules/@actions/tool-cache/lib/tool-cache.js
var tool_cache = __webpack_require__(533);

// EXTERNAL MODULE: ./node_modules/@actions/io/lib/io.js
var io = __webpack_require__(1);

// CONCATENATED MODULE: ./src/download.ts






/**
* Download the Google Cloud SDK archive.
*/
async function download() {
const downloadLink = getDownloadLink();
const downloadPath = await Object(tool_cache.downloadTool)(downloadLink);
const extractionPath = Object(external_path_.resolve)(getCloudSDKFolder(), '..');
await Object(io.mkdirP)(getCloudSDKFolder());
const extractionPath = Object(external_path_.resolve)(getCloudSDKDirectory(), '..');
if (downloadLink.endsWith('.zip')) {
// Windows: simply extract zip file
await Object(tool_cache.extractZip)(downloadPath, extractionPath);
}
else if (downloadLink.endsWith('.tar.gz')) {
await Object(tool_cache.extractTar)(downloadPath, extractionPath);
if (isUbuntu()) {
// Ubuntu: Remove the existing installation of Google Cloud SDK
await Object(exec.exec)(`sudo rm -rf ${UBUNTU_INSTALL_PATH}`);
await Object(exec.exec)(`sudo tar -xf ${downloadPath} -C ${extractionPath}`);
}
else {
// MacOS: simply extract tar.gz file
await Object(tool_cache.extractTar)(downloadPath, extractionPath);
}
}
else {
// Should never be reached
Object(core.setFailed)(`Unexpected extension (expected zip or tar.gz), but got ${downloadLink}`);
}
}

Expand All @@ -4031,42 +4085,44 @@ var external_child_process_ = __webpack_require__(129);


/**
* Setup the Google Cloud SDK.
* Setup the Google Cloud SDK by running the install script.
*/
async function setup() {
const installScriptExtension = isWindows() ? 'bat' : 'sh';
const installScript = Object(external_path_.resolve)(getCloudSDKFolder(), `install.${installScriptExtension}`);
const installScript = Object(external_path_.resolve)(getCloudSDKDirectory(), `install.${installScriptExtension}`);
const args = [
'--usage-reporting=false',
'--command-completion=false',
'--path-update=false',
'--usage-reporting=false',
'--quiet',
];
if (Object(core.getInput)('components')) {
if (Object(core.getInput)('components') !== '') {
args.push('--additional-components=' + Object(core.getInput)('components'));
}
if (isWindows()) {
// @actions/exec does not exit on windows
Object(external_child_process_.execSync)(`"${installScript}" ${args.join(' ')}`, { stdio: 'inherit' });
if (isUbuntu()) {
/*
* On Ubuntu, since we extracted the SDK to a protected directory, we have also to run the installer as root, which
* has side-effects on the user $HOME folder.
*/
await Object(exec.exec)(`sudo ${installScript}`, args);
const user = process.env.USER || '';
const home = process.env.HOME || '';
await Object(exec.exec)(`sudo chown -R ${user} ${home}`);
}
else {
else if (isMacOS()) {
// On MacOS, we simply have to run the install script
await Object(exec.exec)(installScript, args);
}
if (Object(core.getInput)('project') === 'auto' &&
Object(core.getInput)('service-account-key')) {
// Project will be read from the service account key
const buffer = new Buffer(Object(core.getInput)('service-account-key'), 'base64');
const serviceAccountKey = JSON.parse(buffer.toString());
if (serviceAccountKey.hasOwnProperty('project_id')) {
await gcloud(['config', 'set', 'project', serviceAccountKey.project_id]);
}
else if (isWindows()) {
// @actions/exec does not exit on windows
Object(external_child_process_.execSync)(`"${installScript}" ${args.join(' ')}`, { stdio: 'inherit' });
}
else if (Object(core.getInput)('project') !== 'none') {
// Project was passed as input
await gcloud(['config', 'set', 'project', Object(core.getInput)('project')]);
else {
// Should never be reached
Object(core.setFailed)(`Unexpected os platform, got: ${process.platform}`);
}
const binPath = Object(external_path_.resolve)(getCloudSDKFolder(), 'bin');
const binPath = Object(external_path_.resolve)(getCloudSDKDirectory(), 'bin');
Object(core.addPath)(binPath);
}

Expand All @@ -4076,16 +4132,11 @@ async function setup() {




/**
* Install the Google Cloud SDK.
*/
async function install() {
try {
// Currently, Windows is disabled because the installer does not work properly
if (isWindows()) {
Object(core.error)('This action does not support Windows for now. PR are welcome!');
}
await download();
await setup();
await authenticate();
Expand Down
Loading

0 comments on commit 0297024

Please sign in to comment.