From aa4e48e2b925b6ef7aeb0cbe7f1455ddafe59530 Mon Sep 17 00:00:00 2001 From: caibirdme Date: Thu, 1 Aug 2024 23:37:53 +0800 Subject: [PATCH] ci: add workflow that can build docker image automatically when MR is merged --- .github/workflows/auto-docker.yml | 79 +++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 .github/workflows/auto-docker.yml diff --git a/.github/workflows/auto-docker.yml b/.github/workflows/auto-docker.yml new file mode 100644 index 0000000..f392581 --- /dev/null +++ b/.github/workflows/auto-docker.yml @@ -0,0 +1,79 @@ +name: Rust CI + +on: + pull_request: + types: [closed] + +jobs: + check_changes: + runs-on: ubuntu-latest + # exec only when PR is merged + if: github.event.pull_request.merged == true + outputs: + rust_changed: ${{ steps.check_rust_changes.outputs.rust_changed }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Check for Rust file changes + id: check_rust_changes + run: | + git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} > changes.txt + if grep -qE '\.rs$|Cargo\.toml' changes.txt; then + echo "rust_changed=true" >> $GITHUB_OUTPUT + else + echo "rust_changed=false" >> $GITHUB_OUTPUT + fi + + rust_ci: + needs: check_changes + if: needs.check_changes.outputs.rust_changed == 'true' + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + strategy: + matrix: + target: [x86_64-unknown-linux-gnu, x86_64-unknown-linux-musl] + steps: + - uses: actions/checkout@v4 + + - name: Set up Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: 1.80.0 + profile: minimal + override: true + target: ${{ matrix.target }} + + - name: Install cross + run: cargo install cross + + - name: Build with cross + run: cross build --target ${{ matrix.target }} --release + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: ghcr.io/${{ github.repository }}:latest-${{ matrix.target }} + file: docker/${{ matrix.target }}/Dockerfile + no_changes: + needs: check_changes + if: needs.check_changes.outputs.rust_changed == 'false' + runs-on: ubuntu-latest + steps: + - name: No Rust changes + run: echo "No changes to Rust files or Cargo.toml, skipping CI."