diff --git a/.github/workflows/build-push-container.yml b/.github/workflows/build-push-container.yml index c1b2fb041..1f2f0cc92 100644 --- a/.github/workflows/build-push-container.yml +++ b/.github/workflows/build-push-container.yml @@ -28,7 +28,23 @@ jobs: with: submodules: true ref: ${{ github.event.pull_request.head.ref || github.ref }} - + + - name: Cache Cargo registry + uses: actions/cache@v3 + with: + path: ~/.cargo/registry + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-cargo-registry- + + - name: Cache Cargo index + uses: actions/cache@v3 + with: + path: ~/.cargo/git + key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-cargo-index- + - name: Login to GHCR uses: docker/login-action@v3 with: @@ -72,4 +88,4 @@ jobs: - name: Build and push container manifest run: | - ./scripts/movement/manifest ${{ inputs.container_name }} + ./scripts/docker/build-push-manifest ${{ inputs.container_name }} diff --git a/scripts/docker/build-push-manifest b/scripts/docker/build-push-manifest new file mode 100755 index 000000000..6070c0652 --- /dev/null +++ b/scripts/docker/build-push-manifest @@ -0,0 +1,124 @@ +#!/bin/bash +set -e + +# Turn on bash safety options: fail on error, variable unset and error in piped process +set -eou pipefail + +# Initialize parameters +debug=false +container_name=movement-full-node +repository=ghcr.io/movementlabsxyz + +# Example: add "d" as a boolean toggle +while getopts "dn:r:" opt; do + case "$opt" in + d) + debug=true + ;; + n) + container_name=$OPTARG + ;; + r) + repository=$OPTARG + ;; + *) + echo "Usage: $0 [-d] -n -r " + exit 1 + ;; + esac +done + +if [ "$debug" = true ]; then + echo "Debug mode is on" + echo "CONTAINER_NAME: $container_name" + echo "REPOSITORY: $repository" +fi + +# Get dockerfile path +git_root=$(git rev-parse --show-toplevel) +dockerfile_path=${git_root}/docker/build/${container_name}/Dockerfile + +if [ "$debug" = true ]; then + echo "GIT_ROOT: $git_root" + echo "DOCKERFILE: $dockerfile_path" +fi + +# Get git info +commit_hash=$(git rev-parse HEAD | cut -c1-7) +branch_name=$(git rev-parse --abbrev-ref HEAD) +sanitized_branch_name=${branch_name//\//.} +is_tag=false + +# if git describe --exact-match --tags HEAD >/dev/null 2>&1; then +# is_tag="true" +# fi + +if [ "$debug" = true ]; then + echo "COMMIT_HASH: $commit_hash" + echo "BRANCH_NAME: $branch_name" + echo "SANITIZED_BRANCH_NAME: $sanitized_branch_name" + echo "IS_TAG: $is_tag" +fi + +# Get the machine hardware name +arch=$(uname -m) + +# Determine the platform name suffix based on the architecture +case "$arch" in + x86_64) + platform_suffix="-amd64" + ;; + aarch64) + platform_suffix="-arm64" + ;; + *) + echo "Unsupported architecture: $arch" + exit 1 + ;; +esac + +if [ "$debug" = true ]; then + echo "ARCH: $arch" + echo "PLATFORM_SUFFIX: $platform_suffix" +fi + +# Get application version +application_version=$(grep -m1 '^version\s*=' Cargo.toml | sed 's/^version\s*=\s*"\(.*\)"/\1/') + +if [ "$debug" = true ]; then + echo "APPLICATION_VERSION: $application_version" +fi + +# Generate image tags +container_tags=() +container_tag_commit="${repository}/${container_name}:${commit_hash}" +container_tag_version="${repository}/${container_name}:${application_version}" +container_tag_branch="${repository}/${container_name}:${application_version}-${sanitized_branch_name}" + +if [ "$is_tag" = true ]; then + # If it's a tag, use the application version + container_tags+=("$container_tag_version") +else + # If it's not a tag, use the sanitized branch name + container_tags+=("$container_tag_branch") +fi + +container_tags+=("$container_tag_commit") + +if [ "$debug" = true ]; then + for tag in "${container_tags[@]}"; do + echo "CONTAINER_TAG: $tag" + done +fi + +# # build and tag the docker image +docker buildx imagetools create \ + $container_tag_commit-amd64 \ + $container_tag_commit-arm64 \ + $(for tag in "${container_tags[@]}"; do echo -n " --tag $tag"; done) + +# Push the Docker images +for tag in "${container_tags[@]}"; do + docker push $tag +done + \ No newline at end of file