|
8 | 8 | tags: ["*"] |
9 | 9 | workflow_dispatch: |
10 | 10 |
|
11 | | -env: |
12 | | - TARGET: base |
| 11 | +permissions: |
| 12 | + contents: read |
| 13 | + packages: write |
13 | 14 |
|
14 | 15 | jobs: |
15 | | - |
| 16 | + compute-base-build-tag: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + outputs: |
| 19 | + image_tag: ${{ steps.out.outputs.tag }} |
| 20 | + steps: |
| 21 | + - name: Checkout Repository |
| 22 | + uses: actions/checkout@v4 |
| 23 | + - name: Create Tag |
| 24 | + id: out |
| 25 | + run: echo "tag=${{ hashFiles('Dockerfile','rust-toolchain.toml') }}" >> "$GITHUB_OUTPUT" |
16 | 26 | build-base: |
| 27 | + needs: compute-base-build-tag |
17 | 28 | runs-on: ubuntu-latest |
18 | 29 | steps: |
19 | 30 | - name: Checkout Repository |
20 | 31 | uses: actions/checkout@v4 |
21 | | - |
22 | 32 | - name: Log in to GitHub Container Registry |
23 | 33 | uses: docker/login-action@v3 |
24 | 34 | with: |
25 | 35 | registry: ghcr.io |
26 | 36 | username: ${{ github.actor }} |
27 | 37 | password: ${{ secrets.GITHUB_TOKEN }} |
28 | | - |
29 | 38 | - name: Pull Cached Base Image |
30 | | - id: check-image |
31 | | - env: |
32 | | - IMAGE_TAG: ${{ hashFiles('Dockerfile', 'rust-toolchain.toml') }} |
33 | 39 | run: | |
34 | | - if docker pull ghcr.io/${{ github.repository }}/$TARGET:$IMAGE_TAG; then |
| 40 | + if docker pull ghcr.io/${{ github.repository }}/base:${{ needs.compute-base-build-tag.outputs.image_tag }}; then |
35 | 41 | echo "CACHE_HIT=true" >> $GITHUB_ENV |
36 | 42 | else |
37 | 43 | echo "CACHE_HIT=false" >> $GITHUB_ENV |
38 | 44 | fi |
39 | | -
|
40 | 45 | - name: Set up Docker Buildx |
41 | 46 | if: env.CACHE_HIT == 'false' |
42 | 47 | uses: docker/setup-buildx-action@v3 |
43 | | - |
44 | 48 | - name: Build and Push Base Image (if not cached) |
45 | 49 | if: env.CACHE_HIT == 'false' |
46 | | - env: |
47 | | - IMAGE_TAG: ${{ hashFiles('Dockerfile', 'rust-toolchain.toml') }} |
48 | | - run: | |
49 | | - docker build --target $TARGET -t ghcr.io/${{ github.repository }}/$TARGET:$IMAGE_TAG -f Dockerfile . |
50 | | - docker push ghcr.io/${{ github.repository }}/$TARGET:$IMAGE_TAG |
51 | | -
|
| 50 | + uses: docker/build-push-action@v6 |
| 51 | + with: |
| 52 | + context: . |
| 53 | + file: Dockerfile |
| 54 | + target: base |
| 55 | + push: true |
| 56 | + tags: ghcr.io/${{ github.repository }}/base:${{ needs.compute-base-build-tag.outputs.image_tag }} |
52 | 57 | fmt: |
53 | | - needs: build-base |
| 58 | + needs: [compute-base-build-tag, build-base] |
54 | 59 | runs-on: ubuntu-latest |
| 60 | + container: |
| 61 | + image: ghcr.io/${{ github.repository }}/base:${{ needs.compute-base-build-tag.outputs.image_tag }} |
| 62 | + options: --user 1001:1001 |
55 | 63 | concurrency: |
56 | 64 | group: ${{ github.workflow }}-${{ github.ref }}-fmt |
57 | 65 | cancel-in-progress: true |
58 | 66 | steps: |
59 | 67 | - name: Checkout Repository |
60 | 68 | uses: actions/checkout@v4 |
61 | | - |
62 | | - - name: Log in to GitHub Container Registry |
63 | | - uses: docker/login-action@v3 |
64 | | - with: |
65 | | - registry: ghcr.io |
66 | | - username: ${{ github.actor }} |
67 | | - password: ${{ secrets.GITHUB_TOKEN }} |
68 | | - |
69 | | - - name: Pull Base Image |
70 | | - env: |
71 | | - IMAGE_TAG: ${{ hashFiles('Dockerfile', 'rust-toolchain.toml') }} |
72 | | - run: docker pull ghcr.io/${{ github.repository }}/$TARGET:$IMAGE_TAG |
73 | | - |
74 | 69 | - name: Check formatting |
75 | | - env: |
76 | | - IMAGE_TAG: ${{ hashFiles('Dockerfile', 'rust-toolchain.toml') }} |
77 | | - run: | |
78 | | - docker run \ |
79 | | - --rm \ |
80 | | - -v $PWD:/app \ |
81 | | - -w /app \ |
82 | | - ghcr.io/${{ github.repository }}/$TARGET:$IMAGE_TAG \ |
83 | | - cargo fmt -- --check |
84 | | -
|
| 70 | + run: cargo fmt -- --check |
85 | 71 | clippy: |
86 | | - needs: build-base |
| 72 | + needs: [compute-base-build-tag, build-base] |
87 | 73 | runs-on: ubuntu-latest |
| 74 | + container: |
| 75 | + image: ghcr.io/${{ github.repository }}/base:${{ needs.compute-base-build-tag.outputs.image_tag }} |
| 76 | + options: --user 1001:1001 |
88 | 77 | concurrency: |
89 | 78 | group: ${{ github.workflow }}-${{ github.ref }}-clippy |
90 | 79 | cancel-in-progress: true |
91 | 80 | steps: |
92 | 81 | - name: Checkout Repository |
93 | 82 | uses: actions/checkout@v4 |
94 | | - |
95 | 83 | - name: Cache Cargo Target Directory |
96 | 84 | uses: actions/cache@v4 |
97 | | - env: |
98 | | - IMAGE_TAG: ${{ hashFiles('Dockerfile', 'rust-toolchain.toml') }} |
99 | 85 | with: |
100 | 86 | path: target |
101 | | - key: clippy-${{ env.TARGET }}-${{ env.IMAGE_TAG }}-${{ hashFiles('**/Cargo.lock') }} |
| 87 | + key: clippy-base-${{ needs.compute-base-build-tag.outputs.image_tag }}-${{ hashFiles('**/Cargo.lock') }} |
102 | 88 | restore-keys: | |
103 | | - clippy-${{ env.TARGET }}-${{ env.IMAGE_TAG }}- |
104 | | -
|
105 | | - - name: Log in to GitHub Container Registry |
106 | | - uses: docker/login-action@v3 |
107 | | - with: |
108 | | - registry: ghcr.io |
109 | | - username: ${{ github.actor }} |
110 | | - password: ${{ secrets.GITHUB_TOKEN }} |
111 | | - |
112 | | - - name: Pull Base Image |
113 | | - env: |
114 | | - IMAGE_TAG: ${{ hashFiles('Dockerfile', 'rust-toolchain.toml') }} |
115 | | - run: docker pull ghcr.io/${{ github.repository }}/$TARGET:$IMAGE_TAG |
116 | | - |
| 89 | + clippy-base-${{ needs.compute-base-build-tag.outputs.image_tag }}- |
117 | 90 | - name: Clippy |
118 | | - env: |
119 | | - IMAGE_TAG: ${{ hashFiles('Dockerfile', 'rust-toolchain.toml') }} |
120 | | - run: | |
121 | | - docker run \ |
122 | | - --rm \ |
123 | | - -v $PWD:/app \ |
124 | | - -v $PWD/target:/app/target \ |
125 | | - -w /app \ |
126 | | - ghcr.io/${{ github.repository }}/$TARGET:$IMAGE_TAG \ |
127 | | - cargo clippy --all-targets -- -Dclippy::all -D warnings |
128 | | -
|
129 | | - - name: Fix Permissions for Caching |
130 | | - run: sudo chown -R $(whoami):$(whoami) target |
131 | | - |
| 91 | + run: cargo clippy --all-targets -- -Dclippy::all -D warnings |
132 | 92 | generate-matrix: |
133 | 93 | runs-on: ubuntu-latest |
134 | 94 | outputs: |
135 | 95 | matrix: ${{ steps.get-matrix.outputs.matrix }} |
136 | 96 | steps: |
137 | 97 | - name: Checkout Code |
138 | 98 | uses: actions/checkout@v4 |
139 | | - |
140 | 99 | - name: Restore Cached Matrix |
141 | 100 | id: cache-matrix |
142 | 101 | uses: actions/cache@v4 |
143 | 102 | with: |
144 | 103 | path: matrix.json |
145 | 104 | key: matrix-${{ hashFiles('Cargo.toml', 'Cargo.lock') }} |
146 | | - |
147 | 105 | - name: Extract Cargo Workspace Members (if not cached) |
148 | 106 | if: steps.cache-matrix.outputs.cache-hit != 'true' |
149 | 107 | run: | |
150 | 108 | set -e |
151 | 109 | cargo metadata --format-version=1 | jq -c '[.workspace_members[] | split("#")[0] | split("/") | last | gsub("_"; "-") | select(. != "metrics")]' > matrix.json |
152 | | -
|
153 | 110 | - name: Save Matrix to Output |
154 | 111 | id: get-matrix |
155 | 112 | run: echo "matrix=$(cat matrix.json)" >> $GITHUB_OUTPUT |
156 | | - |
157 | 113 | tests: |
158 | | - needs: [generate-matrix, build-base] |
| 114 | + needs: [compute-base-build-tag, build-base, generate-matrix] |
159 | 115 | runs-on: ubuntu-latest |
| 116 | + container: |
| 117 | + image: ghcr.io/${{ github.repository }}/base:${{ needs.compute-base-build-tag.outputs.image_tag }} |
| 118 | + options: --user 1001:1001 |
160 | 119 | strategy: |
161 | 120 | fail-fast: false |
162 | 121 | matrix: |
@@ -186,43 +145,48 @@ jobs: |
186 | 145 | steps: |
187 | 146 | - name: Checkout Repository |
188 | 147 | uses: actions/checkout@v4 |
189 | | - |
190 | 148 | - name: Cache Cargo Target Directory |
191 | 149 | # Anything above 4min run |
192 | | - if: contains('["boost-manager", "ingest", "iot-config", "iot-packet-verifier", "iot-verifier", "mobile-packet-verifier", "mobile-verifier", "price", "solana"]', matrix.package) |
| 150 | + if: contains('["ingest", "iot-config", "iot-packet-verifier", "iot-verifier", "mobile-packet-verifier", "mobile-verifier", "price", "solana"]', matrix.package) |
193 | 151 | uses: actions/cache@v4 |
194 | | - env: |
195 | | - IMAGE_TAG: ${{ hashFiles('Dockerfile', 'rust-toolchain.toml') }} |
196 | 152 | with: |
197 | 153 | path: target |
198 | | - key: tests-${{ matrix.package }}-${{ env.TARGET }}-${{ env.IMAGE_TAG }}-${{ hashFiles('**/Cargo.lock') }} |
| 154 | + key: tests-${{ matrix.package }}-base-${{ needs.compute-base-build-tag.outputs.image_tag }}-${{ hashFiles('**/Cargo.lock') }} |
199 | 155 | restore-keys: | |
200 | | - tests-${{ matrix.package }}-${{ env.TARGET }}-${{ env.IMAGE_TAG }}- |
201 | | -
|
| 156 | + tests-${{ matrix.package }}-base-${{ needs.compute-base-build-tag.outputs.image_tag }}- |
| 157 | + - name: Run tests |
| 158 | + env: |
| 159 | + DATABASE_URL: postgres://postgres:postgres@postgres:5432/postgres |
| 160 | + AWSLOCAL_ENDPOINT: "http://localstack:4566" |
| 161 | + run: cargo test -p ${{ matrix.package }} -- --include-ignored |
| 162 | + build-image: |
| 163 | + if: startsWith(github.ref, 'refs/tags/') |
| 164 | + needs: [fmt, clippy, tests] |
| 165 | + runs-on: ubuntu-latest |
| 166 | + strategy: |
| 167 | + matrix: |
| 168 | + package: ["ingest","mobile-config","mobile-packet-verifier","mobile-verifier","price","reward-index"] |
| 169 | + concurrency: |
| 170 | + group: ${{ github.workflow }}-${{ github.ref }}-build-image-${{ matrix.package }} |
| 171 | + cancel-in-progress: true |
| 172 | + steps: |
| 173 | + - name: Checkout Repository |
| 174 | + uses: actions/checkout@v4 |
202 | 175 | - name: Log in to GitHub Container Registry |
203 | 176 | uses: docker/login-action@v3 |
204 | 177 | with: |
205 | 178 | registry: ghcr.io |
206 | 179 | username: ${{ github.actor }} |
207 | 180 | password: ${{ secrets.GITHUB_TOKEN }} |
208 | | - |
209 | | - - name: Pull Base Image |
210 | | - env: |
211 | | - IMAGE_TAG: ${{ hashFiles('Dockerfile', 'rust-toolchain.toml') }} |
212 | | - run: docker pull ghcr.io/${{ github.repository }}/$TARGET:$IMAGE_TAG |
213 | | - |
214 | | - - name: Run tests |
215 | | - env: |
216 | | - IMAGE_TAG: ${{ hashFiles('Dockerfile', 'rust-toolchain.toml') }} |
217 | | - run: | |
218 | | - docker run \ |
219 | | - --rm \ |
220 | | - --network=host \ |
221 | | - -e DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres" \ |
222 | | - -v $PWD:/app \ |
223 | | - -w /app \ |
224 | | - ghcr.io/${{ github.repository }}/$TARGET:$IMAGE_TAG \ |
225 | | - cargo test -p ${{ matrix.package }} -- --include-ignored |
226 | | -
|
227 | | - - name: Fix Permissions for Caching |
228 | | - run: sudo chown -R $(whoami):$(whoami) target |
| 181 | + - name: Set up Docker Buildx |
| 182 | + uses: docker/setup-buildx-action@v3 |
| 183 | + - name: Build and Push Base Image |
| 184 | + uses: docker/build-push-action@v6 |
| 185 | + with: |
| 186 | + context: . |
| 187 | + file: Dockerfile |
| 188 | + target: runner |
| 189 | + push: true |
| 190 | + tags: ghcr.io/${{ github.repository }}/${{ matrix.package }}:${{ github.ref_name }} |
| 191 | + build-args: | |
| 192 | + PACKAGE=${{ matrix.package }} |
0 commit comments