Skip to content

Commit

Permalink
Build container in CI
Browse files Browse the repository at this point in the history
Signed-off-by: John Strunk <[email protected]>
  • Loading branch information
JohnStrunk committed Apr 25, 2024
1 parent 66063ef commit 3bbef34
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 48 deletions.
47 changes: 0 additions & 47 deletions .github/workflows/pre-commit.yaml

This file was deleted.

100 changes: 100 additions & 0 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
# yamllint disable rule:line-length
name: "CI Workflow"

on: # yamllint disable-line rule:truthy
push:
branches:
- main
tags: ["*"]
pull_request:
# The branches below must be a subset of the branches above
branches:
- main
workflow_dispatch:

env:
CONTAINER_IMAGE: ghcr.io/johnstrunk/jira-summarizer

jobs:
pre-commit:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
# https://github.com/actions/checkout
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4

- name: Set up Python
id: setup-py
# https://github.com/actions/setup-python
uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
python-version: "3.12"

- name: Enable cache for pre-commit hooks
# https://github.com/actions/cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ steps.setup-py.outputs.python-version}}|${{ hashFiles('.pre-commit-config.yaml') }}
restore-keys: |
pre-commit|${{ steps.setup-py.outputs.python-version}}|
pre-commit|
- name: Run pre-commit checks
run: pipx run pre-commit run -a

- name: Run pre-commit gc
run: pipx run pre-commit gc

container:
needs: [pre-commit]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4

- name: Set up Docker Buildx
id: setup-buildx
uses: docker/setup-buildx-action@d70bba72b1f3fd22344832f00baa16ece964efeb # v3.3.0

- name: Login to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set container metadata
id: meta
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1
with:
images: ${{ env.CONTAINER_IMAGE }}
# semver lines are for mapping git tags to container tags
# v1.2.3 -> 1.2.3, 1.2, 1
# type=raw sets 'latest' to match the most recent commit on main
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/v0.') }}
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@2cdde995de11925a030ce8070c3d77a52ffcf1c0 # v5.3.0
with:
annotations: ${{ steps.meta.outputs.annotations }}
build-args: |
BUILD_DATE=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
VERSION_ID=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
COMMIT_ID=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
context: .
platforms: linux/amd64
push: false
# push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ repos:
rev: v2.12.0
hooks:
# Lint Dockerfiles
- id: hadolint
- id: hadolint-docker

- repo: https://github.com/igorshubovych/markdownlint-cli
rev: "v0.39.0"
Expand Down
34 changes: 34 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This Dockerfile is based on the pattern recommended by the pipenv docs:
# https://pipenv.pypa.io/en/latest/docker.html
FROM python:3.12 as builder

RUN pip install --no-cache-dir pipenv==2023.12.1
ENV PIPENV_VENV_IN_PROJECT=1

WORKDIR /app
COPY Pipfile Pipfile.lock /app/
RUN pipenv --no-site-packages install -v --deploy


############################################################
FROM python:3.12-slim as final

ARG BUILD_DATE=""
ARG COMMIT_ID=""
ARG VERSION_ID=""

WORKDIR /app
COPY --from=builder /app/.venv /app/.venv
COPY *.py /app/

# https://github.com/opencontainers/image-spec/blob/main/annotations.md
LABEL org.opencontainers.image.base.name="docker.io/library/python:3.12-slim"
LABEL org.opencontainers.image.created=${BUILD_DATE}
LABEL org.opencontainers.image.description="A simple bot that uses an AI model to summarize Jira issues"
LABEL org.opencontainers.image.licenses="Apache-2.0"
LABEL org.opencontainers.image.revision=${COMMIT_ID}
LABEL org.opencontainers.image.source="https://github.com/JohnStrunk/jira-summary.git"
LABEL org.opencontainers.image.title="jira-summarizer"
LABEL org.opencontainers.image.version=${VERSION_ID}

ENTRYPOINT ["/app/.venv/bin/python", "bot.py"]

0 comments on commit 3bbef34

Please sign in to comment.