Skip to content

Commit 352c803

Browse files
authored
Merge pull request #2 from NVIDIA/docker_ci
feat(agent): add container build ci on commit with attestation
2 parents 09a70ab + 909c74f commit 352c803

22 files changed

+114
-31
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: Build and push container image
2+
3+
# Configures this workflow to run every time a tag is created
4+
on:
5+
push:
6+
branches:
7+
- main
8+
- docker_ci
9+
paths:
10+
- agent/**
11+
- .github/workflows/build_agent_container.yaml
12+
13+
# NOTE: we may want to switch to matrix build for multi-platform support if this is taking too long
14+
# https://docs.docker.com/build/ci/github-actions/multi-platform/#distribute-build-across-multiple-runners
15+
16+
17+
# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
18+
env:
19+
REGISTRY: ghcr.io
20+
IMAGE_NAME: ${{ github.repository }}
21+
22+
# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.
23+
jobs:
24+
build-and-push-image:
25+
runs-on: ubuntu-latest
26+
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
27+
permissions:
28+
contents: read
29+
packages: write
30+
attestations: write
31+
id-token: write
32+
#
33+
steps:
34+
- name: Checkout repository
35+
uses: actions/checkout@v4
36+
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
37+
- name: Log in to the Container registry
38+
uses: docker/login-action@v3
39+
with:
40+
registry: ${{ env.REGISTRY }}
41+
username: ${{ github.actor }}
42+
password: ${{ secrets.GITHUB_TOKEN }}
43+
44+
# Setup for multi-platform
45+
- name: Set up QEMU
46+
uses: docker/setup-qemu-action@v3
47+
48+
- name: Set up Docker Buildx
49+
uses: docker/setup-buildx-action@v3
50+
51+
- name: Build the agent container image
52+
id: build
53+
run: |
54+
apt-get update && apt-get install -y make git jq
55+
cd agent
56+
export TAGS="-t ${REGISTRY@L}/${{env.IMAGE_NAME}}/agent:${{ github.sha }}"
57+
export REGISTRY=${REGISTRY@L}
58+
# Get the last tag and use it as the env var AGENT_VERSION if it doesn't exist use 0.0.0+{github.sha}
59+
export AGENT_VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "0.0.0+${{ github.sha }}")
60+
make docker-build-only agent_version=${AGENT_VERSION}
61+
cat metadata.json
62+
echo "digest=$(cat metadata.json | jq -r .\"containerimage.digest\")" >> $GITHUB_OUTPUT
63+
cat $GITHUB_OUTPUT
64+
env:
65+
AGENT_IMAGE: ${{env.IMAGE_NAME}}/agent
66+
67+
# This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see [AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds).
68+
- name: Generate artifact attestation
69+
uses: actions/attest-build-provenance@v2
70+
with:
71+
subject-name: ${{ env.REGISTRY }}/${{env.IMAGE_NAME}}/agent
72+
subject-digest: ${{ steps.build.outputs.digest }}
73+
push-to-registry: true
74+

agent/Makefile

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ help: ## Display this help.
2727
venv: ## Sets up a python venv at `./venv`
2828
python3 -m venv venv
2929
$(VENV)pip install hatch coverage
30-
$(VENV)hatch config set dirs.project "[\"${PWD}\"]"
30+
$(VENV)hatch config set dirs.project "[\"$(shell pwd)\"]"
3131

3232
##@ Test
3333
.PHONY: test
@@ -46,43 +46,49 @@ format:
4646

4747
##@ Build
4848
.PHONY: build
49-
build: ## Builds using hatch to `skyhook-agent/dist`
50-
$(VENV)hatch -p skyhook-agent version ${1:unkown}
49+
build: ## Builds using hatch to `dist`
50+
$(VENV)hatch -p skyhook-agent version $(build_version)
5151
$(VENV)hatch -p skyhook-agent build -c
5252

5353
##@ Publish
5454
.PHONY: publish
5555
publish: ## Publishes using hatch
5656
$(VENV)hatch -p skyhook-agent publish
5757

58+
DOCKER_CMD ?= docker
5859
BUILD_ARGS ?=
5960
ifndef GITLAB_CI
6061
BUILD_ARGS = --push
6162
COMMIT_SHORT_SHA := $(shell git rev-parse --short HEAD)
6263
endif
6364

6465
docker-setup:
65-
test ! $(docker context ls | grep builder) || docker context create builder;
66-
docker buildx create --platform linux/amd64,linux/arm64 --use builder
67-
docker run --privileged --rm tonistiigi/binfmt --install amd64,arm64
66+
test ! $($(DOCKER_CMD) context ls | grep builder) || $(DOCKER_CMD) context create builder;
67+
$(DOCKER_CMD) buildx create --platform linux/amd64,linux/arm64 --use builder
68+
$(DOCKER_CMD) run --privileged --rm tonistiigi/binfmt --install amd64,arm64
69+
70+
ACTUAL_TAGS=$(shell echo "-t $(REGISTRY)/$(AGENT_IMAGE):$(shell date +%y.%m.%d-%H%M%S)-$(COMMIT_SHORT_SHA) $(TAGS)" | tr A-Z a-z)
71+
.PHONY: docker-build-only
72+
docker-build-only:
73+
@echo "Building skyhook-agent $(DOCKER_CMD) image with tags: $(ACTUAL_TAGS)"
74+
$(DOCKER_CMD) buildx build $(BUILD_ARGS) --build-arg AGENT_VERSION=$(AGENT_VERSION) --platform linux/amd64,linux/arm64 $(ACTUAL_TAGS) --metadata-file=metadata.json -f docker/Dockerfile .
6875

6976
##@ Docker Build
7077
.PHONY: docker-build
71-
docker-build docker-setup ## Builds skyhook-agent docker image using docker buildx.
72-
@TAGS="-t $(REGISTRY)/$(AGENT_IMAGE):$(shell date +%y.%m.%d-%H%M%S)-$(COMMIT_SHORT_SHA)"
73-
docker buildx build $(BUILD_ARGS) --platform linux/amd64,linux/arm64 $(TAGS) -f docker/Dockerfile .
78+
docker-build: docker-build-only docker-setup ## Builds skyhook-agent docker image using docker buildx.
79+
@echo "Built skyhook-agent $(DOCKER_CMD) image."
7480

7581
##@ Vendor
7682
.PHONY: vendor
7783
vendor: ## Uses Unearth to vendor all dependencies locally.
78-
python3 -m venv ./venv_vendor
79-
./venv_vendor/bin/pip install unearth toml
80-
dependencies=$(shell python -c 'import toml; print(" ".join(toml.loads(open("skyhook-agent/pyproject.toml","r").read())["project"]["dependencies"]))')
81-
rm -rf vendor
82-
mkdir -p vendor
83-
for dep in $(dependencies); do \
84-
./venv_vendor/bin/unearth --no-binary -d ./vendor $(dep) >> vendor/lock_file; \
85-
done
84+
python3 -m venv ./venv_vendor
85+
./venv_vendor/bin/pip install unearth toml
86+
dependencies=$(shell python -c 'import toml; print(" ".join(toml.loads(open("skyhook-agent/pyproject.toml","r").read())["project"]["dependencies"]))')
87+
rm -rf vendor
88+
mkdir -p vendor
89+
for dep in $(dependencies); do \
90+
./venv_vendor/bin/unearth --no-binary -d ./vendor $(dep) >> vendor/lock_file; \
91+
done
8692

8793
##@ Clean
8894
.PHONY: clean

agent/docker/Dockerfile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
FROM python:3.10-alpine as builder
1+
FROM python:3.12-alpine AS builder
22

3-
ARG CI_COMMIT_TAG:-0.0.0
3+
ARG AGENT_VERSION
44

55
COPY . /code
66
WORKDIR /code
7-
RUN apk add bash
8-
RUN USE_VENV=false /code/cmds.sh setup
9-
RUN USE_VENV=false /code/cmds.sh build ${CI_COMMIT_TAG}
7+
RUN echo "AGENT_VERSION=${AGENT_VERSION}"
8+
RUN apk update && apk add bash make build-base gcc python3-dev musl-dev linux-headers
9+
RUN make test
10+
RUN make clean
11+
RUN make venv
12+
RUN make build build_version=${AGENT_VERSION}
1013

11-
FROM python:3.10-alpine
14+
FROM python:3.12-alpine
1215

1316
RUN mkdir -p /skyhook-agent-wheels
1417
COPY --from=builder /code/skyhook-agent/dist/* /skyhook-agent-wheels

agent/hatch.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mode = "local"
2+
13
[envs.default]
24
dependencies = [
35
"coverage[toml]",
@@ -16,7 +18,7 @@ cov = [
1618
]
1719

1820
[[envs.all.matrix]]
19-
python = ["3.8", "3.9", "3.10"]
21+
python = ["3.12"]
2022

2123
[envs.lint]
2224
detached = true
@@ -40,6 +42,3 @@ all = [
4042
"style",
4143
"typing",
4244
]
43-
44-
[version]
45-
source = "vcs"

agent/skyhook-agent/README.md

Whitespace-only changes.
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["hatchling", "hatch-vcs"]
2+
requires = ["hatchling"]
33
build-backend = "hatchling.build"
44

55
[project]
@@ -34,6 +34,7 @@ Source = "https://github.com/nvidia.com/skyhook"
3434
[project.scripts]
3535
controller = "skyhook_agent.controller:cli"
3636

37+
3738
[tool.hatch.version]
3839
path = "src/skyhook_agent/__about__.py"
3940

@@ -61,8 +62,8 @@ omit = [
6162
]
6263

6364
[tool.coverage.paths]
64-
skyhook_agent = ["src/skyhook_agent", "*/skyhook-agent/src/skyhook_agent"]
65-
tests = ["tests", "*/skyhook-agent/tests"]
65+
skyhook_agent = ["src/skyhook_agent", "*/src/skyhook_agent"]
66+
tests = ["tests", "*/tests"]
6667

6768
[tool.coverage.report]
6869
exclude_lines = [
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33
#
4-
__version__ = "0.0.6"
4+
__version__ = "0.0.0"
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)