build-new #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: build-new | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| tool_chain_version: | |
| description: 'Tool chain version' | |
| required: false | |
| default: 'nightly-2023-09-18' | |
| version: | |
| description: 'Image version tag' | |
| required: true | |
| default: '' | |
| targets: | |
| description: 'Build targets (comma-separated or "all")' | |
| required: true | |
| default: 'all' | |
| is_free_disk_space: | |
| description: 'Free up disk space before build' | |
| required: false | |
| default: 'false' | |
| jobs: | |
| build: | |
| runs-on: ubuntu-22.04 | |
| continue-on-error: true | |
| steps: | |
| - name: Check Server Performance | |
| run: | | |
| echo "--------------------------CPU info--------------------------" | |
| echo "CPU Physical: $(cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l)" | |
| echo "CPU: $(nproc)" | |
| echo -e "cpu info:$(cat /proc/cpuinfo | grep -m1 name | awk -F: '{print $2}')\n" | |
| echo "--------------------------mem info--------------------------" | |
| echo "memory:" | |
| echo -e "$(sudo lshw -short -C memory | grep GiB)\n" | |
| echo "--------------------------disk info--------------------------" | |
| echo "disk: $(ls /dev/sd* | grep -v [1-9] | wc -l)" && df -hT | |
| - name: Before freeing up disk space | |
| if: ${{ github.event.inputs.is_free_disk_space == 'true' }} | |
| run: | | |
| echo "Before freeing up disk space" | |
| echo "==============================================================================" | |
| df -hT | |
| echo "==============================================================================" | |
| - name: "Optimize Disk Space" | |
| if: ${{ github.event.inputs.is_free_disk_space == 'true' }} | |
| run: | | |
| sudo rm -rf /usr/share/dotnet /etc/apt/sources.list.d /usr/local/lib/android $AGENT_TOOLSDIRECTORY | |
| sudo -E apt-get -y purge azure-cli ghc* zulu* llvm* firefox google* dotnet* powershell openjdk* mongodb* moby* || true | |
| sudo -E apt-get -y update | |
| sudo -E apt-get -y install $(curl -fsSL is.gd/depends_ubuntu_2204) | |
| sudo -E systemctl daemon-reload | |
| sudo -E apt-get -y autoremove --purge | |
| sudo -E apt-get -y clean | |
| - name: Free up disk space complete | |
| if: ${{ github.event.inputs.is_free_disk_space == 'true' }} | |
| run: | | |
| echo "Free up disk space complete" | |
| echo "==============================================================================" | |
| df -hT | |
| echo "==============================================================================" | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| lfs: true | |
| - name: Set up docker buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ secrets.GHCR_USERNAME }} | |
| password: ${{ secrets.GHCR_TOKEN }} | |
| - name: Chmod +x for script | |
| run: chmod -R +x script/ | |
| - name: Parse and Find Dockerfiles for Targets | |
| id: set_targets | |
| run: | | |
| TARGETS="${{ github.event.inputs.targets }}" | |
| if [[ "$TARGETS" == "all" ]]; then | |
| # Find all Dockerfile paths in Dockerfiles/all/ and extract the target names | |
| TARGETS=$(find Dockerfiles/all -type f -name 'Dockerfile.*' | sed -E 's|.*/Dockerfile\.||' | paste -sd ',') | |
| fi | |
| TARGETS_JSON=$(echo "[\"${TARGETS//,/\",\"}\"]") | |
| echo "TARGETS_JSON=${TARGETS_JSON}" >> $GITHUB_ENV | |
| echo "Found targets: $TARGETS" | |
| - name: Build and Push | |
| run: | | |
| echo "Starting build for targets in Dockerfiles/all/" | |
| echo "==============================================================================" | |
| TARGETS=$(echo $TARGETS_JSON | jq -r '.[]') | |
| # Build phase | |
| for TARGET in $TARGETS; do | |
| echo "Building for target: $TARGET" | |
| IMAGE_NAME=$TARGET:${{ github.event.inputs.version }} | |
| DOCKERFILE="Dockerfiles/all/Dockerfile.$TARGET" | |
| if [[ ! -f "$DOCKERFILE" ]]; then | |
| echo "Warning: Dockerfile not found at $DOCKERFILE, skipping..." | |
| continue | |
| fi | |
| echo "Using Dockerfile: $DOCKERFILE" | |
| # Build the docker image | |
| docker build \ | |
| --label "org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}" \ | |
| --build-arg TOOLCHAIN_VERSION=${{ github.event.inputs.tool_chain_version }} \ | |
| -t $IMAGE_NAME \ | |
| -f $DOCKERFILE . || echo "$TARGET build failed, continuing..." | |
| echo "$TARGET has been built successfully" | |
| done | |
| echo "==============================================================================" | |
| echo "All targets have been built" | |
| echo "==============================================================================" | |
| echo "Starting push to GitHub Container Registry and Docker Hub" | |
| echo "==============================================================================" | |
| # Push phase | |
| for TARGET in $TARGETS; do | |
| IMAGE_NAME=$TARGET:${{ github.event.inputs.version }} | |
| # Check if image exists before pushing | |
| if docker image inspect $IMAGE_NAME >/dev/null 2>&1; then | |
| echo "Pushing $TARGET to registries..." | |
| # Push to GitHub Container Registry | |
| echo "Pushing to ghcr.io..." | |
| docker tag $IMAGE_NAME ghcr.io/${{ secrets.GHCR_USERNAME }}/$IMAGE_NAME | |
| docker push ghcr.io/${{ secrets.GHCR_USERNAME }}/$IMAGE_NAME || echo "Failed to push $TARGET to ghcr.io" | |
| # Push to Docker Hub | |
| echo "Pushing to Docker Hub..." | |
| docker tag $IMAGE_NAME ${{ secrets.DOCKERHUB_USERNAME }}/$IMAGE_NAME | |
| docker push ${{ secrets.DOCKERHUB_USERNAME }}/$IMAGE_NAME || echo "Failed to push $TARGET to Docker Hub" | |
| echo "$TARGET has been pushed to both registries" | |
| else | |
| echo "Warning: Image $IMAGE_NAME not found, skipping push..." | |
| fi | |
| done | |
| echo "==============================================================================" | |
| echo "Build and push process completed" | |
| echo "==============================================================================" |