From 7e671cb7ca456f86a501ab2713f2d50200466205 Mon Sep 17 00:00:00 2001 From: unkin <> Date: Sun, 1 Dec 2024 12:28:47 +1100 Subject: [PATCH] Initial commit --- .gitea/workflows/build.yaml | 26 ++++++++++ .gitea/workflows/deploy.yaml | 39 +++++++++++++++ Dockerfile | 18 +++++++ Makefile | 34 +++++++++++++ README.md | 95 ++++++++++++++++++++++++++++++++++++ 5 files changed, 212 insertions(+) create mode 100644 .gitea/workflows/build.yaml create mode 100644 .gitea/workflows/deploy.yaml create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 README.md diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml new file mode 100644 index 0000000..1ceda77 --- /dev/null +++ b/.gitea/workflows/build.yaml @@ -0,0 +1,26 @@ +name: Build + +on: + pull_request: + +jobs: + build: + runs-on: almalinux-8 + container: + image: git.query.consul/unkin/almalinux8:latest + options: --privileged + + steps: + - name: Set up environment + run: | + dnf install -y yum-utils + yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo + dnf module enable -y nodejs:20 + dnf install -y docker-ce-cli make bash git nodejs + + - name: Checkout code + uses: actions/checkout@v3 + + - name: Build Docker Image + run: | + make build diff --git a/.gitea/workflows/deploy.yaml b/.gitea/workflows/deploy.yaml new file mode 100644 index 0000000..35160e3 --- /dev/null +++ b/.gitea/workflows/deploy.yaml @@ -0,0 +1,39 @@ +name: Deploy + +on: + push: + branches: + - master + +jobs: + build: + runs-on: almalinux-8 + container: + image: git.query.consul/unkin/almalinux8:latest + options: --privileged + + steps: + - name: Set up environment + run: | + dnf install -y yum-utils + yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo + dnf module enable -y nodejs:20 + dnf install -y docker-ce-cli make bash git nodejs + + - name: Checkout code + uses: actions/checkout@v3 + + - name: Build Docker Image + run: | + make build + + - name: Log in to Docker + env: + UPLOAD_USER: ${{ secrets.UPLOAD_USER }} + UPLOAD_PASS: ${{ secrets.UPLOAD_PASS }} + run: | + echo "$UPLOAD_PASS" | docker login --username=$UPLOAD_USER --password-stdin git.query.consul + + - name: Push Docker Image + run: | + make push diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9b6b199 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# Start with the AlmaLinux 8.10 base image +FROM git.query.consul/unkin/almalinux8:latest + +# Clean and update the repository cache +RUN dnf clean all && \ + dnf makecache + +# Install nodejs:20 for actions +RUN dnf module enable -y nodejs:20 && \ + dnf install -y nodejs + +# Install packages +RUN dnf groupinstall -y 'Development Tools' && \ + dnf install -y make cmake gcc gcc-c++ rpm rpmdevtools wget + +# Cleanup +RUN dnf clean all && \ + rm -rf /var/cache/dnf diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..291abb6 --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +# Get the current Git commit hash +GIT_COMMIT := $(shell git rev-parse --short HEAD) + +# Get the current date in YYYYMMDD format +DATE_TAG := $(shell date +%Y%m%d) + +# Set the Docker image name and repository information +IMAGE_NAME := almalinux8-template +REGISTRY := git.query.consul +OWNER := unkin + +# Build the Docker image (without tags) +build: + docker build --network=host -t $(REGISTRY)/$(OWNER)/$(IMAGE_NAME) . + +# Tag the Docker image with the Git commit hash, the date, and 'latest' +tag: + docker tag $(REGISTRY)/$(OWNER)/$(IMAGE_NAME) $(REGISTRY)/$(OWNER)/$(IMAGE_NAME):$(GIT_COMMIT) + docker tag $(REGISTRY)/$(OWNER)/$(IMAGE_NAME) $(REGISTRY)/$(OWNER)/$(IMAGE_NAME):$(DATE_TAG) + docker tag $(REGISTRY)/$(OWNER)/$(IMAGE_NAME) $(REGISTRY)/$(OWNER)/$(IMAGE_NAME):latest + +# Push the Docker image to a repository with all tags +push: tag + echo "template repo, not pushing" + #docker push $(REGISTRY)/$(OWNER)/$(IMAGE_NAME):$(GIT_COMMIT) + #docker push $(REGISTRY)/$(OWNER)/$(IMAGE_NAME):$(DATE_TAG) + #docker push $(REGISTRY)/$(OWNER)/$(IMAGE_NAME):latest + +# Clean up dangling Docker images +clean: + docker image prune -f + +# Default target +default: build diff --git a/README.md b/README.md new file mode 100644 index 0000000..de7908e --- /dev/null +++ b/README.md @@ -0,0 +1,95 @@ +# Docker Image Build: Almalinux Template + +This project provides a reproducible Docker image build process for `almalinux:8.10`, with custom YUM repository configurations and package installations. The build is automated using a `Makefile` and managed via CI tasks to ensure consistent and reliable Docker image builds. + +## Makefile Overview + +The `Makefile` provides several targets to automate building, tagging, and pushing Docker images. The Docker image is tagged with the current Git commit hash, the current date, and a `latest` tag for easy reference. + +## Makefile Targets + +### Build the Docker Image + +To build the Docker image without tags: + +```bash +make build +``` + +This will use the `docker build` command with `--network=host` to build the image with the name `git.query.consul/unkin/almalinux8-template`. + +### Tag the Docker Image + +To tag the Docker image with the Git commit hash, the current date, and a `latest` tag: + +```bash +make tag +``` + +This will add three tags to the image: +- `git.query.consul/unkin/almalinux8-template:` +- `git.query.consul/unkin/almalinux8-template:` +- `git.query.consul/unkin/almalinux8-template:latest` + +### Push the Docker Image + +To push the tagged Docker images to the registry: + +```bash +make push +``` + +> **Note**: The `push` command is currently commented out for safety. To enable pushing, remove the comment (`#`) from the `docker push` lines in the `push` target. + +### Clean Up + +To clean up any dangling Docker images: + +```bash +make clean +``` + +This will remove any dangling (unused) Docker images using `docker image prune -f`. + +### Default Target + +The default target is `build`. So running: + +```bash +make +``` + +is equivalent to running `make build`. + +## Development and Testing + +To test or develop with this `Makefile`, follow these steps: + +1. **Clone the repository** and navigate to the project directory: + ```bash + git clone https://git.query.consul/unkin/docker-template.git + cd docker-template + ``` + +2. **Build the Docker image** using the `build` target: + ```bash + make build + ``` + +3. **Tag the image** with the commit hash, date, and latest tag: + ```bash + make tag + ``` + +4. **(Optional)** Modify the `push` target to enable pushing the image to your Docker registry: + Uncomment the lines in the `push` target: + ```makefile + docker push $(REGISTRY)/$(OWNER)/$(IMAGE_NAME):$(GIT_COMMIT) + docker push $(REGISTRY)/$(OWNER)/$(IMAGE_NAME):$(DATE_TAG) + docker push $(REGISTRY)/$(OWNER)/$(IMAGE_NAME):latest + ``` + +5. **Test your changes** locally by running the `clean` target: + ```bash + make clean + ```