-
Notifications
You must be signed in to change notification settings - Fork 2
170 lines (149 loc) · 6.4 KB
/
build_and_deploy_containers.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
name: Build and Deploy Containers
on:
push:
branches-ignore:
- main # Run on branches that are not main
pull_request:
branches:
- main # Run on pull requests to merge into main
workflow_dispatch: # Allow manual triggering
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BACKEND_IMAGE_NAME: rust-backend
FRONTEND_IMAGE_NAME: nextjs-frontend
jobs:
build_test_run:
name: Build and Test
runs-on: ubuntu-22.04
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
# Checkout the repository
- name: Checkout
uses: actions/checkout@v4
# Set environment variables from secrets
- name: Set environment variables
run: |
echo "POSTGRES_USER=${{ secrets.POSTGRES_USER }}" >> $GITHUB_ENV
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> $GITHUB_ENV
echo "POSTGRES_DB=${{ secrets.POSTGRES_DB }}" >> $GITHUB_ENV
echo "POSTGRES_PORT=${{ secrets.POSTGRES_PORT }}" >> $GITHUB_ENV
echo "POSTGRES_SCHEMA=${{ secrets.POSTGRES_SCHEMA }}" >> $GITHUB_ENV
echo "POSTGRES_HOST=${{ secrets.POSTGRES_HOST }}" >> $GITHUB_ENV
echo "DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" >> $GITHUB_ENV
echo "BACKEND_PORT=${{ secrets.BACKEND_PORT }}" >> $GITHUB_ENV
echo "BACKEND_INTERFACE=${{ secrets.BACKEND_INTERFACE }}" >> $GITHUB_ENV
echo "PLATFORM=${{ secrets.PLATFORM }}" >> $GITHUB_ENV
echo "CONTAINER_NAME=${{ secrets.CONTAINER_NAME }}" >> $GITHUB_ENV
echo "FRONTEND_CONTAINER_NAME=${{ secrets.FRONTEND_CONTAINER_NAME }}" >> $GITHUB_ENV
echo "FRONTEND_PORT=${{ secrets.FRONTEND_PORT }}" >> $GITHUB_ENV
echo "DB_DNS_ALIAS=${{ secrets.DB_DNS_ALIAS }}" >> $GITHUB_ENV
echo "BACKEND_DNS_ALIAS=${{ secrets.BACKEND_DNS_ALIAS }}" >> $GITHUB_ENV
echo "FRONTEND_DNS_ALIAS=${{ secrets.FRONTEND_DNS_ALIAS }}" >> $GITHUB_ENV
echo "BACKEND_ALLOWED_ORIGINS=${{ secrets.BACKEND_ALLOWED_ORIGINS }}" >> $GITHUB_ENV
echo "BACKEND_LOG_FILTER_LEVEL=${{ secrets.BACKEND_LOG_FILTER_LEVEL }}" >> $GITHUB_ENV
# Install Rust toolchain
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: x86_64-unknown-linux-gnu
# Use cached dependencies
- name: Use cached dependencies
uses: Swatinem/rust-cache@v2
with:
key: "ubuntu-22.04-x86_64-unknown-linux-gnu"
# Install seaORM CLI
- name: Install seaORM CLI
run: cargo install sea-orm-cli
# Build the project
- name: Build
run: cargo build --all-targets
# Run tests
- name: Test
run: cargo test
build_and_push_docker:
name: Build and Push Docker Images
runs-on: ubuntu-22.04
needs: build_test_run # Ensure tests pass before building Docker images
steps:
# Checkout the repository
- name: Checkout
uses: actions/checkout@v4
# Log in to GitHub Container Registry
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# Cache Docker layers
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
# Extract metadata (tags, labels) for Docker
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE_NAME }}-${{ github.actor }}:${{ github.sha }}, ${{ env.REGISTRY }}/${{ env.FRONTEND_IMAGE_NAME }}-${{ github.actor }}:${{ github.sha }}
# Build and push Rust backend image
- name: Build and Push Rust Backend Image
id: push_backend
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64
context: .
push: true
tags: |
${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE_NAME }}-${{ github.actor }}:${{ github.sha }}
${{ steps.meta.outputs.tags }}-${{ github.sha }}
${{ github.actor }}/${{ env.BACKEND_IMAGE_NAME }}:${{ github.sha }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
file: Dockerfile
# Build and push Next.js frontend image
- name: Build and Push Next.js Frontend Image
id: push_frontend
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64
context: web
push: true
tags: |
${{ env.REGISTRY }}/${{ env.FRONTEND_IMAGE_NAME }}-${{ github.actor }}:${{ github.sha }}
${{ steps.meta.outputs.tags }}-${{ github.sha }}
${{ github.actor }}/${{ env.FRONTEND_IMAGE_NAME }}:${{ github.sha }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
file: web/Dockerfile
# Move new cache to the original location
- name: Move new cache
run: mv /tmp/.buildx-cache-new /tmp/.buildx-cache
# Generate artifact attestation for Rust backend image
- name: Generate artifact attestation for Rust Backend Image
uses: actions/attest-build-provenance@v2
with:
subject-name: ${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE_NAME }}-rust-backend
subject-digest: ${{ steps.push_backend.outputs.digest }}
push-to-registry: true
# Generate artifact attestation for Next.js frontend image
- name: Generate artifact attestation for Next.js Frontend Image
uses: actions/attest-build-provenance@v2
with:
subject-name: ${{ env.REGISTRY }}/${{ env.FRONTEND_IMAGE_NAME }}-nextjs-frontend
subject-digest: ${{ steps.push_frontend.outputs.digest }}
push-to-registry: true