Skip to content

Commit df34d4d

Browse files
authored
Merge pull request #77 from oceanhackweek/Pixi
Trying out Pixi for image building
2 parents c3d1f4b + 6114389 commit df34d4d

File tree

11 files changed

+18808
-851
lines changed

11 files changed

+18808
-851
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
name: Build and Push Docker Image
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
working_directory:
7+
type: string
8+
description: What directory should the image be built from
9+
required: true
10+
image_name:
11+
type: string
12+
description: Name of Docker image
13+
required: true
14+
image_tag:
15+
type: string
16+
description: Tag for Docker image
17+
required: true
18+
push_image:
19+
type: boolean
20+
description: Should the image be pushed to the Github Container Registry
21+
required: false
22+
default: false
23+
secrets:
24+
GH_TOKEN:
25+
description: Github access token
26+
required: false
27+
SLACK_WEBHOOK_URL:
28+
description: Slack webhook URL to send messages to
29+
required: false
30+
outputs:
31+
image_name:
32+
description: Fully qualified image name
33+
value: ${{ jobs.build-image.outputs.image_name}}
34+
image_ref:
35+
description: Docker image reference
36+
value: ${{ jobs.build-image.outputs.image_ref }}
37+
38+
workflow_dispatch:
39+
inputs:
40+
working_directory:
41+
type: string
42+
description: What directory should the image be built from
43+
required: true
44+
image_name:
45+
type: string
46+
description: Name of Docker image
47+
required: true
48+
image_tag:
49+
type: string
50+
description: Tag for Docker image
51+
required: true
52+
push_image:
53+
type: boolean
54+
description: Should the image be pushed to the Github Container Registry
55+
required: false
56+
default: false
57+
58+
jobs:
59+
build-image:
60+
runs-on: ubuntu-22.04
61+
name: Build and push image
62+
timeout-minutes: 30
63+
64+
outputs:
65+
image_name: ${{ steps.env_var.outputs.image_name }}
66+
image_ref: ${{ steps.env_var.outputs.image_ref }}
67+
68+
steps:
69+
- name: Checkout Repository
70+
uses: actions/checkout@v2
71+
72+
- name: Set up Docker Buildx
73+
uses: docker/setup-buildx-action@v1.5.1
74+
75+
- name: Cache Docker layers
76+
uses: actions/cache@v2.1.6
77+
with:
78+
path: /tmp/.buildx-cache
79+
key: ohw-docker-buildx-${{ inputs.image_name }}-${{ github.sha }}
80+
restore-keys: |
81+
ohw-docker-buildx-${{ inputs.image_name }}
82+
83+
- name: Set Job Environment Variables
84+
id: env_var
85+
run: |
86+
SHA7="${GITHUB_SHA::7}"
87+
DOCKER_TAG=$SHA7
88+
IMAGE_NAME="ghcr.io/${{ github.repository_owner }}/${{ inputs.image_name }}"
89+
echo "DOCKER_TAG=${{ inputs.image_tag }}" >> $GITHUB_ENV
90+
echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_ENV
91+
echo "::set-output name=image_name::${IMAGE_NAME})"
92+
echo "::set-output name=image_ref::${DOCKER_TAG})"
93+
94+
- name: Build Docker Image
95+
uses: docker/build-push-action@v2.6.1
96+
with:
97+
tags: |
98+
${{ env.IMAGE_NAME }}:${{ env.DOCKER_TAG }}
99+
cache-from: type=local,src=/tmp/.buildx-cache
100+
cache-to: type=local,dest=/tmp/.buildx-cache-new
101+
push: false
102+
load: true
103+
context: ${{ inputs.working_directory }}
104+
105+
- name: Docker image sizes
106+
run: |
107+
docker images | grep ${{ env.IMAGE_NAME }}
108+
109+
echo "### Image sizes" >> $GITHUB_STEP_SUMMARY
110+
docker images | grep ${{ env.IMAGE_NAME }} >> $GITHUB_STEP_SUMMARY
111+
112+
- name: Export Full Conda Environment
113+
run: |
114+
docker run ${{ env.IMAGE_NAME }}:${{ env.DOCKER_TAG }} pixi list > conda-packages.txt
115+
116+
echo "### Conda Environment" >> $GITHUB_STEP_SUMMARY
117+
cat conda-packages.txt >> $GITHUB_STEP_SUMMARY
118+
119+
- name: Archive Conda Package List
120+
uses: actions/upload-artifact@v1
121+
with:
122+
name: conda-packages
123+
path: conda-packages.txt
124+
125+
- name: "Log into GitHub Container Registery"
126+
uses: docker/login-action@v1.9.0
127+
if: ${{ inputs.push_image}}
128+
with:
129+
registry: ghcr.io
130+
username: ${{ github.repository_owner }}
131+
password: ${{ secrets.GH_TOKEN || secrets.GITHUB_TOKEN }}
132+
133+
- name: Push Docker Image to GitHub Container Registry
134+
if: ${{ inputs.push_image }}
135+
run: docker push ${{ env.IMAGE_NAME }}:${{ env.DOCKER_TAG }}
136+
137+
- name: Notify on newly built image
138+
if: ${{ inputs.push_image }}
139+
uses: slackapi/slack-github-action@v1.18.0
140+
env:
141+
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
142+
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
143+
with:
144+
payload: |
145+
{
146+
"text": "Built image ${{ env.IMAGE_NAME }}:${{ env.DOCKER_TAG }}. Maybe let 2i2c know?",
147+
"blocks": [
148+
{
149+
"type": "section",
150+
"text": {
151+
"type": "mrkdwn",
152+
"text": "Built image `${{ env.IMAGE_NAME }}:${{ env.DOCKER_TAG }}`. Maybe let 2i2c know?"
153+
}
154+
}
155+
]
156+
}
157+
158+
- name: Move Docker Cache
159+
run: |
160+
rm -rf /tmp/.buildx-cache
161+
mv /tmp/.buildx-cache-new /tmp/.buildx-cache

.github/workflows/py-image.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919

2020
build_push:
2121
needs: [shortsha]
22-
uses: ./.github/workflows/build-image.yml
22+
uses: ./.github/workflows/build-pixi-image.yml
2323
with:
2424
working_directory: ./py-base
2525
image_tag: ${{ needs.shortsha.outputs.shortsha }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
106+
# Mac
107+
**/.DS_Store

py-base/.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# GitHub syntax highlighting
2+
pixi.lock linguist-language=YAML
3+

py-base/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# pixi environments
2+
.pixi
3+
*.egg-info
4+

py-base/CONDARC

Lines changed: 0 additions & 2 deletions
This file was deleted.

py-base/Dockerfile

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,33 @@
1-
#syntax=docker/dockerfile:1.3
2-
FROM continuumio/miniconda3:4.12.0@sha256:58b1c7df8d69655ffec017ede784a075e3c2e9feff0fc50ef65300fc75aa45ae
1+
#syntax=docker/dockerfile:1.7
2+
FROM ghcr.io/prefix-dev/pixi:0.26.1-jammy@sha256:45d86bb788aaa24d215eff57712f250329486ca8f442b91959bc9ce7ce6e053c
33

44
ENV NB_USER jovyan
55
ENV NB_UID 1000
66
ENV HOME /home/jovyan
77

8-
ENV CONDA_DIR /srv/conda
9-
ENV CONDA_ENV base
8+
ENV PIXI_DIR /srv/pixi_env
109

1110
# Output logging faster
1211
ENV PYTHONUNBUFFERED 1
13-
# Don't write bytecode
14-
ENV PYTHONDONTWRITEBYTECODE 1
1512

1613
USER root
1714
RUN adduser --disabled-password --gecos "Default Jupyter user" ${NB_USER} \
18-
&& echo ". ${CONDA_DIR}/etc/profile.d/conda.sh ; conda activate ${CONDA_ENV}" > /etc/profile.d/init_conda.sh \
19-
&& chown -R ${NB_USER}:${NB_USER} /srv
15+
&& chown -R ${NB_USER}:${NB_USER} /srv \
16+
&& mkdir -p ${PIXI_DIR} \
17+
&& chown -R ${NB_USER}:${NB_USER} ${PIXI_DIR}
2018

21-
WORKDIR ${HOME}
22-
USER ${USER}
2319

24-
COPY ./conda-linux-64.lock /tmp/
20+
WORKDIR ${PIXI_DIR}
21+
USER ${NB_USER}
2522

26-
RUN --mount=type=cache,id=ohw_py,target=/opt/conda/pkgs,uid=${NB_UID},gid=${NB_UID} \
27-
conda install --name ${CONDA_ENV} --file /tmp/conda-linux-64.lock && \
28-
# micromamba install --name ${CONDA_ENV} --file environment.yml && \
29-
find -name '*.a' -delete && \
30-
# rm -rf /opt/conda/conda-meta && \
31-
rm -rf ${CONDA_DIR}/include && \
32-
find -name '__pycache__' -type d -exec rm -rf '{}' '+'
23+
COPY ./pixi.toml ./pixi.lock ${PIXI_DIR}/
3324

34-
COPY CONDARC ./.condarc
35-
COPY --chown=${NB_USER} entrypoint.sh /opt/entrypoint.sh
25+
RUN --mount=type=cache,id=ohw_py,target=/home/jovyan/.cache/rattler/cache,uid=${NB_UID},gid=${NB_UID} \
26+
pixi install --frozen -e default
3627

37-
# USER root
38-
# RUN chown -R jovyan ${CONDA_DIR}
39-
USER ${NB_USER}
28+
RUN pixi shell-hook --frozen -e default > /srv/shell-hook.sh \
29+
&& echo 'exec "$@"' >> /srv/shell-hook.sh
4030

41-
ENTRYPOINT [ "/opt/entrypoint.sh" ]
31+
ENTRYPOINT ["/bin/bash", "/srv/shell-hook.sh"]
32+
33+
WORKDIR ${HOME}

0 commit comments

Comments
 (0)