Skip to content

Commit

Permalink
test: one reusable workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthB committed Jan 21, 2025
1 parent 9b94372 commit 8e3f28b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 246 deletions.
83 changes: 4 additions & 79 deletions .github/workflows/cargo_update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: cargo update

on:
schedule:
- cron: '0 8 * * *'
- cron: "0 8 * * *"
workflow_dispatch:

permissions:
Expand All @@ -12,81 +12,6 @@ env:
TZ: Europe/Stockholm

jobs:
update-dependencies:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.DELTA_BOT_GH_TOKEN }}

steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: Cache Cargo Dependencies
uses: actions/cache@v4
with:
path: |
~/.cargo
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-
- name: Install Protobuf
run: sudo apt-get install -y protobuf-compiler

- name: Install cargo-edit and cargo-outdated
run: |
cargo install cargo-edit
cargo install cargo-outdated
- name: Get Outdated Dependencies
id: outdated_before
run: cargo outdated > outdated_before.txt

- name: Update Dependencies
id: update_step
run: |
BRANCH_NAME="update-dependencies-$(date +%Y%m%d%H%M%S)"
git checkout -b "$BRANCH_NAME"
echo "Branch created: $BRANCH_NAME"
# Update all dependencies
cargo update
- name: Get Updated Dependencies
id: outdated_after
run: cargo outdated > outdated_after.txt

- name: Commit and Push Changes
if: steps.update_step.outputs.changes_detected == 'true'
run: |
git config --global user.name "dmlops"
git config --global user.email "[email protected]"
git add .
UPDATED_DEPS=$(diff -u outdated_before.txt outdated_after.txt | grep '^+[^+]' | sed 's/^+//')
COMMIT_MSG="Bump dependencies\n\n$UPDATED_DEPS"
git commit -m "$COMMIT_MSG"
git push https://x-access-token:${{ secrets.DELTA_BOT_GH_TOKEN }}@github.com/${{ github.repository }} $BRANCH_NAME
echo "Changes committed and pushed to $BRANCH_NAME"
- name: Create Pull Request
if: steps.update_step.outputs.changes_detected == 'true'
run: |
# Compare outdated dependencies before and after update
UPDATED_DEPS=$(diff -u outdated_before.txt outdated_after.txt | grep '^+[^+]' | sed 's/^+//')
PR_BODY="Bump dependencies\n\n$UPDATED_DEPS"
# Create a pull request
PR_URL=$(gh pr create --base master --head $BRANCH_NAME --title "Update dependencies" --body "$PR_BODY" --draft=false --label "dependencies")
echo "Pull request created: $PR_URL"
# Automatically merge the pull request after it meets the requirements
gh pr merge "$PR_URL" --merge --delete-branch
echo "Pull request merged and branch deleted."
build:
uses: blackportal-ai/infra/.github/workflows/cargo-update.yml@master
secrets: inherit
118 changes: 12 additions & 106 deletions .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: core
on:
push:
branches:
- master
- reusable-workflow
pull_request:
branches:
- master
Expand All @@ -17,111 +17,17 @@ on:

jobs:
clippy:
name: Clippy
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

- name: Cache Cargo Dependencies
uses: actions/cache@v4
with:
path: |
~/.cargo
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Install Protobuf
run: sudo apt-get install -y protobuf-compiler

- name: Run Clippy
run: cargo clippy --all-targets

build:
name: Build
runs-on: ubuntu-latest
needs: clippy

steps:
- uses: actions/checkout@v4

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable

- name: Cache Cargo Dependencies
uses: actions/cache@v4
with:
path: |
~/.cargo
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Install Protobuf
run: sudo apt-get install -y protobuf-compiler

- name: Build Project
run: cargo build --release

test:
name: Tests
runs-on: ubuntu-latest
needs: build

steps:
- uses: actions/checkout@v4

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable

- name: Cache Cargo Dependencies
uses: actions/cache@v4
with:
path: |
~/.cargo
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Install Protobuf
run: sudo apt-get install -y protobuf-compiler

- name: Run Tests
run: cargo test

coverage:
name: Code Coverage
runs-on: ubuntu-latest
needs: test

steps:
- uses: actions/checkout@v4

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable

- name: Install Protobuf
run: sudo apt-get install -y protobuf-compiler
uses: blackportal-ai/infra/.github/workflows/core.yml@master
secrets: inherit

- name: Install Tarpaulin
run: cargo install cargo-tarpaulin
#build:
# uses: blackportal-ai/infra/.github/workflows/core.yml@master
# secrets: inherit

- name: Run Coverage
run: cargo tarpaulin --out Html
#test:
# uses: blackportal-ai/infra/.github/workflows/core.yml@master
# secrets: inherit

- name: Upload Coverage Report
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: ./tarpaulin-report.html
#coverage:
# uses: blackportal-ai/infra/.github/workflows/core.yml@master
# secrets: inherit
63 changes: 2 additions & 61 deletions .github/workflows/machete.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,5 @@ env:

jobs:
cleanup:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.DELTA_BOT_GH_TOKEN }}

steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: Cache Cargo Dependencies
uses: actions/cache@v4
with:
path: |
~/.cargo
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-
- name: Install Protobuf
run: sudo apt-get install -y protobuf-compiler

- name: Install cargo-machete
run: cargo install cargo-machete

- name: Create Branch and Fix Unused Dependencies
id: cleanup_step
run: |
BRANCH_NAME="cleanup-$(date +%Y%m%d%H%M%S)"
git checkout -b "$BRANCH_NAME"
echo "Branch created: $BRANCH_NAME"
# Run cargo machete --fix and capture its exit code
cargo machete --fix || true
# Check if there are changes after fixing
if [[ -n "$(git status --porcelain)" ]]; then
git config --global user.name "dmlops"
git config --global user.email "[email protected]"
git add .
git commit -m "Fixing unused dependencies"
git push https://x-access-token:${{ secrets.DELTA_BOT_GH_TOKEN }}@github.com/${{ github.repository }} $BRANCH_NAME
echo "Changes committed and pushed to $BRANCH_NAME"
echo "Attempting to create a pull request..."
# Create a pull request
PR_URL=$(gh pr create --base master --head $BRANCH_NAME --title "Fix unused dependencies" --body "This PR fixes unused dependencies found by cargo machete." --draft=false --label "dependencies")
echo "Pull request created: $PR_URL"
# Automatically merge the pull request after it meets the requirements
gh pr merge "$PR_URL" --squash --delete-branch
echo "Pull request merged and branch deleted."
else
echo "No changes detected. No commit or push."
fi
uses: blackportal-ai/infra/.github/workflows/core.yml@master
secrets: inherit

0 comments on commit 8e3f28b

Please sign in to comment.