-
Notifications
You must be signed in to change notification settings - Fork 5
105 lines (92 loc) · 3.12 KB
/
docker-build.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
name: Docker Build and Push
on:
push:
branches:
- 'main' # Only trigger push events on main
tags: [ "v*" ]
pull_request: # Keep PR triggers for all branches
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
docker:
# Skip this job if it's a push event on a non-main branch
if: github.event_name != 'push' || github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
# Generate Cargo.lock if it doesn't exist
- name: Generate Cargo.lock
run: |
if [ ! -f "Cargo.lock" ]; then
cargo generate-lockfile
fi
- name: Extract version from Cargo.toml
id: version
run: |
VERSION=$(awk -F '"' '/^version = / {print $2}' Cargo.toml)
echo "CARGO_VERSION=${VERSION}" >> $GITHUB_ENV
echo "Version found: ${VERSION}"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# Always build to verify Dockerfile, but push only on main
- name: Build Docker image (non-main branch)
if: github.ref != 'refs/heads/main'
uses: docker/build-push-action@v5
with:
context: .
push: false
platforms: linux/amd64,linux/arm64
cache-from: type=gha
cache-to: type=gha,mode=max
# Main branch handling - build and push
- name: Log in to GitHub Container Registry
if: github.ref == 'refs/heads/main'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for GHCR
if: github.ref == 'refs/heads/main'
id: meta-ghcr
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest
type=raw,value=${{ env.CARGO_VERSION }}
- name: Build and push to GitHub Container Registry
if: github.ref == 'refs/heads/main'
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta-ghcr.outputs.tags }}
labels: ${{ steps.meta-ghcr.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64
- name: Login to Docker Hub
if: github.ref == 'refs/heads/main'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push to Docker Hub
if: github.ref == 'refs/heads/main'
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: |
magicapi1/magicapi-ai-gateway:latest
magicapi1/magicapi-ai-gateway:${{ env.CARGO_VERSION }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64