Skip to content

Commit

Permalink
Merge pull request #60 from openmethane/container-version
Browse files Browse the repository at this point in the history
Container version
  • Loading branch information
aethr authored Nov 27, 2024
2 parents c522b2b + d000758 commit f06cfc2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/build_docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,22 @@ jobs:
type=ref,event=branch
type=ref,event=pr
type=raw,value=build-${{ github.run_number }}
# Duplicated from Dockerfile due to https://github.com/docker/metadata-action/issues/295
labels: |
org.opencontainers.image.title="Open Methane Prior Emissions"
org.opencontainers.image.description="Method to calculate a gridded, prior emissions estimate for methane across Australia."
org.opencontainers.image.authors="Peter Rayner <[email protected]>, Jared Lewis <[email protected]>"
org.opencontainers.image.vendor="The Superpower Institute"
- name: Build and push image
uses: docker/build-push-action@v5
id: build
with:
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
tags: ${{ steps.meta.outputs.tags }}
build-args: |
OPENMETHANE_PRIOR_VERSION=${{ steps.meta.outputs.version }}
push: true
pull: false
cache-from: type=gha
Expand All @@ -86,6 +95,15 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.github_token }}
steps:
- name: Check package version
if: startsWith(github.event.ref, 'refs/tags/v')
run: |
TAG_REF="${{ github.event.ref }}"
TAG_VERSION=${TAG_REF/"refs\/tags\/"}
if [ "$OPENMETHANE_PRIOR_VERSION" != "$TAG_VERSION" ]; then
echo "OPENMETHANE_PRIOR_VERSION is $OPENMETHANE_PRIOR_VERSION; expected version is $TAG_VERSION"
exit 1
fi
- name: Run a quick test suite
run: |
cd /opt/project
Expand Down
14 changes: 13 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ RUN python -m venv /opt/venv && \
# This isn't a hyper optimised container but it's a good starting point
FROM python:3.11

LABEL org.opencontainers.image.authors="[email protected]"
# These will be overwritten in GHA due to https://github.com/docker/metadata-action/issues/295
# These must be duplicated in .github/workflows/build_docker.yaml
LABEL org.opencontainers.image.title="Open Methane Prior Emissions"
LABEL org.opencontainers.image.description="Method to calculate a gridded, prior emissions estimate for methane across Australia."
LABEL org.opencontainers.image.authors="Peter Rayner <[email protected]>, Jared Lewis <[email protected]>"
LABEL org.opencontainers.image.vendor="The Superpower Institute"

# OPENMETHANE_PRIOR_VERSION will be overridden in release builds with semver vX.Y.Z
ARG OPENMETHANE_PRIOR_VERSION=development
# Make the $OPENMETHANE_PRIOR_VERSION available as an env var inside the container
ENV OPENMETHANE_PRIOR_VERSION=$OPENMETHANE_PRIOR_VERSION

LABEL org.opencontainers.image.version="${OPENMETHANE_PRIOR_VERSION}"

# Configure Python
ENV PYTHONFAULTHANDLER=1 \
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ clean-all: ## remove all temporary files including downloaded data
download: ## Download the data for the project
poetry run python scripts/omDownloadInputs.py

.PHONY: run
run: download ## Run the project for an example period
poetry run python scripts/omPrior.py 2022-07-01 2022-07-01
.PHONY: run-example
run-example: download ## Run the project for an example period
poetry run python scripts/omPrior.py --start-date 2022-07-01 --end-date 2022-07-01

.PHONY: ruff-fixes
ruff-fixes: # Run ruff on the project
Expand Down
1 change: 1 addition & 0 deletions changelog/60.improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make OPENMETHANE_PRIOR_VERSION environment variable available inside the container
10 changes: 10 additions & 0 deletions src/openmethane_prior/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
#
"""Input file definitions and checks"""

import datetime
import os
import pathlib
import shutil
import sys
import urllib.parse

import requests
import netCDF4 as nc

from openmethane_prior.config import PriorConfig
from openmethane_prior.utils import get_version


def download_input_file(remote_url: str, url_fragment: str, save_path: pathlib.Path) -> bool:
Expand Down Expand Up @@ -86,6 +89,13 @@ def initialise_output(config: PriorConfig):

shutil.copyfile(config.input_domain_file, config.output_domain_file)

# Add the version of openmethane-prior used to generate the file as an attribute
outfile = nc.Dataset(config.output_domain_file, "a", format="NETCDF4")
outfile.title = "Open Methane Prior Estimate"
outfile.comment = "Gridded prior emissions estimate for methane across Australia"
outfile.history = f"{datetime.datetime.now(datetime.timezone.utc)}: {' '.join(sys.argv)}"
outfile.openmethane_prior_version = get_version()
outfile.sync() # write attributes back to the file

def check_input_files(config: PriorConfig):
"""
Expand Down
6 changes: 6 additions & 0 deletions src/openmethane_prior/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@

import datetime
import gzip
import importlib
import os
import pathlib
import pickle
import sys
import typing

import numpy as np
Expand Down Expand Up @@ -140,3 +143,6 @@ def redistribute_spatially(lat_shape, ind_x, ind_y, coefs, subset, from_areas, t
gridded[i, j] += subset[iy, ix] * coefs[ij][k] * from_areas[iy, ix]
gridded /= to_areas
return gridded

def get_version():
return os.getenv('OPENMETHANE_PRIOR_VERSION', importlib.metadata.version('openmethane_prior'))

0 comments on commit f06cfc2

Please sign in to comment.