Skip to content

Commit

Permalink
Merge pull request #1 from freenowtech/feature/dockerize
Browse files Browse the repository at this point in the history
Feature/dockerize
  • Loading branch information
sergioasantiago committed Aug 21, 2023
2 parents c54b8ad + 14c5fa2 commit 35f5795
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 75 deletions.
56 changes: 56 additions & 0 deletions .chglog/CHANGELOG.tpl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{{ if .Versions -}}
<a name="unreleased"></a>
## [Unreleased]

{{ if .Unreleased.CommitGroups -}}
{{ range .Unreleased.CommitGroups -}}
### {{ .Title }}
{{ range .Commits -}}
- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }}
{{ end }}
{{ end -}}
{{ end -}}
{{ end -}}

{{ range .Versions }}
<a name="{{ .Tag.Name }}"></a>
## {{ if .Tag.Previous }}[{{ .Tag.Name }}]{{ else }}{{ .Tag.Name }}{{ end }} - {{ datetime "2006-01-02" .Tag.Date }}
{{ range .CommitGroups -}}
### {{ .Title }}
{{ range .Commits -}}
- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }}
{{ end }}
{{ end -}}

{{- if .RevertCommits -}}
### Reverts
{{ range .RevertCommits -}}
- {{ .Revert.Header }}
{{ end }}
{{ end -}}

{{- if .MergeCommits -}}
### Pull Requests
{{ range .MergeCommits -}}
- {{ .Header }}
{{ end }}
{{ end -}}

{{- if .NoteGroups -}}
{{ range .NoteGroups -}}
### {{ .Title }}
{{ range .Notes }}
{{ .Body }}
{{ end }}
{{ end -}}
{{ end -}}
{{ end -}}

{{- if .Versions }}
[Unreleased]: {{ .Info.RepositoryURL }}/compare/{{ $latest := index .Versions 0 }}{{ $latest.Tag.Name }}...HEAD
{{ range .Versions -}}
{{ if .Tag.Previous -}}
[{{ .Tag.Name }}]: {{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}
{{ end -}}
{{ end -}}
{{ end -}}
27 changes: 27 additions & 0 deletions .chglog/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
style: github
template: CHANGELOG.tpl.md
info:
title: CHANGELOG
repository_url: https://github.com/freenowtech/tableau-prometheus-exporter
options:
commits:
# filters:
# Type:
# - feat
# - fix
# - perf
# - refactor
commit_groups:
# title_maps:
# feat: Features
# fix: Bug Fixes
# perf: Performance Improvements
# refactor: Code Refactoring
header:
pattern: "^((\\w+)\\s.*)$"
pattern_maps:
- Subject
- Type
notes:
keywords:
- BREAKING CHANGE
202 changes: 132 additions & 70 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,91 +14,122 @@ on:
- cron: '0 0 1,16 * *'

permissions:
contents: read
contents: write
pull-requests: write
packages: write

concurrency:
group: >-
${{ github.workflow }}-${{ github.ref_type }}-
${{ github.event.pull_request.number || github.sha }}
cancel-in-progress: true

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
prepare:
# prepare:
# runs-on: ubuntu-latest
# outputs:
# wheel-distribution: ${{ steps.wheel-distribution.outputs.path }}
# steps:
# - uses: actions/checkout@v3
# with: {fetch-depth: 0} # deep clone for setuptools-scm
# - uses: actions/setup-python@v4
# id: setup-python
# with: {python-version: "3.11"}
# - name: Run static analysis and format checkers
# run: pipx run pre-commit run --all-files --show-diff-on-failure
# - name: Build package distribution files
# run: >-
# pipx run --python '${{ steps.setup-python.outputs.python-path }}'
# tox -e clean,build
# - name: Record the path of wheel distribution
# id: wheel-distribution
# run: echo "path=$(ls dist/*.whl)" >> $GITHUB_OUTPUT
# - name: Store the distribution files for use in other stages
# # `tests` and `publish` will use the same pre-built distributions,
# # so we make sure to release the exact same package that was tested
# uses: actions/upload-artifact@v3
# with:
# name: python-distribution-files
# path: dist/
# retention-days: 1
#
# test:
# needs: prepare
# strategy:
# matrix:
# python:
# - "3.7" # oldest Python supported by PSF
# - "3.11" # newest Python that is stable
# platform:
# - ubuntu-latest
# - macos-latest
# - windows-latest
# runs-on: ${{ matrix.platform }}
# steps:
# - uses: actions/checkout@v3
# - uses: actions/setup-python@v4
# id: setup-python
# with:
# python-version: ${{ matrix.python }}
# - name: Retrieve pre-built distribution files
# uses: actions/download-artifact@v3
# with: {name: python-distribution-files, path: dist/}
# - name: Run tests
# run: >-
# pipx run --python '${{ steps.setup-python.outputs.python-path }}'
# tox --installpkg '${{ needs.prepare.outputs.wheel-distribution }}'
# -- -rFEx --durations 10 --color yes # pytest args
# - name: Generate coverage report
# run: pipx run coverage lcov -o coverage.lcov
# - name: Upload partial coverage report
# uses: coverallsapp/github-action@master
# with:
# path-to-lcov: coverage.lcov
# github-token: ${{ secrets.GITHUB_TOKEN }}
# flag-name: ${{ matrix.platform }} - py${{ matrix.python }}
# parallel: true
#
# finalize:
# needs: test
# runs-on: ubuntu-latest
# steps:
# - name: Finalize coverage report
# uses: coverallsapp/github-action@master
# with:
# github-token: ${{ secrets.GITHUB_TOKEN }}
# parallel-finished: true
#
changelog:
# needs: finalize
# if: ${{ github.event_name == 'push' && contains(github.ref, 'refs/tags/') }}
runs-on: ubuntu-latest
outputs:
wheel-distribution: ${{ steps.wheel-distribution.outputs.path }}
steps:
- uses: actions/checkout@v3
with: {fetch-depth: 0} # deep clone for setuptools-scm
- uses: actions/setup-python@v4
id: setup-python
with: {python-version: "3.11"}
- name: Run static analysis and format checkers
run: pipx run pre-commit run --all-files --show-diff-on-failure
- name: Build package distribution files
run: >-
pipx run --python '${{ steps.setup-python.outputs.python-path }}'
tox -e clean,build
- name: Record the path of wheel distribution
id: wheel-distribution
run: echo "path=$(ls dist/*.whl)" >> $GITHUB_OUTPUT
- name: Store the distribution files for use in other stages
# `tests` and `publish` will use the same pre-built distributions,
# so we make sure to release the exact same package that was tested
uses: actions/upload-artifact@v3
with:
name: python-distribution-files
path: dist/
retention-days: 1

test:
needs: prepare
strategy:
matrix:
python:
- "3.7" # oldest Python supported by PSF
- "3.11" # newest Python that is stable
platform:
- ubuntu-latest
- macos-latest
- windows-latest
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
id: setup-python
with:
python-version: ${{ matrix.python }}
- name: Retrieve pre-built distribution files
uses: actions/download-artifact@v3
with: {name: python-distribution-files, path: dist/}
- name: Run tests
run: >-
pipx run --python '${{ steps.setup-python.outputs.python-path }}'
tox --installpkg '${{ needs.prepare.outputs.wheel-distribution }}'
-- -rFEx --durations 10 --color yes # pytest args
- name: Generate coverage report
run: pipx run coverage lcov -o coverage.lcov
- name: Upload partial coverage report
uses: coverallsapp/github-action@master
ref: 'main'
fetch-depth: 0
- name: Generate Changelog
uses: sergioasantiago/generate-changelog-action@main
id: changelog
with:
path-to-lcov: coverage.lcov
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: ${{ matrix.platform }} - py${{ matrix.python }}
parallel: true

finalize:
needs: test
runs-on: ubuntu-latest
steps:
- name: Finalize coverage report
uses: coverallsapp/github-action@master
# next_tag: 0.2.1
output_file: CHANGELOG.md
write_file: true
config_path: ${{ github.workspace }}/.chglog
- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
commit-message: "Update CHANGELOG.md"
branch: chore/update-changelog
delete-branch: true
title: "Update CHANGELOG"

publish:
needs: finalize
publish-pypi:
needs: changelog
if: ${{ github.event_name == 'push' && contains(github.ref, 'refs/tags/') }}
runs-on: ubuntu-latest
permissions:
Expand All @@ -116,3 +147,34 @@ jobs:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: pipx run tox -e publish

publish-docker:
needs: publish-pypi
if: ${{ github.event_name == 'push' && contains(github.ref, 'refs/tags/') }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.FREE_NOW_GITHUB_USER }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push
uses: docker/build-push-action@v4
with:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: "VERSION===${{ github.ref_name }}"
platforms: linux/amd64,linux/arm64
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ MANIFEST
.conda*/
.python-version

config.yaml
/config.yaml
/config.yml
50 changes: 50 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/engine/reference/builder/

ARG PYTHON_VERSION=3.11.3
FROM python:${PYTHON_VERSION}-slim as base

# Tableau Prometheus Exporter Version
ARG VERSION=>=0.1.0

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

WORKDIR /app

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN --mount=type=cache,target=/root/.cache/pip \
python -m pip install tableau-prometheus-exporter${VERSION}

# Switch to the non-privileged user to run the application.
USER appuser

# Expose the port that the application listens on.
EXPOSE 8000

ENTRYPOINT ["python", "-m", "tableau_prometheus_exporter"]

# Run the application.
CMD ["--version"]
Loading

0 comments on commit 35f5795

Please sign in to comment.