Auto build docker #44
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: Auto build docker | |
| on: | |
| schedule: | |
| - cron: '0 12 * * *' | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| env: | |
| DOCKER_REPO: initialencounter/llonebot | |
| jobs: | |
| update: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| BUILD: ${{ steps.set_build.outputs.BUILD }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Install Nix | |
| uses: cachix/install-nix-action@v31 | |
| - name: Get latest version | |
| run: | | |
| echo "BUILD=false" >> $GITHUB_OUTPUT | |
| git config --local user.email "[email protected]" | |
| git config --local user.name "GitHub Actions" | |
| echo "LATEST_LLONEBOT_VERSION=$(echo "$(curl "https://api.github.com/repos/LLOneBot/LLOneBot/releases/latest" | jq -r '.tag_name')" | sed 's/^.//')" >> $GITHUB_ENV | |
| echo "LATEST_PMHQ_VERSION=$(echo "$(curl "https://api.github.com/repos/linyuchen/PMHQ/releases/latest" | jq -r '.tag_name')" | sed 's/^.//')" >> $GITHUB_ENV | |
| - name: Get local version | |
| run: | | |
| echo "LOCAL_LLONEBOT_VERSION=$(grep "llonebot_version = " package/sources.nix | cut -d'"' -f2)" >> $GITHUB_ENV | |
| echo "LOCAL_PMHQ_VERSION=$(grep "pmhq_version = " package/sources.nix | cut -d'"' -f2)" >> $GITHUB_ENV | |
| - name: update llonebot | |
| if: env.LOCAL_LLONEBOT_VERSION != env.LATEST_LLONEBOT_VERSION | |
| run: | | |
| ./update.sh llonebot ${{ env.LATEST_LLONEBOT_VERSION }} | |
| git add . | |
| git commit -m "llonebot ${{ env.LOCAL_LLONEBOT_VERSION }} -> ${{ env.LATEST_LLONEBOT_VERSION }}" | |
| echo "BUILD=true" >> $GITHUB_OUTPUT | |
| - name: update pmhq | |
| if: env.LOCAL_PMHQ_VERSION != env.LATEST_PMHQ_VERSION | |
| run: | | |
| ./update.sh pmhq ${{ env.LATEST_PMHQ_VERSION }} | |
| git add . | |
| git commit -m "pmhq ${{ env.LOCAL_PMHQ_VERSION }} -> ${{ env.LATEST_PMHQ_VERSION }}" | |
| echo "BUILD=true" >> $GITHUB_OUTPUT | |
| - name: Get old hash | |
| run: | | |
| echo "old_pmhq_amd64_hash=$(grep "pmhq_amd64_hash = " package/sources.nix | cut -d'"' -f2)" >> $GITHUB_ENV | |
| echo "old_pmhq_arm64_hash=$(grep "pmhq_arm64_hash = " package/sources.nix | cut -d'"' -f2)" >> $GITHUB_ENV | |
| echo "old_llonebot_hash=$(grep "llonebot_hash = " package/sources.nix | cut -d'"' -f2)" >> $GITHUB_ENV | |
| - name: update hash | |
| run: | | |
| ./update.sh pmhq ${{ env.LATEST_PMHQ_VERSION }} | |
| new_pmhq_amd64_hash=$(grep "pmhq_amd64_hash = " package/sources.nix | cut -d'"' -f2) | |
| new_pmhq_arm64_hash=$(grep "pmhq_arm64_hash = " package/sources.nix | cut -d'"' -f2) | |
| if [[ "$old_pmhq_amd64_hash" != "$new_pmhq_amd64_hash" ]] || [[ "$old_pmhq_arm64_hash" != "$new_pmhq_arm64_hash" ]]; then | |
| git add . | |
| git commit -m "fix: pmhq_hash" | |
| echo "BUILD=true" >> $GITHUB_OUTPUT | |
| fi | |
| ./update.sh llonebot ${{ env.LATEST_LLONEBOT_VERSION }} | |
| new_llonebot_hash=$(grep "llonebot_hash = " package/sources.nix | cut -d'"' -f2) | |
| if [[ "$old_llonebot_hash" != "$new_llonebot_hash" ]]; then | |
| git add . | |
| git commit -m "fix: llonebot_hash" | |
| echo "BUILD=true" >> $GITHUB_OUTPUT | |
| fi | |
| - name: git push | |
| run: git push | |
| build: | |
| needs: update | |
| if: github.event_name == 'workflow_dispatch' || needs.update.outputs.BUILD == 'true' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| ref: ${{ github.ref }} | |
| - uses: cachix/install-nix-action@v31 | |
| - uses: DeterminateSystems/flakehub-cache-action@main | |
| - name: Build Docker Image | |
| run: | | |
| git pull origin main | |
| nix build .#dockerImage | |
| - name: Load Docker Image | |
| run: docker load < result | |
| - name: Login Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Push Docker Image | |
| run: | | |
| VERSION=$(grep "llonebot_version = " package/sources.nix | cut -d'"' -f2) | |
| if [ -z "$VERSION" ]; then | |
| echo "Failed to extract version" | |
| exit 1 | |
| fi | |
| TAG="v$VERSION" | |
| echo "Using tag: $TAG" | |
| IMAGE_ID=$(docker images --format '{{.ID}}' llonebot:latest) | |
| docker tag $IMAGE_ID ${{ env.DOCKER_REPO }}:latest | |
| docker tag $IMAGE_ID ${{ env.DOCKER_REPO }}:$TAG | |
| docker push ${{ env.DOCKER_REPO }}:latest | |
| docker push ${{ env.DOCKER_REPO }}:$TAG |