Skip to content

Commit

Permalink
Merge pull request #5 from lavanet/tag-latest-on-same-image
Browse files Browse the repository at this point in the history
Tag latest on same image
  • Loading branch information
praveenperera authored Oct 19, 2023
2 parents a37b91a + 0d82fe9 commit 6aa54f5
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 28 deletions.
116 changes: 89 additions & 27 deletions .github/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import subprocess
from typing import List, Tuple


def get_release_and_pre_release_tags() -> Tuple[List[str], List[str]]:
tags_env = os.environ['TAGS']
if tags_env == '':
tags_env = os.environ["TAGS"]
if tags_env == "":
print("ERROR: TAGS environment variable is not set.")
exit(1)

Expand Down Expand Up @@ -34,68 +35,129 @@ def get_release_and_pre_release_tags() -> Tuple[List[str], List[str]]:

for release in releases_json:
# create releases list
if release['tag_name'] in tags and release['draft'] == False and release['prerelease'] == False:
releases.append(release['tag_name'])
if (
release["tag_name"] in tags
and release["draft"] == False
and release["prerelease"] == False
):
releases.append(release["tag_name"])

# create pre-releases list
if release['tag_name'] in tags and release['draft'] == False and release['prerelease'] == True:
pre_releases.append(release['tag_name'])
if (
release["tag_name"] in tags
and release["draft"] == False
and release["prerelease"] == True
):
pre_releases.append(release["tag_name"])

# break if we have all the tags we need
if len(releases) + len(pre_releases) == len(tags):
break

return releases, pre_releases


def main():
releases, pre_releases = get_release_and_pre_release_tags()
latest_release = releases[0]

# build release versions
for release in releases:
build(release)
for i, release in enumerate(releases):
if i == 0:
build(release, ["latest", release])
else:
build(release, [release])

# build pre-release versions
for pre_release in pre_releases:
build(pre_release, f"prerelease-{pre_release}")
build(pre_release, [f"prerelease-{pre_release}"])

# build latest
build(latest_release, "latest")

def build(version_tag: str, docker_tag = None):
if docker_tag is None:
docker_tag = version_tag
def build(version_tag: str, docker_tags=[]):
if docker_tags == []:
docker_tags = [version_tag]

use_cache_env = os.environ.get('USE_CACHE')
use_cache = use_cache_env != None and use_cache_env != 'false' and use_cache_env != '' and use_cache_env != '0'
use_cache_env = os.environ.get("USE_CACHE")
use_cache = (
use_cache_env != None
and use_cache_env != "false"
and use_cache_env != ""
and use_cache_env != "0"
)

images = [
["rpc", "lava-rpc"],
["provider", "lava-provider"],
["lavad", "lavad"],
["lavap", "lavap"],
["lavavisor", "lavavisor"],
]

images = [["rpc", "lava-rpc"], ["provider", "lava-provider"]]
for [dockerfile_path, image_name] in images:
if docker_tag != "latest" and image_exists_in_repo(image_name, docker_tag):
print(f"Image {image_name}:{docker_tag} already exists in repository, skipping")
continue
# if latest is one of the tag, always build
# for non latest only build if image doesn't exist in repo
if "latest" in docker_tags:
filtered_docker_tags = docker_tags
else:
print(f"Building {dockerfile_path} ({image_name}:{docker_tag}): TAG={version_tag}\n")
filtered_docker_tags = [docker_tag for docker_tag in docker_tags if not image_exists_in_repo(image_name, docker_tag)]

args = ["docker", "buildx", "build", ".", "-t", f"us-central1-docker.pkg.dev/lavanet-public/images/{image_name}:{docker_tag}", "--build-arg", f"TAG={version_tag}", "-f", "Dockerfile", "--push"]
if filtered_docker_tags == []:
print(f"Image {image_name} {docker_tags} already exists in repository, skipping")
continue
else:
print(f"Building {dockerfile_path} ({image_name} {filtered_docker_tags}): TAG={version_tag}\n")

# create tags list and flatten
tags = [
[
"-t",
f"us-central1-docker.pkg.dev/lavanet-public/images/{image_name}:{docker_tag}",
]
for docker_tag in filtered_docker_tags
]
tags = [tag for sub_list in tags for tag in sub_list]

args = [
"docker",
"buildx",
"build",
".",
*tags,
"--build-arg",
f"TAG={version_tag}",
"-f",
"Dockerfile",
"--push",
]

if use_cache:
args = args + ["--cache-from", "type=local,src=/tmp/.buildx-cache", "--cache-to", "type=local,dest=/tmp/.buildx-cache-new"]
args = args + [
"--cache-from",
"type=local,src=/tmp/.buildx-cache",
"--cache-to",
"type=local,dest=/tmp/.buildx-cache-new",
]

exit_code = subprocess.Popen(args, cwd=f"dockerfiles/{dockerfile_path}").wait()

if exit_code != 0:
print(f"ERROR: Failed to build {image_name}")
exit(1)

print(f"Successfully built {image_name}:{docker_tag}\n")
print(f"Successfully built {image_name} {filtered_docker_tags}\n")


def image_exists_in_repo(image_name: str, tag: str) -> bool:
args = ["docker", "manifest", "inspect", f"us-central1-docker.pkg.dev/lavanet-public/images/{image_name}:{tag}"]
args = [
"docker",
"manifest",
"inspect",
f"us-central1-docker.pkg.dev/lavanet-public/images/{image_name}:{tag}",
]

exit_code = subprocess.Popen(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).wait()

return exit_code == 0


if __name__ == '__main__':
if __name__ == "__main__":
main()
32 changes: 32 additions & 0 deletions dockerfiles/lavad/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
###############################################################################
################################## BUILDER ####################################
###############################################################################
FROM ubuntu as builder

RUN apt-get update && apt-get install -y curl git

ARG TAG

RUN [ -z "$TAG" ] && echo "TAG is required (ex: v0.24.0)" && exit 1 || true

WORKDIR app

# create bin directory
RUN mkdir -p /app/bin

# download lava binaries
RUN curl -L "https://github.com/lavanet/lava/releases/download/${TAG}/lavad-${TAG}-linux-amd64" > /app/bin/lavad && \
chmod +x /app/bin/lavad

###############################################################################
################################## RUNTIME ####################################
###############################################################################
FROM debian:stable-slim

WORKDIR app

RUN apt-get update && apt-get install -y ca-certificates

COPY --from=builder /app/bin/* /app/bin/

ENV PATH="/app/bin:${PATH}"
32 changes: 32 additions & 0 deletions dockerfiles/lavap/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
###############################################################################
################################## BUILDER ####################################
###############################################################################
FROM ubuntu as builder

RUN apt-get update && apt-get install -y curl git

ARG TAG

RUN [ -z "$TAG" ] && echo "TAG is required (ex: v0.24.0)" && exit 1 || true

WORKDIR app

# create bin directory
RUN mkdir -p /app/bin

# download lava binaries
RUN curl -L "https://github.com/lavanet/lava/releases/download/${TAG}/lavap-${TAG}-linux-amd64" > /app/bin/lavap && \
chmod +x /app/bin/lavap

###############################################################################
################################## RUNTIME ####################################
###############################################################################
FROM debian:stable-slim

WORKDIR app

RUN apt-get update && apt-get install -y ca-certificates

COPY --from=builder /app/bin/* /app/bin/

ENV PATH="/app/bin:${PATH}"
32 changes: 32 additions & 0 deletions dockerfiles/lavavisor/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
###############################################################################
################################## BUILDER ####################################
###############################################################################
FROM ubuntu as builder

RUN apt-get update && apt-get install -y curl git

ARG TAG

RUN [ -z "$TAG" ] && echo "TAG is required (ex: v0.24.0)" && exit 1 || true

WORKDIR app

# create bin directory
RUN mkdir -p /app/bin

# download lava binaries
RUN curl -L "https://github.com/lavanet/lava/releases/download/${TAG}/lavavisor-${TAG}-linux-amd64" > /app/bin/lavavisor && \
chmod +x /app/bin/lavavisor

###############################################################################
################################## RUNTIME ####################################
###############################################################################
FROM debian:stable-slim

WORKDIR app

RUN apt-get update && apt-get install -y ca-certificates

COPY --from=builder /app/bin/* /app/bin/

ENV PATH="/app/bin:${PATH}"
2 changes: 1 addition & 1 deletion dockerfiles/provider/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
###############################################################################
FROM ubuntu as builder

RUN apt-get update && apt-get install -y curl git build-essential tar
RUN apt-get update && apt-get install -y curl git

ARG TAG

Expand Down

0 comments on commit 6aa54f5

Please sign in to comment.