ghcr
is a CLI tool to build and publish Docker images to GitHub Container Registry (GHCR). This tool also supports logging in to GHCR using Docker credentials securely via environment variables.
- Modularized Rust code for easy maintenance and extensibility
- Robust error handling and informative logging
- Modern CI/CD with matrix Rust testing and Docker build/push
- Comprehensive tests for config, commands, and integration scenarios
- Build Docker Image: Build a Docker image using the provided
Dockerfile
and context. - Push Docker Image: Push the built Docker image to GitHub Container Registry (GHCR).
- Login to GHCR: Login to GHCR using your username and a token stored in an environment variable.
You can use this project as a GitHub Action to build, login, and push Docker images to GHCR in your CI workflows.
Basic Example:
- name: Build and push Docker image
uses: khulnasoft-lab/ghcr@v1
with:
command: build
config: ./ghcr.toml
token: ${{ secrets.GHCR_TOKEN }}
Advanced Multi-Step Example:
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Login to GHCR
uses: khulnasoft-lab/ghcr@v1
with:
command: login
config: ./ghcr.toml
token: ${{ secrets.GHCR_TOKEN }}
- name: Build Docker image
uses: khulnasoft-lab/ghcr@v1
with:
command: build
config: ./ghcr.toml
- name: Push Docker image
uses: khulnasoft-lab/ghcr@v1
with:
command: push
config: ./ghcr.toml
token: ${{ secrets.GHCR_TOKEN }}
- Pin to a specific version, e.g.,
@v1
, for stability in your workflows. - See the action.yml for all available inputs.
To use a stable version of this action, pin to a release tag (e.g., @v1
or @v1.0.0
).
To publish a new release:
- Commit all changes to
main
. - Tag a release:
git tag v1.0.0 git push --tags
- The action will appear on the GitHub Marketplace and can be referenced as
khulnasoft-lab/ghcr@v1.0.0
in workflows.
- Docker: You need Docker installed on your machine.
- GitHub Account: Required for authentication and pushing to GHCR.
- Rust: This tool is written in Rust, and you'll need to have Rust installed to build it locally. However, a pre-built binary can also be used.
You can download the pre-built binary from the GitHub releases page.
- Clone the repository:
git clone https://github.com/khulnasoft-lab/ghcr.git cd ghcr
- Build the binary:
cargo build --release
- The binary will be in
target/release/ghcr
.
Create a ghcr.toml
file in your project root. Example:
[package]
name = "ghcr"
version = "0.1.0"
description = "Build and publish Docker images to GitHub Container Registry"
repository = "https://github.com/khulnasoft-lab/ghcr"
[image]
tag = "ghcr.io/khulnasoft-lab/ghcr:latest"
context = "."
[auth]
username = "khulnasoft-lab"
token_env = "GHCR_TOKEN" # Set this environment variable with your GHCR token
RUST_LOG=info ./target/release/ghcr build
RUST_LOG=info ./target/release/ghcr push
export GHCR_TOKEN=your_token_here
RUST_LOG=info ./target/release/ghcr login
A recommended .dockerignore
is included to keep your Docker build context clean and secure. Edit as needed for your project.
- Docker Daemon Not Running:
- Error:
Cannot connect to the Docker daemon...
- Solution: Ensure Docker is installed and running (
docker info
).
- Error:
- Token Environment Variable Not Set:
- Error:
Token environment variable 'GHCR_TOKEN' not set
- Solution: Export the variable before running
login
.
- Error:
- Invalid TOML Format:
- Error:
Invalid TOML format: ...
- Solution: Check your
ghcr.toml
for syntax errors.
- Error:
- Permission Issues:
- Ensure you have permission to access the Docker socket and to push images to GHCR.
- Use environment variables for secrets (never hardcode tokens).
- Use the provided
.dockerignore
to avoid leaking sensitive files into your Docker images. - Run containers as a non-root user (see the provided Dockerfile).
- Add unit and integration tests for command logic and configuration parsing (see
src/main.rs
). - Example test frameworks:
assert_cmd
,tempfile
.
- Add workflows in
.github/workflows/
for linting, testing, and building Docker images. - Example: GitHub Actions for Rust and Docker.
MIT OR Apache-2.0