From 7bbcdb47e56742496eabbc63fa5746c579ba1a2b Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 21 Aug 2024 08:35:55 -0700 Subject: [PATCH] Use separate flags for deploy and update-traffic commands (#539) https://github.com/google-github-actions/deploy-cloudrun/issues/537#issuecomment-2302097605 --- README.md | 33 ++++++++++++++++++++++++++++----- action.yml | 36 +++++++++++++++++++++++++++++++----- src/main.ts | 10 +++++++++- tests/unit/main.test.ts | 25 +++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2e62536..0efeb87 100644 --- a/README.md +++ b/README.md @@ -185,8 +185,11 @@ jobs: - timeout: _(Optional)_ Maximum request execution time, specified as a duration like "10m5s" for ten minutes and 5 seconds. -- flags: _(Optional)_ Space separate list of other Cloud Run flags. This can be used to access - features that are not exposed via this GitHub Action. +- flags: _(Optional)_ Space separate list of additional Cloud Run flags to pass to the deploy + command. This can be used to apply advanced features that are not exposed + via this GitHub Action. For Cloud Run services, this command will be + `gcloud run deploy`. For Cloud Run jobs, this command will be `gcloud jobs + deploy. with: flags: '--add-cloudsql-instances=...' @@ -203,9 +206,7 @@ jobs: Please note, this GitHub Action does not parse or validate the flags. You are responsible for making sure the flags are available on the gcloud - version and subcommand. The provided flags will be appended to the - `deploy` command. When `revision_traffic` or `tag_traffic` are set, the - flags will also be appended to the subsequent `update-traffic` command. + version and subcommand. - no_traffic: _(Optional, default: `false`)_ If true, the newly deployed revision will not receive traffic. This option is only applies to services. @@ -231,6 +232,28 @@ jobs: This is mutually-exclusive with `revision_traffic`. This option is only applies to services. +- update_traffic_flags: _(Optional)_ Space separate list of additional Cloud Run flags to pass to the `gcloud + run services update-traffic` command. This can be used to apply advanced + features that are not exposed via this GitHub Action. This flag only + applies with `revision_traffic` or `tag_traffic` is set. + + with: + traffic_flags: '--set-tags=...' + + Flags that include other flags must quote the _entire_ outer flag value. For + example, to pass `--args=-X=123`: + + with: + flags: '--set-tags=... "--args=-X=123"' + + See the [complete list of + flags](https://cloud.google.com/sdk/gcloud/reference/run/services/update#FLAGS) + for more information. + + Please note, this GitHub Action does not parse or validate the flags. You + are responsible for making sure the flags are available on the gcloud + version and subcommand. + - project_id: _(Optional)_ ID of the Google Cloud project in which to deploy the service. - region: _(Optional, default: `us-central1`)_ Region in which the Cloud Run services are deployed. diff --git a/action.yml b/action.yml index 5e4a4d2..f5270e8 100644 --- a/action.yml +++ b/action.yml @@ -198,8 +198,11 @@ inputs: flags: description: |- - Space separate list of other Cloud Run flags. This can be used to access - features that are not exposed via this GitHub Action. + Space separate list of additional Cloud Run flags to pass to the deploy + command. This can be used to apply advanced features that are not exposed + via this GitHub Action. For Cloud Run services, this command will be + `gcloud run deploy`. For Cloud Run jobs, this command will be `gcloud jobs + deploy. with: flags: '--add-cloudsql-instances=...' @@ -216,9 +219,7 @@ inputs: Please note, this GitHub Action does not parse or validate the flags. You are responsible for making sure the flags are available on the gcloud - version and subcommand. The provided flags will be appended to the - `deploy` command. When `revision_traffic` or `tag_traffic` are set, the - flags will also be appended to the subsequent `update-traffic` command. + version and subcommand. required: false no_traffic: @@ -255,6 +256,31 @@ inputs: applies to services. required: false + update_traffic_flags: + description: |- + Space separate list of additional Cloud Run flags to pass to the `gcloud + run services update-traffic` command. This can be used to apply advanced + features that are not exposed via this GitHub Action. This flag only + applies with `revision_traffic` or `tag_traffic` is set. + + with: + traffic_flags: '--set-tags=...' + + Flags that include other flags must quote the _entire_ outer flag value. For + example, to pass `--args=-X=123`: + + with: + flags: '--set-tags=... "--args=-X=123"' + + See the [complete list of + flags](https://cloud.google.com/sdk/gcloud/reference/run/services/update#FLAGS) + for more information. + + Please note, this GitHub Action does not parse or validate the flags. You + are responsible for making sure the flags are available on the gcloud + version and subcommand. + required: false + project_id: description: |- ID of the Google Cloud project in which to deploy the service. diff --git a/src/main.ts b/src/main.ts index 4f4076e..a18900c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -118,6 +118,7 @@ export async function run(): Promise { const labels = parseKVString(getInput('labels')); const skipDefaultLabels = parseBoolean(getInput('skip_default_labels')); const flags = getInput('flags'); + const updateTrafficFlags = getInput('update_traffic_flags'); let deployCmd: string[] = []; @@ -262,11 +263,18 @@ export async function run(): Promise { updateTrafficCmd.push('--project', projectId); } - // Add optional flags + // Add optional deploy flags if (flags) { const flagList = parseFlags(flags); if (flagList) { deployCmd = deployCmd.concat(flagList); + } + } + + // Add optional update-traffic flags + if (updateTrafficFlags) { + const flagList = parseFlags(updateTrafficFlags); + if (flagList) { updateTrafficCmd = updateTrafficCmd.concat(flagList); } } diff --git a/tests/unit/main.test.ts b/tests/unit/main.test.ts index a0cac83..82f13dc 100644 --- a/tests/unit/main.test.ts +++ b/tests/unit/main.test.ts @@ -398,6 +398,18 @@ test('#run', { concurrency: true }, async (suite) => { assertMembers(args, ['--tag', 'test']); }); + await suite.test('sets additional flags on the deploy command', async (t) => { + const mocks = defaultMocks(t.mock, { + service: 'my-test-service', + flags: '--arg1=1 --arg2=2', + }); + + await run(); + + const args = mocks.getExecOutput.mock.calls?.at(0)?.arguments?.at(1); + assertMembers(args, ['--arg1', '1', '--arg2', '2']); + }); + await suite.test('sets tag traffic if given', async (t) => { const mocks = defaultMocks(t.mock, { service: 'my-test-service', @@ -457,6 +469,19 @@ test('#run', { concurrency: true }, async (suite) => { assertMembers(updateTrafficArgs, ['--to-revisions', 'TEST=100']); }); + await suite.test('sets additional flags on the update-traffic command', async (t) => { + const mocks = defaultMocks(t.mock, { + service: 'my-test-service', + tag_traffic: 'test', + update_traffic_flags: '--arg1=1 --arg2=2', + }); + + await run(); + + const args = mocks.getExecOutput.mock.calls?.at(1)?.arguments?.at(1); + assertMembers(args, ['--arg1', '1', '--arg2', '2']); + }); + await suite.test('fails if service is not provided with revision traffic', async (t) => { defaultMocks(t.mock, { service: '',