diff --git a/acceptance/bundle/dms/attempt-name/databricks.yml.tmpl b/acceptance/bundle/dms/attempt-name/databricks.yml.tmpl new file mode 100644 index 00000000000..ba1b8328097 --- /dev/null +++ b/acceptance/bundle/dms/attempt-name/databricks.yml.tmpl @@ -0,0 +1,9 @@ +bundle: + name: dms-attempt-name-$UNIQUE_NAME +experimental: + deployment_history: true + +resources: + jobs: + foo: + name: foo diff --git a/acceptance/bundle/dms/attempt-name/out.test.toml b/acceptance/bundle/dms/attempt-name/out.test.toml new file mode 100644 index 00000000000..84b22ba6735 --- /dev/null +++ b/acceptance/bundle/dms/attempt-name/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["true"] diff --git a/acceptance/bundle/dms/attempt-name/output.txt b/acceptance/bundle/dms/attempt-name/output.txt new file mode 100644 index 00000000000..10a059e9403 --- /dev/null +++ b/acceptance/bundle/dms/attempt-name/output.txt @@ -0,0 +1,88 @@ + +=== With DATABRICKS_AUTO_DEPLOY_ATTEMPT set, the version the deploy creates records the attempt + +>>> DATABRICKS_AUTO_DEPLOY_ATTEMPT=webhook-triggers/[UUID][0]/attempts/[UUID][1] [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-attempt-name-[UNIQUE_NAME]/default/files... +Created jobs.foo +Files: 2 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged + +>>> print_requests.py --dms //versions ^//operations ^//complete +{ + "method": "POST", + "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", + "q": { + "version_id": "1" + }, + "body": { + "auto_deploy_attempt": "webhook-triggers/[UUID][0]/attempts/[UUID][1]", + "cli_version": "[CLI_VERSION]", + "operations": [ + { + "action_type": "OPERATION_ACTION_TYPE_CREATE", + "resource_key": "jobs.foo" + } + ], + "version_type": "VERSION_TYPE_DEPLOY" + } +} + +=== Without it, the request has no attempt + +>>> update_file.py databricks.yml name: foo name: renamed + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-attempt-name-[UNIQUE_NAME]/default/files... +Updated jobs.foo +Files: 1 uploaded, 0 deleted +Resources: 0 created, 1 changed, 0 deleted, 0 unchanged + +>>> print_requests.py --dms //versions ^//operations ^//complete +{ + "method": "POST", + "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", + "q": { + "version_id": "2" + }, + "body": { + "cli_version": "[CLI_VERSION]", + "operations": [ + { + "action_type": "OPERATION_ACTION_TYPE_UPDATE", + "resource_key": "jobs.foo" + } + ], + "previous_version_id": "1", + "version_type": "VERSION_TYPE_DEPLOY" + } +} + +=== A destroy doesn't record the attempt, even with the variable set + +>>> DATABRICKS_AUTO_DEPLOY_ATTEMPT=webhook-triggers/[UUID][0]/attempts/[UUID][1] [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.jobs.foo + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/dms-attempt-name-[UNIQUE_NAME]/default + +Destroy: 1 deleted + +>>> print_requests.py --dms //versions ^//operations ^//complete +{ + "method": "POST", + "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", + "q": { + "version_id": "3" + }, + "body": { + "cli_version": "[CLI_VERSION]", + "operations": [ + { + "action_type": "OPERATION_ACTION_TYPE_DELETE", + "resource_key": "jobs.foo" + } + ], + "previous_version_id": "2", + "version_type": "VERSION_TYPE_DESTROY" + } +} diff --git a/acceptance/bundle/dms/attempt-name/script b/acceptance/bundle/dms/attempt-name/script new file mode 100644 index 00000000000..9d47b9a87ac --- /dev/null +++ b/acceptance/bundle/dms/attempt-name/script @@ -0,0 +1,19 @@ +envsubst < databricks.yml.tmpl > databricks.yml +attempt=webhook-triggers/11111111-1111-1111-1111-111111111111/attempts/22222222-2222-2222-2222-222222222222 +trap "errcode trace '$CLI' bundle destroy --auto-approve" EXIT + +title "With DATABRICKS_AUTO_DEPLOY_ATTEMPT set, the version the deploy creates records the attempt" +trace DATABRICKS_AUTO_DEPLOY_ATTEMPT=$attempt $CLI bundle deploy +add_repl "$($CLI bundle summary -o json | jq -r .bundle.deployment.deployment_id)" DEPLOYMENT_ID +trace print_requests.py --dms //versions ^//operations ^//complete + +title "Without it, the request has no attempt" +trace update_file.py databricks.yml 'name: foo' 'name: renamed' +trace $CLI bundle deploy +trace print_requests.py --dms //versions ^//operations ^//complete + +title "A destroy doesn't record the attempt, even with the variable set" +trace DATABRICKS_AUTO_DEPLOY_ATTEMPT=$attempt $CLI bundle destroy --auto-approve +trace print_requests.py --dms //versions ^//operations ^//complete +# Destroyed above, so the cleanup trap has nothing left to do. +trap - EXIT diff --git a/bundle/env/dms.go b/bundle/env/dms.go index a92d9f09a07..d9dd49e5e5c 100644 --- a/bundle/env/dms.go +++ b/bundle/env/dms.go @@ -18,3 +18,14 @@ func RecordsDeploymentHistory(ctx context.Context, configured *bool) bool { value, _ := get(ctx, []string{DeploymentHistoryVariable}) return value == "true" } + +// AutoDeployAttemptVariable names the auto-deploy attempt a deploy runs for. The auto-deploy runner +// sets it, and the deploy records it on the deployment version it creates. +const AutoDeployAttemptVariable = "DATABRICKS_AUTO_DEPLOY_ATTEMPT" + +// AutoDeployAttempt returns the auto-deploy attempt to record on the deployment version. +func AutoDeployAttempt(ctx context.Context) (string, bool) { + return get(ctx, []string{ + AutoDeployAttemptVariable, + }) +} diff --git a/bundle/phases/dms.go b/bundle/phases/dms.go index bd5d846420d..c35201a6eb7 100644 --- a/bundle/phases/dms.go +++ b/bundle/phases/dms.go @@ -12,6 +12,7 @@ import ( "github.com/databricks/cli/bundle/config" "github.com/databricks/cli/bundle/config/resources" "github.com/databricks/cli/bundle/deployplan" + "github.com/databricks/cli/bundle/env" "github.com/databricks/cli/internal/build" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/dms" @@ -225,6 +226,13 @@ func startVersion(ctx context.Context, b *bundle.Bundle, versionType dms.Version OriginUrl: git.OriginURL, } } + // Links a deploy's version to the auto-deploy attempt that ran it, passed through as is for the + // service to validate. Auto-deploy never destroys, and other deploys leave the variable unset, so + // their request doesn't change. + var attempt string + if versionType == dms.VersionTypeDeploy { + attempt, _ = env.AutoDeployAttempt(ctx) + } version, err := w.BundleDeployments.CreateVersion(ctx, bundledeployments.CreateVersionRequest{ Parent: dms.DeploymentName(deploymentID), VersionId: strconv.Itoa(versionID), @@ -234,6 +242,7 @@ func startVersion(ctx context.Context, b *bundle.Bundle, versionType dms.Version PreviousVersionId: previousVersionID, GitInfo: gitInfo, Operations: staged, + AutoDeployAttempt: attempt, }, }) if err != nil {