diff --git a/.github/workflows/publish_docker_images.yml b/.github/workflows/publish_docker_images.yml index 3686670aa..efa92610e 100644 --- a/.github/workflows/publish_docker_images.yml +++ b/.github/workflows/publish_docker_images.yml @@ -9,101 +9,89 @@ on: - main jobs: - docker: + publish_osdk_image: runs-on: ubuntu-latest steps: - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 - with: - driver: docker - - - name: Login to Docker Hub - uses: docker/login-action@v2 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - uses: actions/checkout@v4 - - - name: Check if Docker image exists - id: check-images + - name: Prepare for Docker build and push + id: prepare-for-docker-build-and-push run: | - IMAGE_TAG=$(cat DOCKER_IMAGE_VERSION) - if docker manifest inspect asterinas/osdk:${IMAGE_TAG} > /dev/null 2>&1; then - echo "osdk_image_exists=true" >> $GITHUB_ENV - else - echo "osdk_image_exists=false" >> $GITHUB_ENV - fi - if docker manifest inspect asterinas/osdk:${IMAGE_TAG}-tdx > /dev/null 2>&1; then - echo "osdk_tdx_image_exists=true" >> $GITHUB_ENV - else - echo "osdk_tdx_image_exists=false" >> $GITHUB_ENV - fi - if docker manifest inspect asterinas/asterinas:${IMAGE_TAG} > /dev/null 2>&1; then - echo "asterinas_image_exists=true" >> $GITHUB_ENV - else - echo "asterinas_image_exists=false" >> $GITHUB_ENV - fi - if docker manifest inspect asterinas/asterinas:${IMAGE_TAG}-tdx > /dev/null 2>&1; then - echo "asterinas_tdx_image_exists=true" >> $GITHUB_ENV - else - echo "asterinas_tdx_image_exists=false" >> $GITHUB_ENV - fi - - - name: Fetch versions in the repo - id: fetch-versions - run: | - ASTER_VERSION=$(cat DOCKER_IMAGE_VERSION) - RUST_VERSION=$(grep -m1 -o 'nightly-[0-9]\+-[0-9]\+-[0-9]\+' rust-toolchain.toml) - echo "aster_version=$ASTER_VERSION" >> "$GITHUB_OUTPUT" - echo "rust_version=$RUST_VERSION" >> "$GITHUB_OUTPUT" + ./tools/github_workflows/prepare_for_docker_build_and_push.sh ${{ secrets.DOCKERHUB_USERNAME }} ${{ secrets.DOCKERHUB_TOKEN }} osdk - name: Build and push the OSDK development image - if: env.osdk_image_exists == 'false' + if: ${{ steps.prepare-for-docker-build-and-push.outputs.is_existed == 'false' }} uses: docker/build-push-action@v4 with: context: . file: ./osdk/tools/docker/Dockerfile platforms: linux/amd64 push: true - load: true - tags: asterinas/osdk:${{ steps.fetch-versions.outputs.aster_version }} + tags: asterinas/osdk:${{ steps.prepare-for-docker-build-and-push.outputs.image_version }} build-args: | - ASTER_RUST_VERSION=${{ steps.fetch-versions.outputs.rust_version }} + ASTER_RUST_VERSION=${{ steps.prepare-for-docker-build-and-push.outputs.rust_version }} + + publish_osdk_tdx_image: + needs: publish_osdk_image + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Prepare for Docker build and push + id: prepare-for-docker-build-and-push + run: | + ./tools/github_workflows/prepare_for_docker_build_and_push.sh ${{ secrets.DOCKERHUB_USERNAME }} ${{ secrets.DOCKERHUB_TOKEN }} osdk-tdx - name: Build and push the OSDK development image for Intel TDX - if: env.osdk_tdx_image_exists == 'false' + if: ${{ steps.prepare-for-docker-build-and-push.outputs.is_existed == 'false' }} uses: docker/build-push-action@v4 with: context: . file: ./osdk/tools/docker/tdx/Dockerfile platforms: linux/amd64 push: true - tags: asterinas/osdk:${{ steps.fetch-versions.outputs.aster_version }}-tdx + tags: asterinas/osdk:${{ steps.prepare-for-docker-build-and-push.outputs.image_version }}-tdx build-args: | - BASE_VERSION=${{ steps.fetch-versions.outputs.aster_version }} + BASE_VERSION=${{ steps.prepare-for-docker-build-and-push.outputs.image_version }} + + publish_asterinas_image: + needs: publish_osdk_image + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Prepare for Docker build and push + id: prepare-for-docker-build-and-push + run: | + ./tools/github_workflows/prepare_for_docker_build_and_push.sh ${{ secrets.DOCKERHUB_USERNAME }} ${{ secrets.DOCKERHUB_TOKEN }} asterinas - name: Build and push the Asterinas development image - if: env.asterinas_image_exists == 'false' + if: ${{ steps.prepare-for-docker-build-and-push.outputs.is_existed == 'false' }} uses: docker/build-push-action@v4 with: context: . file: ./tools/docker/Dockerfile platforms: linux/amd64 push: true - load: true - tags: asterinas/asterinas:${{ steps.fetch-versions.outputs.aster_version }} + tags: asterinas/asterinas:${{ steps.prepare-for-docker-build-and-push.outputs.image_version }} build-args: | - BASE_VERSION=${{ steps.fetch-versions.outputs.aster_version }} + BASE_VERSION=${{ steps.prepare-for-docker-build-and-push.outputs.image_version }} + + publish_asterinas_tdx_image: + needs: publish_asterinas_image + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Prepare for Docker build and push + id: prepare-for-docker-build-and-push + run: | + ./tools/github_workflows/prepare_for_docker_build_and_push.sh ${{ secrets.DOCKERHUB_USERNAME }} ${{ secrets.DOCKERHUB_TOKEN }} asterinas-tdx - name: Build and push the Asterinas development image for Intel TDX - if: env.asterinas_tdx_image_exists == 'false' + if: ${{ steps.prepare-for-docker-build-and-push.outputs.is_existed == 'false' }} uses: docker/build-push-action@v4 with: context: . file: ./tools/docker/tdx/Dockerfile platforms: linux/amd64 push: true - tags: asterinas/asterinas:${{ steps.fetch-versions.outputs.aster_version }}-tdx + tags: asterinas/asterinas:${{ steps.prepare-for-docker-build-and-push.outputs.image_version }}-tdx build-args: | - BASE_VERSION=${{ steps.fetch-versions.outputs.aster_version }} + BASE_VERSION=${{ steps.prepare-for-docker-build-and-push.outputs.image_version }} diff --git a/tools/github_workflows/prepare_for_docker_build_and_push.sh b/tools/github_workflows/prepare_for_docker_build_and_push.sh new file mode 100755 index 000000000..fcd8d4d7c --- /dev/null +++ b/tools/github_workflows/prepare_for_docker_build_and_push.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# SPDX-License-Identifier: MPL-2.0 + +set -e + +if [[ -z "$1" || -z "$2" || -z "$3" ]]; then + echo "Prepare the environment for the Github action of docker/build-push-action" + echo "Usage: $0 " + exit 1 +fi + +USERNAME="$1" +TOKEN="$2" +IMAGE_NAME="$3" + +# Step 1: Set up Docker Buildx +echo "Setting up Docker Buildx..." +docker buildx create --use || { + echo "Failed to set up Docker Buildx" + exit 1 +} + +# Step 2: Login to Docker Hub +echo "Logging in to Docker Hub..." +echo "${TOKEN}" | docker login -u "${USERNAME}" --password-stdin || { + echo "Docker login failed" + exit 2 +} + +# Step 3: Fetch versions +echo "Fetching Docker image version and Rust version..." +ASTER_SRC_DIR=$(dirname "$0")/../.. +IMAGE_VERSION=$(cat ${ASTER_SRC_DIR}/DOCKER_IMAGE_VERSION) +RUST_VERSION=$(grep -m1 -o 'nightly-[0-9]\+-[0-9]\+-[0-9]\+' ${ASTER_SRC_DIR}/rust-toolchain.toml) +echo "image_version=$IMAGE_VERSION" >> $GITHUB_OUTPUT +echo "rust_version=$RUST_VERSION" >> $GITHUB_OUTPUT + +# Step 4: Check if Docker image exists +echo "Checking if Docker image exists..." +if [[ "${IMAGE_NAME}" == "osdk" ]]; then + DOCKER_IMAGE="asterinas/osdk:${IMAGE_VERSION}" +elif [[ "${IMAGE_NAME}" == "osdk-tdx" ]]; then + DOCKER_IMAGE="asterinas/osdk:${IMAGE_VERSION}-tdx" +elif [[ "${IMAGE_NAME}" == "asterinas" ]]; then + DOCKER_IMAGE="asterinas/asterinas:${IMAGE_VERSION}" +elif [[ "${IMAGE_NAME}" == "asterinas-tdx" ]]; then + DOCKER_IMAGE="asterinas/asterinas:${IMAGE_VERSION}-tdx" +else + echo "Error: Unknown image name '${IMAGE_NAME}'" + exit 4 +fi +if docker manifest inspect "${DOCKER_IMAGE}" > /dev/null 2>&1; then + echo "is_existed=true" >> $GITHUB_OUTPUT +else + echo "is_existed=false" >> $GITHUB_OUTPUT +fi