Make script to building image to Docker Hub

This commit is contained in:
Jesus 2023-09-25 09:51:35 +08:00
parent aa77d14f26
commit 288e0b0b24
No known key found for this signature in database
GPG Key ID: E607CE7149F4D71C
6 changed files with 169 additions and 23 deletions

View File

@ -1,2 +1,4 @@
* *
!bootstrap.tar.gz !bootstrap.tar.gz
!hyperbola-bootstrap-x86_64.tar.gz
!hyperbola-bootstrap-i686.tar.gz

View File

@ -1,15 +0,0 @@
FROM scratch
ADD bootstrap.tar.gz /
RUN ldconfig
RUN pacman-key --init && pacman-key --populate archlinux hyperbola
RUN pacman -Su --noconfirm --noprogressbar --quiet gzip awk
RUN pacman -Scc --noconfirm && paccache -r -k0
ENV LANG en_US.UTF-8
CMD ["/bin/bash"]

View File

@ -1,6 +1,6 @@
FROM scratch FROM scratch
ADD bootstrap.tar.gz / ADD hyperbola-bootstrap-i686.tar.gz /
RUN ldconfig RUN ldconfig

15
Dockerfile.amd64 Normal file
View File

@ -0,0 +1,15 @@
FROM scratch
ADD hyperbola-bootstrap-x86_64.tar.gz /
RUN ldconfig
RUN pacman-key --init && pacman-key --populate hyperbola
RUN pacman -Suu --noconfirm --noprogressbar
RUN pacman -Scc --noconfirm
ENV LANG en_US.UTF-8
CMD ["/bin/bash"]

View File

@ -12,14 +12,24 @@ Usage
- `git clone` this repository and move to directory where you have cloned it. - `git clone` this repository and move to directory where you have cloned it.
- Generate bootstrap image with [hyperbola-bootstrap](https://git.sr.ht/~heckyel/hyperbola-bootstrap) or download file from [archive.fridu.us](https://archive.fridu.us/hyperbola/iso/) - Generate bootstrap image with [hyperbola-bootstrap](https://git.sr.ht/~heckyel/hyperbola-bootstrap) or download file from [archive.fridu.us](https://archive.fridu.us/hyperbola/iso/)
- Rename hyperbola-bootstrap to bootstrap.tar.gz image - Make sure you have `hyperbola-bootstrap-i686.tar.gz` or `hyperbola-bootstrap-x86_64.tar.gz` in the directory.
mv -T hyperbola-bootstrap-x86_64.tar.gz bootstrap.tar.gz
- Make sure you have my `Dockerfile`, `.dockerignore` and the `bootstrap.tar.gz` in one directory.
- Build the image with a command like: - Build the image with a command like:
docker build --tag hyperbola:v0.4.3 -f Dockerfile-Hyperbola-v04 . docker build --no-cache --platform=linux/amd64 --tag rusian/hyperbola:v0.4.3-amd64 -f Dockerfile.amd64 .
docker build --no-cache --platform=linux/386 --tag rusian/hyperbola:v0.4.3-386 -f Dockerfile.386 .
- Optionally, you can use the provided **make-hyperbola.sh** script to build and push the images to Docker Hub, you can provide your username and password like environment variables or directly in the script.
export DOCKER_USERNAME="your_username"
export DOCKER_PASSWORD="your_password"
- Run **make-hyperbola.sh**
bash make-hyperbola.sh
- Clean environment variables
unset DOCKER_USERNAME && unset DOCKER_PASSWORD
License License
======= =======

134
make-hyperbola.sh Normal file
View File

@ -0,0 +1,134 @@
#!/bin/bash
# Default tag
default_tag="v0.4.3"
default_release="2023.09.25"
latest_tag="latest"
# Function to display help
show_help() {
echo "Usage: $0 [-t TAG] [-h]"
echo " -t TAG Specify the tag (default: $default_tag)"
echo " -r VERSION Specify the release version (default: $default_release)"
echo " -h Show this help"
}
# Process command line options
while getopts ":t:h" opt; do
case $opt in
r)
release="$OPTARG"
;;
t)
tag="$OPTARG"
;;
h)
show_help && exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
show_help && exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
show_help && exit 1
;;
esac
done
# Use default tag if not specified
tag="${tag:-$default_tag}"
# Use default release version if not specified
release="${release:-$default_release}"
# Working directory
script_dir="$(cd "$(dirname "$0")" && pwd)"
# Check if files exist in the current directory
if [[ ! -e "$script_dir/hyperbola-bootstrap-i686.tar.gz" || ! -e "$script_dir/hyperbola-bootstrap-x86_64.tar.gz" ]]; then
echo "Downloading files for version $release..."
# Download files from the provided link
wget -P "$script_dir" "https://archive.fridu.us/hyperbola/iso/$release/hyperbola-bootstrap-i686.tar.gz"
wget -P "$script_dir" "https://archive.fridu.us/hyperbola/iso/$release/hyperbola-bootstrap-x86_64.tar.gz"
echo "Files downloaded successfully for version $release."
else
echo "Files already exist in the current directory. No download is required."
fi
# Logout docker
docker logout > /dev/null 2>&1
# Prompt user for credentials
if [[ -z "$DOCKER_USERNAME" ]]; then
read -p "Enter your Docker Hub username: " DOCKER_USERNAME
fi
if [[ -z "$DOCKER_PASSWORD" ]]; then
read -sp "Enter your Docker Hub password: " DOCKER_PASSWORD
echo
fi
# Log in to Docker Hub
echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_USERNAME" --password-stdin
# Build the images
docker build --no-cache --platform=linux/amd64 --tag "$DOCKER_USERNAME/hyperbola:$tag-amd64" -f Dockerfile.amd64 .
docker build --no-cache --platform=linux/386 --tag "$DOCKER_USERNAME/hyperbola:$tag-386" -f Dockerfile.386 .
# Push the images to Docker Hub
docker push "$DOCKER_USERNAME/hyperbola:$tag-amd64"
docker push "$DOCKER_USERNAME/hyperbola:$tag-386"
# Create and push the manifest for the specified tag
docker manifest create "$DOCKER_USERNAME/hyperbola:$tag" \
--amend "$DOCKER_USERNAME/hyperbola:$tag-amd64" \
--amend "$DOCKER_USERNAME/hyperbola:$tag-386"
docker manifest push "$DOCKER_USERNAME/hyperbola:$tag"
# Create and push the manifest for "latest"
docker manifest create "$DOCKER_USERNAME/hyperbola:$latest_tag" \
--amend "$DOCKER_USERNAME/hyperbola:$tag-amd64" \
--amend "$DOCKER_USERNAME/hyperbola:$tag-386"
docker manifest push "$DOCKER_USERNAME/hyperbola:$latest_tag"
# Get the token
token=$(curl -s -H 'Content-Type: application/json' -X POST -H 'Content-Type: application/json' -d "{\"username\":\"$DOCKER_USERNAME\", \"password\":\"$DOCKER_PASSWORD\"}" "https://hub.docker.com/v2/users/login/" | jq -r .token)
# Function to perform a curl with retries
function curl_with_retry() {
local url="$1"
local retries=3 # Number of retries
local delay=5 # Delay between retries in seconds
for ((i = 0; i < retries; i++)); do
response=$(curl -s -X DELETE -H "Authorization: JWT $token" "$url")
if [ $? -eq 0 ]; then
echo "$response"
return 0
else
echo "Attempt $((i+1)) failed. Retrying in $delay seconds..."
sleep $delay
fi
done
echo "All attempts failed. Curl command with URL '$url' has failed."
return 1
}
# Delete tags on Docker Hub
if curl_with_retry "https://hub.docker.com/v2/repositories/$DOCKER_USERNAME/hyperbola/tags/$tag-amd64/"; then
echo "Tag $tag-amd64 deleted successfully."
else
echo "Failed to delete tag $tag-amd64."
fi
if curl_with_retry "https://hub.docker.com/v2/repositories/$DOCKER_USERNAME/hyperbola/tags/$tag-386/"; then
echo "Tag $tag-386 deleted successfully."
else
echo "Failed to delete tag $tag-386."
fi