-
Notifications
You must be signed in to change notification settings - Fork 2
123 lines (106 loc) · 3.71 KB
/
docker-publish.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
name: Docker Build and Publish
on:
push:
branches: ["main"]
release:
types: [created]
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/hxckr-core
CARGO_TERM_COLOR: always
jobs:
build-and-push-dev:
if: github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Nix
uses: cachix/install-nix-action@v20
with:
nix_path: nixpkgs=channel:nixos-unstable
extra_nix_config: |
experimental-features = nix-command flakes
accept-flake-config = true
keep-outputs = true
keep-derivations = true
- name: Setup Nix caching
uses: actions/cache@v3
with:
path: |
~/.cache/nix
key: ${{ runner.os }}-nix-${{ hashFiles('**/docker-image.nix', '**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-nix-
- name: Build with Nix
run: |
nix-build build-image.nix
docker load < result
- name: Verify image contents
run: |
docker run --rm hxckr-core:latest ls -l /app
docker run --rm hxckr-core:latest ls -l /app/migrations
docker run --rm hxckr-core:latest cat /app/entrypoint.sh
docker run --rm hxckr-core:latest diesel --version
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ env.DOCKERHUB_USERNAME }}
password: ${{ env.DOCKERHUB_TOKEN }}
- name: Push Docker image
run: |
docker tag hxckr-core:latest ${{ env.IMAGE_NAME }}:dev
docker tag hxckr-core:latest ${{ env.IMAGE_NAME }}:${{ github.sha }}
docker push ${{ env.IMAGE_NAME }}:dev
docker push ${{ env.IMAGE_NAME }}:${{ github.sha }}
- name: Print image size and details
run: |
docker image ls ${{ env.IMAGE_NAME }}:dev
docker history ${{ env.IMAGE_NAME }}:dev
build-and-push-prod:
if: github.event_name == 'release'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
- name: Build project
run: |
cargo install cargo-chef
cargo chef prepare --recipe-path recipe.json
cargo chef cook --release --recipe-path recipe.json
cargo build --release --all-features
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ env.DOCKERHUB_USERNAME }}
password: ${{ env.DOCKERHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
file: Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: |
${{ env.IMAGE_NAME }}:latest
${{ env.IMAGE_NAME }}:${{ github.ref_name }}
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache
cache-to: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache,mode=max
- name: Run migrations
env:
DATABASE_URL: ${{ secrets.PRODUCTION_DATABASE_URL }}
run: |
docker run --rm \
-e DATABASE_URL \
${{ env.IMAGE_NAME }}:${{ github.ref_name }} \
diesel migration run
- name: Print image size and details
run: |
docker image ls ${{ env.IMAGE_NAME }}:${{ github.ref_name }}
docker history ${{ env.IMAGE_NAME }}:${{ github.ref_name }}