From 4bdb91ee96159c487658d84ac175fe2006e68945 Mon Sep 17 00:00:00 2001 From: Ceyhun Onur Date: Mon, 8 Apr 2024 19:43:25 +0300 Subject: [PATCH] Fix docker push test (#1138) * refactor docker yml and scripts * enable pr for test * fix path * fix path * rename img id * try simpler name * use -a * test tag * remove test steps * remove avalanchego from image id --- .github/workflows/publish_docker.yml | 10 ++++--- .github/workflows/publish_docker_image.sh | 30 +++++++++++++++++++++ scripts/build_docker_image.sh | 21 +++++++++++++++ scripts/build_image.sh | 33 ----------------------- scripts/constants.sh | 6 ++--- 5 files changed, 60 insertions(+), 40 deletions(-) create mode 100755 .github/workflows/publish_docker_image.sh create mode 100755 scripts/build_docker_image.sh delete mode 100755 scripts/build_image.sh diff --git a/.github/workflows/publish_docker.yml b/.github/workflows/publish_docker.yml index 8a5dfde727..b8f5576b92 100644 --- a/.github/workflows/publish_docker.yml +++ b/.github/workflows/publish_docker.yml @@ -14,7 +14,9 @@ jobs: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v4 - - run: echo ${{ secrets.docker_pass }} | docker login --username ${{ secrets.docker_username }} --password-stdin - shell: bash - - run: CURRENT_BRANCH=${{ github.head_ref || github.ref_name }} PUSH_DOCKER_IMAGE=true ./scripts/build_image.sh - shell: bash + - name: Publish image to Dockerhub + env: + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} + DOCKER_PASS: ${{ secrets.DOCKER_PASS }} + DOCKER_REPO: "avaplatform/subnet-evm" + run: .github/workflows/publish_docker_image.sh diff --git a/.github/workflows/publish_docker_image.sh b/.github/workflows/publish_docker_image.sh new file mode 100755 index 0000000000..b85c26409f --- /dev/null +++ b/.github/workflows/publish_docker_image.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +# If this is not a trusted build (Docker Credentials are not set) +if [[ -z "$DOCKER_USERNAME" ]]; then + exit 0; +fi + +# Avalanche root directory +SUBNET_EVM_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd ../.. && pwd ) + +# Load the versions +source "$SUBNET_EVM_PATH"/scripts/versions.sh + +# Buld the docker image +source "$SUBNET_EVM_PATH"/scripts/build_docker_image.sh + +if [[ $CURRENT_BRANCH == "master" ]]; then + echo "Tagging current image as $DOCKERHUB_REPO:latest" + docker tag "$DOCKERHUB_REPO:$BUILD_IMAGE_ID" "$DOCKERHUB_REPO:latest" +fi + +echo "Pushing $DOCKERHUB_REPO:$BUILD_IMAGE_ID" + +echo "$DOCKER_PASS" | docker login --username "$DOCKER_USERNAME" --password-stdin + +docker push -a "$DOCKERHUB_REPO" diff --git a/scripts/build_docker_image.sh b/scripts/build_docker_image.sh new file mode 100755 index 0000000000..8124ea9cdb --- /dev/null +++ b/scripts/build_docker_image.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# Directory above this script +SUBNET_EVM_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd ) + +# Load the constants +source "$SUBNET_EVM_PATH"/scripts/constants.sh + +# Load the versions +source "$SUBNET_EVM_PATH"/scripts/versions.sh + +# WARNING: this will use the most recent commit even if there are un-committed changes present +BUILD_IMAGE_ID=${BUILD_IMAGE_ID:-"${CURRENT_BRANCH}"} + +echo "Building Docker Image: $DOCKERHUB_REPO:$BUILD_IMAGE_ID based of AvalancheGo@$AVALANCHE_VERSION" +docker build -t "$DOCKERHUB_REPO:$BUILD_IMAGE_ID" "$SUBNET_EVM_PATH" -f "$SUBNET_EVM_PATH/Dockerfile" \ + --build-arg AVALANCHE_VERSION="$AVALANCHE_VERSION" \ + --build-arg SUBNET_EVM_COMMIT="$SUBNET_EVM_COMMIT" \ + --build-arg CURRENT_BRANCH="$CURRENT_BRANCH" diff --git a/scripts/build_image.sh b/scripts/build_image.sh deleted file mode 100755 index fa1b74f87d..0000000000 --- a/scripts/build_image.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit -set -o nounset -set -o pipefail - -# Avalanche root directory -SUBNET_EVM_PATH=$( - cd "$(dirname "${BASH_SOURCE[0]}")" - cd .. && pwd -) - -# Load the versions -source "$SUBNET_EVM_PATH"/scripts/versions.sh - -# Load the constants -source "$SUBNET_EVM_PATH"/scripts/constants.sh - -BUILD_IMAGE_ID=${BUILD_IMAGE_ID:-"avalanchego-${AVALANCHE_VERSION}-subnet-evm-${CURRENT_BRANCH}"} - -echo "Building Docker Image: $DOCKERHUB_REPO:$BUILD_IMAGE_ID based of $AVALANCHE_VERSION" -docker build -t "$DOCKERHUB_REPO:$BUILD_IMAGE_ID" "$SUBNET_EVM_PATH" -f "$SUBNET_EVM_PATH/Dockerfile" \ - --build-arg AVALANCHE_VERSION="$AVALANCHE_VERSION" \ - --build-arg SUBNET_EVM_COMMIT="$SUBNET_EVM_COMMIT" \ - --build-arg CURRENT_BRANCH="$CURRENT_BRANCH" - -if [[ ${PUSH_DOCKER_IMAGE:-""} == "true" ]]; then - if [[ $CURRENT_BRANCH == "master" ]]; then - echo "Tagging current image as $DOCKERHUB_REPO:latest" - docker tag "$DOCKERHUB_REPO:$BUILD_IMAGE_ID" "$DOCKERHUB_REPO:latest" - fi - docker push "$DOCKERHUB_REPO:$BUILD_IMAGE_ID" -fi diff --git a/scripts/constants.sh b/scripts/constants.sh index 32d9699254..0838e45552 100644 --- a/scripts/constants.sh +++ b/scripts/constants.sh @@ -9,13 +9,14 @@ set -euo pipefail GOPATH="$(go env GOPATH)" # Avalabs docker hub -DOCKERHUB_REPO="avaplatform/subnet-evm" +# avaplatform/avalanchego - defaults to local as to avoid unintentional pushes +# You should probably set it - export DOCKER_REPO='avaplatform/subnet-evm' +DOCKERHUB_REPO=${DOCKER_REPO:-"subnet-evm"} # if this isn't a git repository (say building from a release), don't set our git constants. if [ ! -d .git ]; then CURRENT_BRANCH="" SUBNET_EVM_COMMIT="" - SUBNET_EVM_COMMIT_ID="" else # Current branch CURRENT_BRANCH=${CURRENT_BRANCH:-$(git describe --tags --exact-match 2>/dev/null || git symbolic-ref -q --short HEAD || git rev-parse --short HEAD || :)} @@ -25,7 +26,6 @@ else # Use an abbreviated version of the full commit to tag the image. # WARNING: this will use the most recent commit even if there are un-committed changes present SUBNET_EVM_COMMIT="$(git --git-dir="$SUBNET_EVM_PATH/.git" rev-parse HEAD || :)" - SUBNET_EVM_COMMIT_ID="${SUBNET_EVM_COMMIT::8}" fi echo "Using branch: ${CURRENT_BRANCH}"