예제 #1
0
def build_new_release(args):  # pylint: disable=too-many-locals
    """Find the latest release and build the artifacts if they are newer then
  the current release.
  """
    if not args.src_dir:
        raise ValueError("src_dir must be provided when building last green.")

    gcs_client = storage.Client()
    sha = get_latest_green_presubmit(gcs_client)

    bucket_name, _ = util.split_gcs_uri(args.releases_path)
    bucket = gcs_client.get_bucket(bucket_name)

    logging.info("Latest passing postsubmit is %s", sha)

    last_release_sha = get_last_release(bucket)
    logging.info("Most recent release was for %s", last_release_sha)

    sha = build_and_push_image.GetGitHash(args.src_dir)

    if sha == last_release_sha:
        logging.info("Already cut release for %s", sha)
        return

    build(args)
예제 #2
0
def build_operator_image(root_dir,
                         registry,
                         project=None,
                         should_push=True,
                         version_tag=None):
    """Build the main docker image for the TFJob CRD.
  Args:
    root_dir: Root directory of the repository.
    registry: The registry to use.
    project: If set it will be built using GCB.
    should_push: Should push the image to the registry, Defaule is True.
    version_tag: Optional tag for the version. If not specified derive
      the tag from the git hash.
  Returns:
    build_info: Dictionary containing information about the build.
  """
    context_dir = tempfile.mkdtemp(prefix="tmpTFJobCrdContext")
    logging.info("context_dir: %s", context_dir)
    if not os.path.exists(context_dir):
        os.makedirs(context_dir)

    # Build the go binaries
    go_path = os.environ["GOPATH"]
    commit = build_and_push_image.GetGitHash(root_dir)

    targets = [
        "github.com/kubeflow/tf-operator/cmd/tf-operator.v1",
    ]
    for t in targets:
        if t in ["github.com/kubeflow/tf-operator/cmd/tf-operator.v1"]:
            util.run([
                "go", "install", "-ldflags",
                '''-X github.com/kubeflow/tf-operator/pkg/version.GitSHA={}
          -X github.com/kubeflow/tf-operator/pkg/version.Version={}'''.format(
                    commit, version_tag), t
            ])
            continue
        util.run(["go", "install", t])

    # If the release is not done from a Linux machine
    # we need to grab the artefacts from /bin/linux_amd64
    bin_path = "bin"
    if platform.system() != "Linux":
        bin_path += "/linux_amd64"

    # List of paths to copy relative to root.
    sources = [
        "build/images/tf_operator/Dockerfile",
        "examples/tf_sample/tf_smoke.py",
        os.path.join(go_path, bin_path,
                     "tf-operator.v1"), "cmd", "pkg", "third_party", "vendor"
    ]

    for s in sources:
        src_path = os.path.join(root_dir, s)
        dest_path = os.path.join(context_dir, os.path.basename(s))
        if os.path.exists(dest_path):
            os.unlink(dest_path)
        if os.path.isdir(src_path):
            shutil.copytree(src_path, dest_path, symlinks=True)
        else:
            shutil.copyfile(src_path, dest_path)

    image_base = registry + "/tf_operator"

    if not version_tag:
        logging.info("No version tag specified; computing tag automatically.")
        n = datetime.datetime.now()
        version_tag = n.strftime("v%Y%m%d") + "-" + commit
    logging.info("Using version tag: %s", version_tag)
    image = image_base + ":" + version_tag
    latest_image = image_base + ":latest"

    if project:
        util.run([
            "gcloud", "builds", "submit", context_dir, "--tag=" + image,
            "--project=" + project
        ])

        # Add the latest tag.
        util.run([
            "gcloud", "container", "images", "add-tag", "--quiet", image,
            latest_image
        ])

    else:
        util.run(["docker", "build", "-t", image, context_dir])
        logging.info("Built image: %s", image)

        util.run(["docker", "tag", image, latest_image])

        if should_push:
            _push_image(image, latest_image)

    output = {
        "image": image,
        "commit": commit,
    }
    return output