diff --git a/.github/workflows/setup-gcloud-it.yml b/.github/workflows/setup-gcloud-it.yml index e5c094d1..0c276eb4 100644 --- a/.github/workflows/setup-gcloud-it.yml +++ b/.github/workflows/setup-gcloud-it.yml @@ -69,3 +69,22 @@ jobs: - name: Verify Exported shell: bash run: test -f $GOOGLE_APPLICATION_CREDENTIALS + + project_id: + name: setup-gcloud with configured project + runs-on: ${{ matrix.operating-system }} + strategy: + fail-fast: false + matrix: + operating-system: [ubuntu-latest, windows-latest, macos-latest] + steps: + - uses: actions/checkout@v2 + + - name: setup-gcloud + uses: ./setup-gcloud/ + with: + project_id: ${{ secrets.SETUP_GCLOUD_IT_PROJECT_ID }} + + - name: Verify project + shell: bash + run: gcloud config get-value project | grep ${{ secrets.SETUP_GCLOUD_IT_PROJECT_ID }} diff --git a/action.yml b/action.yml index 2f4e4741..fc447082 100644 --- a/action.yml +++ b/action.yml @@ -40,6 +40,14 @@ inputs: value can be raw or base64-encoded. required: true + project_id: + description: |- + ID of the Google Cloud project. If provided, this will configure gcloud to + use this project ID by default for commands. Individual commands can still + override the project using the --project flag which takes precedence. + default: null + required: false + export_default_credentials: description: |- Export the provided credentials as Google Default Application Credentials. diff --git a/setup-gcloud/action.yml b/setup-gcloud/action.yml index cf3d2fc4..a87d793a 100644 --- a/setup-gcloud/action.yml +++ b/setup-gcloud/action.yml @@ -40,6 +40,14 @@ inputs: value can be raw or base64-encoded. required: true + project_id: + description: |- + ID of the Google Cloud project. If provided, this will configure gcloud to + use this project ID by default for commands. Individual commands can still + override the project using the --project flag which takes precedence. + default: null + required: false + export_default_credentials: description: |- Export the provided credentials as Google Default Application Credentials. diff --git a/setup-gcloud/dist/index.js b/setup-gcloud/dist/index.js index 57c3930e..aab09e8d 100644 --- a/setup-gcloud/dist/index.js +++ b/setup-gcloud/dist/index.js @@ -9972,12 +9972,31 @@ function run() { let toolPath = toolCache.find('gcloud', version); if (!toolPath) { toolPath = yield installGcloudSDK(version); + core.info('Successfully installed gcloud Cloud SDK'); + } + // A workaround for https://github.com/actions/toolkit/issues/229 + // Currently exec on windows runs as cmd shell. + let toolCommand = 'gcloud'; + if (process.platform == 'win32') { + toolCommand = 'gcloud.cmd'; + } + // Set the project ID, if given. + const projectId = core.getInput('project_id'); + if (projectId) { + yield exec.exec(toolCommand, [ + '--quiet', + 'config', + 'set', + 'core/project', + projectId, + ]); + core.info('Successfully set default project'); } const serviceAccountEmail = core.getInput('service_account_email') || ''; const serviceAccountKey = core.getInput('service_account_key'); // if a service account key isn't provided, log an un-authenticated notice if (!serviceAccountKey) { - core.info('gcloud SDK installed without authentication.'); + core.info('No credentials provided, skipping authentication'); return; } // Handle base64-encoded credentials @@ -9998,14 +10017,15 @@ function run() { }); }); yield fs_1.promises.writeFile(tmpKeyFilePath, serviceAccountJSON); - // A workaround for https://github.com/actions/toolkit/issues/229 - // Currently exec on windows runs as cmd shell. - let toolCommand = 'gcloud'; - if (process.platform == 'win32') { - toolCommand = 'gcloud.cmd'; - } - // authenticate as the specified service account - yield exec.exec(`${toolCommand} auth activate-service-account ${serviceAccountEmail} --key-file=${tmpKeyFilePath}`); + // Authenticate as the specified service account. + yield exec.exec(toolCommand, [ + '--quiet', + 'auth', + 'activate-service-account', + serviceAccountEmail, + '--key-file', + tmpKeyFilePath, + ]); // Export credentials if requested - these credentials must be exported in // the shared workspace directory, since the filesystem must be shared among // all steps. diff --git a/setup-gcloud/src/setup-gcloud.ts b/setup-gcloud/src/setup-gcloud.ts index d2589ca3..7f0b9d33 100644 --- a/setup-gcloud/src/setup-gcloud.ts +++ b/setup-gcloud/src/setup-gcloud.ts @@ -56,6 +56,27 @@ async function run(): Promise { let toolPath = toolCache.find('gcloud', version); if (!toolPath) { toolPath = await installGcloudSDK(version); + core.info('Successfully installed gcloud Cloud SDK'); + } + + // A workaround for https://github.com/actions/toolkit/issues/229 + // Currently exec on windows runs as cmd shell. + let toolCommand = 'gcloud'; + if (process.platform == 'win32') { + toolCommand = 'gcloud.cmd'; + } + + // Set the project ID, if given. + const projectId = core.getInput('project_id'); + if (projectId) { + await exec.exec(toolCommand, [ + '--quiet', + 'config', + 'set', + 'core/project', + projectId, + ]); + core.info('Successfully set default project'); } const serviceAccountEmail = core.getInput('service_account_email') || ''; @@ -63,7 +84,7 @@ async function run(): Promise { // if a service account key isn't provided, log an un-authenticated notice if (!serviceAccountKey) { - core.info('gcloud SDK installed without authentication.'); + core.info('No credentials provided, skipping authentication'); return; } @@ -87,17 +108,15 @@ async function run(): Promise { }); await fs.writeFile(tmpKeyFilePath, serviceAccountJSON); - // A workaround for https://github.com/actions/toolkit/issues/229 - // Currently exec on windows runs as cmd shell. - let toolCommand = 'gcloud'; - if (process.platform == 'win32') { - toolCommand = 'gcloud.cmd'; - } - - // authenticate as the specified service account - await exec.exec( - `${toolCommand} auth activate-service-account ${serviceAccountEmail} --key-file=${tmpKeyFilePath}`, - ); + // Authenticate as the specified service account. + await exec.exec(toolCommand, [ + '--quiet', + 'auth', + 'activate-service-account', + serviceAccountEmail, + '--key-file', + tmpKeyFilePath, + ]); // Export credentials if requested - these credentials must be exported in // the shared workspace directory, since the filesystem must be shared among