Skip to content

Commit

Permalink
🐳 Multistage build (#24)
Browse files Browse the repository at this point in the history
* Refactor GitHub Actions Workflows

- Remove unnecessary lines in `deploy.yml`.
- Rename the `validate-pr` job to `validate-labels` in `pr_lint.yml` and add concurrency settings.
- Modify the way of getting pull request data in `pr_lint.yml`.
- Add `validate-code`, `validate-docker-build`, and `validate-branch` jobs in `pr_lint.yml` with proper concurrency settings.
- In `validate-docker-build`, implement Docker image building and caching.
- Check the branch name with the version in the `validate-branch` job.

* Update PR Lint Workflow to Post Docker Image Info

- Modify the Docker build step to tag images using the PR number only.
- Add a new step to retrieve Docker image information using `docker inspect` and `jq`.
- Add another step to post this Docker image information as a comment on the PR using `actions/github-script`.
- The image information includes the image ID, creation time, size, and tags.

* Update Docker Build for Multi-arch Support

- Add support for multiple architectures (linux/amd64, linux/arm64, linux/arm/v7).
- Enable Docker Buildx for cross-platform builds.
- Update the GitHub Actions workflow to support matrix builds.
- Add Docker Scout for image size and recommendations.
  • Loading branch information
jag-k authored Apr 13, 2024
1 parent 745bc11 commit 283d2fe
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = false
insert_final_newline = true
max_line_length = 120
tab_width = 4
trim_trailing_whitespace = true
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ jobs:
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64,linux/arm/v7


release:
runs-on: ubuntu-latest
if: ${{ needs.lint.outputs.is_release }}
Expand Down Expand Up @@ -141,7 +140,6 @@ jobs:
- name: Release
uses: softprops/action-gh-release@v1

with:
body: |
Docker image: [`${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.get_version.outputs.version }}`](https://github.com/jag-k/owntinfoil/pkgs/container/owntinfoil/${{ steps.docker_build.outputs }}?tag=${{ steps.get_version.outputs.version }})
Expand Down
60 changes: 55 additions & 5 deletions .github/workflows/pr_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
pull_request:

jobs:
validate-pr:
validate-labels:
runs-on: ubuntu-latest
steps:
- name: Check labels
Expand All @@ -13,7 +13,9 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
# language=JavaScript
script: |
const { data: pullRequest } = await github.rest.pulls.get({
const {
data: pullRequest
} = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
Expand All @@ -37,7 +39,9 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
# language=JavaScript
script: |
const { data: pullRequest } = await github.rest.pulls.get({
const {
data: pullRequest
} = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
Expand All @@ -53,7 +57,9 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
# language=JavaScript
script: |
const { data: pullRequest } = await github.rest.pulls.get({
const {
data: pullRequest
} = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
Expand Down Expand Up @@ -96,7 +102,6 @@ jobs:
runs-on: ubuntu-latest
needs:
- validate-code

if: startsWith(github.head_ref, 'release/')
steps:
- name: Check branch name with version
Expand All @@ -109,3 +114,48 @@ jobs:
echo "Branch name does not match the version in pyproject.toml"
exit 1
fi
validate-docker-build:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
packages: read
strategy:
matrix:
platform: [linux/amd64, linux/arm64, linux/arm/v7]
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
labels: |
org.opencontainers.image.revision=${{ github.sha }}
tags: |
type=sha,prefix=,suffix=,format=short
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ matrix.platform }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-${{ matrix.platform }}-
- name: Build Docker image for ${{ matrix.platform }}
uses: docker/build-push-action@v5
id: build-and-push
with:
context: .
push: false
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
platforms: ${{ matrix.platform }}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.12-slim as base
FROM --platform=$BUILDPLATFORM python:3.12-slim as base

LABEL org.opencontainers.image.source="https://github.com/jag-k/owntinfoil"
LABEL org.opencontainers.image.description="OwnTinfoil Image"
Expand Down
2 changes: 1 addition & 1 deletion app/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def get_data(self, retries: int = 2) -> bytes:
print(f"Server disconnected for file {self.name!r}, retrying...", file=sys.stderr)
retries -= 1
print(f"Failed to download file {self.name!r} after {original_retries} retries!", file=sys.stderr)
return None
return b""

@staticmethod
def normalize_name(name: str) -> str:
Expand Down

0 comments on commit 283d2fe

Please sign in to comment.