Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.git/
.github/
.earthlyignore
Earthfile
earthly-golem/
node_modules/
output/
coverage/
*.log
.env
.env.*
docker-bake.hcl
29 changes: 29 additions & 0 deletions .github/workflows/publish-docker-bake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Publish
on:
push:
branches:
- main
- master
- dev
pull_request:
branches: ['*']
release:
types:
- created
tags:
- 'v[0-9]+'
- 'v[0-9]+.[0-9]+'
- 'v[0-9]+.[0-9]+.[0-9]+'

jobs:
build:
uses: zondax/_workflows/.github/workflows/_publish-docker-bake.yaml@main
with:
registry: dockerhub
enable_remote_builder: true
enable_signing: true
enable_provenance: true
secrets:
DOCKERHUB_USER: ${{ secrets.DOCKERHUB_USER }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
Comment thread Fixed
REMOTE_BUILD_KIT: tcp://buildkit.int-dev.zondax.io:8372
Comment on lines +20 to +29

Check warning

Code scanning / CodeQL

Workflow does not contain permissions Medium

Actions job or workflow does not limit the permissions of the GITHUB_TOKEN. Consider setting an explicit permissions block, using the following as a minimal starting point: {}

Copilot Autofix

AI about 1 month ago

To fix the problem, explicitly declare a permissions block that grants only the minimal required scopes to GITHUB_TOKEN. For a publish-to-registry workflow that doesn’t obviously need to write to the repository (no checkouts, tagging, or releases in the shown snippet), a conservative starting point is repository contents read-only plus packages write (so the token can push images if needed). This should be declared at the workflow root so it applies to the reused job unless that job overrides it.

Concretely, in .github/workflows/publish-docker-bake.yml, insert a root-level permissions section between the on: block and the jobs: block (around line 17). Use:

permissions:
  contents: read
  packages: write

This keeps repository contents read-only while allowing package publishing (e.g., to GitHub Container Registry). If the reusable workflow later proves to need more/less, this block can be adjusted, but this is a safe, least-privilege default that resolves the CodeQL finding without changing any functional behavior for Docker Hub publishing (which uses explicit secrets, not GITHUB_TOKEN).

Suggested changeset 1
.github/workflows/publish-docker-bake.yml

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/.github/workflows/publish-docker-bake.yml b/.github/workflows/publish-docker-bake.yml
--- a/.github/workflows/publish-docker-bake.yml
+++ b/.github/workflows/publish-docker-bake.yml
@@ -15,6 +15,10 @@
       - 'v[0-9]+.[0-9]+'
       - 'v[0-9]+.[0-9]+.[0-9]+'
 
+permissions:
+  contents: read
+  packages: write
+
 jobs:
   build:
     uses: zondax/_workflows/.github/workflows/_publish-docker-bake.yaml@main
EOF
@@ -15,6 +15,10 @@
- 'v[0-9]+.[0-9]+'
- 'v[0-9]+.[0-9]+.[0-9]+'

permissions:
contents: read
packages: write

jobs:
build:
uses: zondax/_workflows/.github/workflows/_publish-docker-bake.yaml@main
Copilot is powered by AI and may make mistakes. Always verify output.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ test:
earthly:
earthly +all

## Docker Bake:
docker-bake: ## Build production image locally
./scripts/docker-bake.sh

docker-bake-push: ## Build and push with all flex tags
./scripts/docker-bake.sh --push

docker-bake-debug: ## Build with verbose output
BUILDX_NO_DEFAULT_ATTESTATIONS=1 ./scripts/docker-bake.sh --progress=plain

docker-bash:
docker run --platform linux/amd64 -it zondax/${APP_NAME}:latest /bin/sh

Expand Down
26 changes: 26 additions & 0 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# syntax=docker/dockerfile:1
FROM golang:1.24.1-alpine AS builder

RUN apk update && apk --no-cache --update add build-base bash git make

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN make build

FROM alpine:3.17 AS production

RUN apk update && apk --no-cache --update add ca-certificates

RUN addgroup --system --gid 1001 zondax
RUN adduser --system --uid 1001 zondax
USER zondax

COPY --chown=zondax:zondax --from=builder /app/output /zondax/bin

EXPOSE 9090

CMD ["/zondax/bin/golem", "start", "-c", "/zondax/config/golem.yaml"]
52 changes: 52 additions & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
variable "BASE_NAME" {
default = "golem"
}

variable "REGISTRY" {
default = "zondax"
}

variable "GIT_SHORT_HASH" {
default = ""
}

variable "GIT_BRANCH" {
default = ""
}

variable "GIT_TAG" {
default = ""
}

variable "GIT_COMMIT_TIMESTAMP" {
default = ""
}

function "flex_tags" {
params = [name]
result = compact([
"${REGISTRY}/${name}:latest",
notequal(GIT_SHORT_HASH, "") ? "${REGISTRY}/${name}:${GIT_SHORT_HASH}" : "",
notequal(GIT_BRANCH, "") ? "${REGISTRY}/${name}:${GIT_BRANCH}" : "",
notequal(GIT_COMMIT_TIMESTAMP, "") ? "${REGISTRY}/${name}:T${GIT_COMMIT_TIMESTAMP}" : "",
notequal(GIT_TAG, "") ? "${REGISTRY}/${name}:${GIT_TAG}" : "",
])
}

group "default" {
targets = ["production"]
}

target "builder" {
context = "."
dockerfile = "build/Dockerfile"
target = "builder"
tags = ["${REGISTRY}/${BASE_NAME}:builder"]
}

target "production" {
context = "."
dockerfile = "build/Dockerfile"
tags = flex_tags(BASE_NAME)
platforms = ["linux/amd64"]
}
59 changes: 59 additions & 0 deletions scripts/docker-bake.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash
set -euo pipefail

detect_git_metadata() {
if [ -z "${GIT_SHORT_HASH:-}" ]; then
export GIT_SHORT_HASH=$(git rev-parse --short HEAD 2>/dev/null || echo "")
fi

if [ -z "${GIT_BRANCH:-}" ]; then
export GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null | tr '/' '-' || echo "")
fi

if [ -z "${GIT_TAG:-}" ]; then
export GIT_TAG=$(git describe --tags --exact-match HEAD 2>/dev/null || echo "")
fi

if [ -z "${GIT_COMMIT_TIMESTAMP:-}" ]; then
local unix_ts
unix_ts=$(git log -1 --format=%ct 2>/dev/null || echo "")
if [ -n "$unix_ts" ]; then
if date --version >/dev/null 2>&1; then
export GIT_COMMIT_TIMESTAMP=$(date -d "@${unix_ts}" +"%Y%m%d%H%M%S")
else
export GIT_COMMIT_TIMESTAMP=$(date -r "${unix_ts}" +"%Y%m%d%H%M%S")
fi
fi
fi
}

print_config() {
echo "Git metadata detected:"
echo " GIT_SHORT_HASH: ${GIT_SHORT_HASH:-<not set>}"
echo " GIT_BRANCH: ${GIT_BRANCH:-<not set>}"
echo " GIT_TAG: ${GIT_TAG:-<not set>}"
echo " GIT_COMMIT_TIMESTAMP: ${GIT_COMMIT_TIMESTAMP:-<not set>}"
}

main() {
local push_flag=""
local extra_args=()

for arg in "$@"; do
case "$arg" in
--push) push_flag="--push" ;;
--print) detect_git_metadata; print_config; exit 0 ;;
*) extra_args+=("$arg") ;;
esac
done

detect_git_metadata
print_config

export DOCKER_BUILDKIT=1
export BUILDX_NO_DEFAULT_ATTESTATIONS=1

docker buildx bake $push_flag "${extra_args[@]+"${extra_args[@]}"}"
}

main "$@"
Loading