Skip to content

Commit b2e03b4

Browse files
authored
Merge pull request #87 from refactor-group/82-feature-request-suggestion-create-github-actions-workflow-for-automating-the-build-and-deployments-for-feature-branches
82 feature request suggestion create GitHub actions workflow for automating the build and deployments for feature branches
2 parents 4b18db2 + 5372ece commit b2e03b4

15 files changed

+1464
-1199
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
name: Build and Deploy Containers
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
types: [opened, synchronize, reopened]
11+
workflow_dispatch:
12+
13+
env:
14+
REGISTRY: ghcr.io
15+
IMAGE_NAME: ${{ github.repository }}
16+
17+
jobs:
18+
build_test_run:
19+
runs-on: ubuntu-22.04
20+
21+
steps:
22+
# Checkout code
23+
- uses: actions/checkout@v4
24+
25+
# Enable QEMU for multi-arch builds (arm64 on x86)
26+
- name: Set up Rust + QEMU
27+
uses: docker/setup-qemu-action@v2
28+
with:
29+
platforms: linux/amd64,linux/arm64
30+
31+
# Install Rust and cache artifacts
32+
- name: Set up Rust
33+
uses: dtolnay/rust-toolchain@stable
34+
with:
35+
toolchain: stable
36+
37+
- uses: Swatinem/rust-cache@v2
38+
with:
39+
shared-key: ${{ runner.os }}-cargo-${{ github.sha }}
40+
41+
# Install sea-orm-cli globally (if needed for migration or seed)
42+
- name: Install sea-orm-cli
43+
run: cargo install sea-orm-cli
44+
45+
# Run tests for x86_64 only (CI feedback)
46+
- name: Run tests
47+
run: cargo test --release
48+
49+
build_and_push_docker:
50+
runs-on: ubuntu-22.04
51+
needs: build_test_run
52+
permissions:
53+
contents: read
54+
packages: write
55+
attestations: write
56+
id-token: write
57+
58+
steps:
59+
# Checkout source code
60+
- uses: actions/checkout@v4
61+
62+
# Docker login to GHCR
63+
- name: Docker login
64+
uses: docker/login-action@v2
65+
with:
66+
registry: ${{ env.REGISTRY }}
67+
username: ${{ github.actor }}
68+
password: ${{ secrets.GITHUB_TOKEN }}
69+
70+
# Set up Docker Buildx for multi-platform builds
71+
- uses: docker/setup-buildx-action@v3
72+
with:
73+
install: true
74+
75+
# Show current Docker cache usage
76+
- name: Show Docker Build Cache (Before)
77+
run: |
78+
echo -e "\033[1;34m🔍 Checking buildx cache BEFORE build...\033[0m"
79+
docker buildx du || echo -e "\033[1;33m⚠️ No cache found yet.\033[0m"
80+
81+
# Compute image tag
82+
- name: Determine Image Tags
83+
id: tags
84+
run: |
85+
BRANCH_NAME=${GITHUB_HEAD_REF:-${GITHUB_REF##*/}}
86+
IMAGE_NAME="${{ env.REGISTRY }}/${{ github.repository }}/${BRANCH_NAME}"
87+
echo "backend_tags=$IMAGE_NAME:latest" >> $GITHUB_OUTPUT
88+
echo "backend_image_name=$IMAGE_NAME" >> $GITHUB_OUTPUT
89+
90+
# Build and push multi-arch Docker image with cache
91+
- name: Build + Push Backend
92+
id: push_backend
93+
uses: docker/build-push-action@v5
94+
with:
95+
context: .
96+
file: ./Dockerfile
97+
platforms: linux/amd64,linux/arm64 # ✅ Key multi-arch setting
98+
push: true
99+
provenance: true
100+
tags: ${{ steps.tags.outputs.backend_tags }}
101+
cache-from: type=gha
102+
cache-to: type=gha,mode=max
103+
104+
# Show updated cache usage
105+
- name: Show Docker Build Cache (After)
106+
run: |
107+
echo -e "\033[1;34m📦 Checking buildx cache AFTER build...\033[0m"
108+
docker buildx du || echo -e "\033[1;31m❌ Failed to get updated cache info\033[0m"
109+
110+
# Attest build provenance if on main branch
111+
- name: Attest Backend
112+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
113+
uses: actions/attest-build-provenance@v2
114+
with:
115+
subject-name: ${{ steps.tags.outputs.backend_image_name }}
116+
subject-digest: ${{ steps.push_backend.outputs.digest }}
117+
push-to-registry: true
118+
119+
# Output how to pull and run the pushed image
120+
- name: Print Usage Instructions
121+
run: |
122+
echo -e "\033[1;32m✅ Backend Image Pushed:\033[0m"
123+
echo " docker pull ${{ steps.tags.outputs.backend_image_name }}:latest"
124+
echo ""
125+
echo -e "\033[1;36m▶️ Run Backend:\033[0m"
126+
echo " docker run --rm --env-file .env -p 8000:8000 ${{ steps.tags.outputs.backend_image_name }}:latest"

.github/workflows/ci.yml

+11-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ jobs:
1919
- name: Install Rust toolchain
2020
uses: dtolnay/rust-toolchain@stable
2121
with:
22-
targets: x86_64-unknown-linux-gnu
22+
targets: x86_64-unknown-linux-gnu,aarch64-unknown-linux-gnu
23+
24+
- name: Use cached dependencies
25+
uses: Swatinem/rust-cache@v2
26+
with:
27+
key: "ubuntu-22.04-x86_64-and-aarch64"
28+
29+
- name: Set OpenSSL Paths
30+
run: |
31+
echo "OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu" >> $GITHUB_ENV
32+
echo "OPENSSL_INCLUDE_DIR=/usr/include/x86_64-linux-gnu" >> $GITHUB_ENV
2333
2434
- name: Use cached dependencies
2535
uses: Swatinem/rust-cache@v2

0 commit comments

Comments
 (0)