def ParseBuildConfigArgs(trigger, args, messages, default_image, need_repo=False): """Parses build-config flags. Args: trigger: The trigger to populate. args: An argparse arguments object. messages: A Cloud Build messages module. default_image: The docker image to use if args.dockerfile_image is empty. need_repo: Whether or not a repo needs to be included explicitly in flags. """ if args.build_config: # If we don't need a repo, then the repository information is already known # and we just need the filename. Otherwise, this trigger needs to # be a GitFileSource trigger (which is taken care of in ParseGitRepoSource). if not need_repo: trigger.filename = args.build_config trigger.substitutions = cloudbuild_util.EncodeTriggerSubstitutions( args.substitutions, messages) if args.dockerfile: if args.substitutions: raise c_exceptions.ConflictingArgumentsException( 'Dockerfile and substitutions', 'Substitutions are not supported with a Dockerfile configuration.') image = args.dockerfile_image or default_image trigger.build = messages.Build(steps=[ messages.BuildStep( name='gcr.io/cloud-builders/docker', dir=args.dockerfile_dir, args=['build', '-t', image, '-f', args.dockerfile, '.'], ) ]) if args.inline_config: trigger.build = cloudbuild_util.LoadMessageFromPath(args.inline_config, messages.Build, 'inline build config', ['substitutions']) trigger.substitutions = cloudbuild_util.EncodeTriggerSubstitutions( args.substitutions, messages) if need_repo: # Repo is required if a build config (filename) or dockerfile was provided. required = args.build_config or args.dockerfile ParseGitRepoSource(trigger, args, messages, required=required)
def ParseBuildConfigArgs(trigger, args, messages, default_image): """Parses build-config flags. Args: trigger: The trigger to populate. args: An argparse arguments object. messages: A Cloud Build messages module. default_image: The docker image to use if args.dockerfile_image is empty. """ if args.build_config: trigger.filename = args.build_config trigger.substitutions = cloudbuild_util.EncodeTriggerSubstitutions( args.substitutions, messages) if args.dockerfile: image = args.dockerfile_image or default_image trigger.build = messages.Build(steps=[ messages.BuildStep( name='gcr.io/cloud-builders/docker', dir=args.dockerfile_dir, args=['build', '-t', image, '-f', args.dockerfile, '.'], ) ]) if args.included_files: trigger.includedFiles = args.included_files if args.ignored_files: trigger.ignoredFiles = args.ignored_files
def ParseTriggerFromFlags(self, args): project = properties.VALUES.core.project.Get(required=True) messages = cloudbuild_util.GetMessagesModule() trigger = messages.BuildTrigger() trigger.description = args.description # GitHub config gh = messages.GitHubEventsConfig(owner=args.repo_owner, name=args.repo_name) if args.comment_control and not args.pull_request_pattern: raise c_exceptions.RequiredArgumentException( '--comment-control', '--comment-control must be specified with --pull-request-pattern' ) if args.pull_request_pattern: gh.pullRequest = messages.PullRequestFilter( branch=args.pull_request_pattern) if args.comment_control: gh.pullRequest.commentControl = messages.PullRequestFilter.CommentControlValueValuesEnum.COMMENTS_ENABLED else: # Push event gh.push = messages.PushFilter(branch=args.branch_pattern, tag=args.tag_pattern) trigger.github = gh # Build Config if args.build_config: trigger.filename = args.build_config trigger.substitutions = cloudbuild_util.EncodeTriggerSubstitutions( args.substitutions, messages) if args.dockerfile: image = args.dockerfile_image if args.dockerfile_image else 'gcr.io/%s/github.com/%s/%s:$COMMIT_SHA' % ( project, args.repo_owner, args.repo_name) trigger.build = messages.Build(steps=[ messages.BuildStep( name='gcr.io/cloud-builders/docker', dir=args.dockerfile_dir, args=['build', '-t', image, '-f', args.dockerfile, '.'], ) ]) # Include/Exclude files if args.included_files: trigger.includedFiles = args.included_files if args.ignored_files: trigger.ignoredFiles = args.ignored_files return trigger
def CreateCleanPreviewBuildTrigger(messages, name, description, github_repo_owner, github_repo_name, cluster, location, build_tags, build_trigger_tags): """Creates the Cloud BuildTrigger config that deletes expired preview deployments. Args: messages: Cloud Build messages module. This is the value returned from cloudbuild_util.GetMessagesModule(). name: Trigger name, which must be unique amongst all triggers in a project. description: Trigger description. github_repo_owner: A GitHub repo owner to be used in the trigger's github field. github_repo_name: A GitHub repo name to be used in the trigger's github field. cluster: The name of the target cluster to check for expired deployments that is set to a substitution variable. location: The zone/region of the target cluster to check for the expired deployments that is set to a substitution variable. build_tags: Tags to append to build tags in addition to default tags. build_trigger_tags: Tags to append to build trigger tags in addition to default tags. Returns: messages.BuildTrigger, the Cloud BuildTrigger config. """ substitutions = { _GKE_CLUSTER_SUB_VAR: cluster, _GKE_LOCATION_SUB_VAR: location, } build_trigger = messages.BuildTrigger( name=name, description=description, github=messages.GitHubEventsConfig(owner=github_repo_owner, name=github_repo_name, push=messages.PushFilter( branch='$manual-only^', )), build=messages.Build(steps=[ messages.BuildStep(id=_CLEANUP_PREVIEW_BUILD_STEP_ID, name='gcr.io/cloud-builders/kubectl', entrypoint='bash', args=['-c', _CLEANUP_PREVIEW_SCRIPT]) ], substitutions=cloudbuild_util.EncodeSubstitutions( substitutions, messages), timeout='600s'), substitutions=cloudbuild_util.EncodeTriggerSubstitutions( substitutions, messages)) build_trigger.build.tags = _DEFAULT_CLEAN_PREVIEW_TAGS[:] if build_tags: for tag in build_tags: build_trigger.build.tags.append(tag) build_trigger.tags = _DEFAULT_CLEAN_PREVIEW_TAGS[:] if build_trigger_tags: for tag in build_trigger_tags: build_trigger.tags.append(tag) return build_trigger
def CreatePRPreviewBuildTrigger(messages, name, description, build_timeout, github_repo_owner, github_repo_name, pr_pattern, preview_expiry_days, comment_control, dockerfile_path, app_name, config_path, expose_port, gcs_config_staging_path, cluster, location, build_tags, build_trigger_tags): """Creates the Cloud BuildTrigger config that deploys an application when triggered by a PR create/update. Args: messages: Cloud Build messages module. This is the value returned from cloudbuild_util.GetMessagesModule(). name: Trigger name, which must be unique amongst all triggers in a project. description: Trigger description. build_timeout: An optional maximum time a triggered build is run before it times out. For example, "2h15m5s" is 2 hours, 15 minutes, and 5 seconds. If you do not specify a unit, seconds is assumed. If this value is None, a timeout is not set. github_repo_owner: A GitHub repo owner to be used in the trigger's github field. github_repo_name: A GitHub repo name to be used in the trigger's github field. pr_pattern: A regex value that is the base branch that the PR is targeting, which triggers the creation of the PR preview deployment. preview_expiry_days: How long a deployed preview application can exist before it is expired, in days, that is set to a substitution variable. comment_control: Whether or not a user must comment /gcbrun to trigger the deployment build. dockerfile_path: An optional path to the source repository's Dockerfile, relative to the source repository's root directory that is set to a substitution variable. If this value is not provided, 'Dockerfile' is used. app_name: An optional app name that is set to a substitution variable. If this value is None, the substitution variable is set to '' to indicate its absence. config_path: An optional path to the source repository's Kubernetes configs, relative to the source repository's root directory that is set to a substitution variable. If this value is None, the substitution variable is set to '' to indicate its absence. expose_port: An optional port that the deployed application listens to that is set to a substitution variable. If this value is None, the substitution variable is set to 0 to indicate its absence. gcs_config_staging_path: An optional path to a GCS subdirectory to copy application configs that is set to a substitution variable. If this value is None, the substitution variable is set to '' to indicate its absence. cluster: The name of the target cluster to deploy to that is set to a substitution variable. location: The zone/region of the target cluster to deploy to that is set to a substitution variable. build_tags: Tags to append to build tags in addition to default tags. build_trigger_tags: Tags to append to build trigger tags in addition to default tags. Returns: messages.BuildTrigger, the Cloud BuildTrigger config. """ substitutions = _BaseBuildSubstitutionsDict(dockerfile_path, app_name, config_path, expose_port, cluster, location, gcs_config_staging_path) substitutions[_PREVIEW_EXPIRY_SUB_VAR] = six.text_type(preview_expiry_days) build = messages.Build( steps=[ _BuildBuildStep(messages, _IMAGE), _PushBuildStep(messages, _IMAGE), messages.BuildStep(id=_PREPARE_DEPLOY_BUILD_STEP_ID, name=_GKE_DEPLOY_PROD, entrypoint='sh', args=['-c', _PREPARE_PREVIEW_DEPLOY_SCRIPT]), _SaveConfigsBuildStep(messages), messages.BuildStep(id=_APPLY_DEPLOY_BUILD_STEP_ID, name=_GKE_DEPLOY_PROD, entrypoint='sh', args=['-c', _APPLY_PREVIEW_DEPLOY_SCRIPT]), messages.BuildStep(id=_ANNOTATE_PREVIEW_NAMESPACE_BUILD_STEP_ID, name='gcr.io/cloud-builders/kubectl', entrypoint='sh', args=['-c', _ANNOTATE_PREVIEW_NAMESPACE_SCRIPT]) ], substitutions=cloudbuild_util.EncodeSubstitutions( substitutions, messages), options=messages.BuildOptions( substitutionOption=messages.BuildOptions. SubstitutionOptionValueValuesEnum.ALLOW_LOOSE), images=[_IMAGE], artifacts=messages.Artifacts( objects=messages.ArtifactObjects(location='gs://' + _EXPANDED_CONFIGS_PATH_DYNAMIC, paths=['output/expanded/*']))) if build_timeout is not None: try: # A bare number is interpreted as seconds. build_timeout_secs = int(build_timeout) except ValueError: build_timeout_duration = times.ParseDuration(build_timeout) build_timeout_secs = int(build_timeout_duration.total_seconds) build.timeout = six.text_type(build_timeout_secs) + 's' build.tags = _DEFAULT_PR_PREVIEW_TAGS[:] if build_tags: for tag in build_tags: build.tags.append(tag) github_config = messages.GitHubEventsConfig( owner=github_repo_owner, name=github_repo_name, pullRequest=messages.PullRequestFilter(branch=pr_pattern)) if comment_control: github_config.pullRequest.commentControl = messages.PullRequestFilter.CommentControlValueValuesEnum.COMMENTS_ENABLED build_trigger = messages.BuildTrigger( name=name, description=description, build=build, github=github_config, substitutions=cloudbuild_util.EncodeTriggerSubstitutions( substitutions, messages)) build_trigger.tags = _DEFAULT_PR_PREVIEW_TAGS[:] if build_trigger_tags: for tag in build_trigger_tags: build_trigger.tags.append(tag) return build_trigger
def CreateGitPushBuildTrigger(messages, name, description, build_timeout, csr_repo_name, github_repo_owner, github_repo_name, branch_pattern, tag_pattern, dockerfile_path, app_name, config_path, namespace, expose_port, gcs_config_staging_path, cluster, location, build_tags, build_trigger_tags): """Creates the Cloud BuildTrigger config that deploys an application when triggered by a git push. Args: messages: Cloud Build messages module. This is the value returned from cloudbuild_util.GetMessagesModule(). name: Trigger name, which must be unique amongst all triggers in a project. description: Trigger description. build_timeout: An optional maximum time a triggered build is run before it times out. For example, "2h15m5s" is 2 hours, 15 minutes, and 5 seconds. If you do not specify a unit, seconds is assumed. If this value is None, a timeout is not set. csr_repo_name: An optional CSR repo name to be used in the trigger's triggerTemplate field. If this field is provided, github_repo_owner and github_repo_name should not be provided. Either csr_repo_name or both github_repo_owner and github_repo_name must be provided. github_repo_owner: An optional GitHub repo owner to be used in the trigger's github field. If this field is provided, github_repo_name must be provided and csr_repo_name should not be provided. Either csr_repo_name or both github_repo_owner and github_repo_name must be provided. github_repo_name: An optional GitHub repo name to be used in the trigger's github field. If this field is provided, github_repo_owner must be provided and csr_repo_name should not be provided. Either csr_repo_name or both github_repo_owner and github_repo_name must be provided. branch_pattern: An optional regex value to be used to trigger. If this value if provided, tag_pattern should not be provided. branch_pattern or tag_pattern must be provided. tag_pattern: An optional regex value to be used to trigger. If this value if provided, branch_pattern should not be provided. branch_pattern or tag_pattern must be provided. dockerfile_path: An optional path to the source repository's Dockerfile, relative to the source repository's root directory that is set to a substitution variable. If this value is not provided, 'Dockerfile' is used. app_name: An optional app name that is set to a substitution variable. If this value is None, the substitution variable is set to '' to indicate its absence. config_path: An optional path to the source repository's Kubernetes configs, relative to the source repository's root directory that is set to a substitution variable. If this value is None, the substitution variable is set to '' to indicate its absence. namespace: An optional Kubernetes namespace of the cluster to deploy to that is set to a substitution variable. If this value is None, the substitution variable is set to 'default'. expose_port: An optional port that the deployed application listens to that is set to a substitution variable. If this value is None, the substitution variable is set to 0 to indicate its absence. gcs_config_staging_path: An optional path to a GCS subdirectory to copy application configs that is set to a substitution variable. If this value is None, the substitution variable is set to '' to indicate its absence. cluster: The name of the target cluster to deploy to that is set to a substitution variable. location: The zone/region of the target cluster to deploy to that is set to a substitution variable. build_tags: Tags to append to build tags in addition to default tags. build_trigger_tags: Tags to append to build trigger tags in addition to default tags. Returns: messages.BuildTrigger, the Cloud BuildTrigger config. """ substitutions = _BaseBuildSubstitutionsDict(dockerfile_path, app_name, config_path, expose_port, cluster, location, gcs_config_staging_path) if namespace is None: namespace = 'default' substitutions[_K8S_NAMESPACE_SUB_VAR] = namespace build_trigger = messages.BuildTrigger( name=name, description=description, build=CreateBuild(messages, build_timeout, True, None, _IMAGE, dockerfile_path, app_name, _VERSION, config_path, namespace, expose_port, gcs_config_staging_path, cluster, location, build_tags), substitutions=cloudbuild_util.EncodeTriggerSubstitutions( substitutions, messages)) if csr_repo_name: build_trigger.triggerTemplate = messages.RepoSource( projectId=properties.VALUES.core.project.Get(required=True), repoName=csr_repo_name, branchName=branch_pattern, tagName=tag_pattern) elif github_repo_owner and github_repo_name: build_trigger.github = messages.GitHubEventsConfig( owner=github_repo_owner, name=github_repo_name, push=messages.PushFilter(branch=branch_pattern, tag=tag_pattern)) build_trigger.tags = _DEFAULT_TAGS[:] if build_trigger_tags: for tag in build_trigger_tags: build_trigger.tags.append(tag) return build_trigger
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: Some value that we want to have printed later. """ client = cloudbuild_util.GetClientInstance() messages = cloudbuild_util.GetMessagesModule() trigger = messages.BuildTrigger() if args.trigger_config: trigger = cloudbuild_util.LoadMessageFromPath( path=args.trigger_config, msg_type=messages.BuildTrigger, msg_friendly_name='build trigger config', skip_camel_case=['substitutions']) else: repo_ref = args.CONCEPTS.repo.Parse() repo = repo_ref.reposId trigger = messages.BuildTrigger( description=args.description, triggerTemplate=messages.RepoSource( repoName=repo, branchName=args.branch_pattern, tagName=args.tag_pattern, ), ) # Build Config if args.build_config: trigger.filename = args.build_config trigger.substitutions = cloudbuild_util.EncodeTriggerSubstitutions( args.substitutions, messages) if args.dockerfile: project = properties.VALUES.core.project.Get(required=True) image = args.dockerfile_image if args.dockerfile_image else 'gcr.io/%s/%s:$COMMIT_SHA' % ( project, repo) trigger.build = messages.Build(steps=[ messages.BuildStep( name='gcr.io/cloud-builders/docker', dir=args.dockerfile_dir, args=[ 'build', '-t', image, '-f', args.dockerfile, '.' ], ) ]) # Include/Exclude files if args.included_files: trigger.includedFiles = args.included_files if args.ignored_files: trigger.ignoredFiles = args.ignored_files # Send the Create request project = properties.VALUES.core.project.Get(required=True) created_trigger = client.projects_triggers.Create( messages.CloudbuildProjectsTriggersCreateRequest( buildTrigger=trigger, projectId=project)) trigger_resource = resources.REGISTRY.Parse( None, collection='cloudbuild.projects.triggers', api_version='v1', params={ 'projectId': project, 'triggerId': created_trigger.id, }) log.CreatedResource(trigger_resource) return created_trigger
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: Some value that we want to have printed later. """ client = cloudbuild_util.GetClientInstance() messages = cloudbuild_util.GetMessagesModule() # Convert 'repo type' input string to proto. git_file_source_type = messages.GitFileSource.RepoTypeValueValuesEnum.UNKNOWN git_repo_source_type = messages.GitRepoSource.RepoTypeValueValuesEnum.UNKNOWN if args.repo_type == 'CLOUD_SOURCE_REPOSITORIES': git_file_source_type = messages.GitFileSource.RepoTypeValueValuesEnum.CLOUD_SOURCE_REPOSITORIES git_repo_source_type = messages.GitRepoSource.RepoTypeValueValuesEnum.CLOUD_SOURCE_REPOSITORIES elif args.repo_type == 'GITHUB': git_file_source_type = messages.GitFileSource.RepoTypeValueValuesEnum.GITHUB git_repo_source_type = messages.GitRepoSource.RepoTypeValueValuesEnum.GITHUB trigger = messages.BuildTrigger() if args.trigger_config: trigger = cloudbuild_util.LoadMessageFromPath( path=args.trigger_config, msg_type=messages.BuildTrigger, msg_friendly_name='build trigger config', skip_camel_case=['substitutions']) else: trigger = messages.BuildTrigger( name=args.name, description=args.description, cron=messages.CronConfig( schedule=args.schedule, timeZone=args.time_zone, ), ) # Source To Build trigger.sourceToBuild = messages.GitRepoSource( uri=args.repo_uri, ref=args.revision, repoType=git_repo_source_type, ) # Build Config if args.build_config: trigger.gitFileSource = messages.GitFileSource( path=args.build_config, uri=args.repo_uri, repoType=git_file_source_type, revision=args.revision, ) trigger.substitutions = cloudbuild_util.EncodeTriggerSubstitutions( args.substitutions, messages) # Send the Create request project = properties.VALUES.core.project.Get(required=True) created_trigger = client.projects_triggers.Create( messages.CloudbuildProjectsTriggersCreateRequest( buildTrigger=trigger, projectId=project)) trigger_resource = resources.REGISTRY.Parse( None, collection='cloudbuild.projects.triggers', api_version='v1', params={ 'projectId': project, 'triggerId': created_trigger.id, }) log.CreatedResource(trigger_resource) return created_trigger