Skip to content

Commit 2b8bbd6

Browse files
authored
fix(dockerfile): improve documentation and change working directory (#4)
- Installs code at /home/speckle to prevent collisions with files & directories in the root (/) directory - Updates the README and provides instructions for building and testing the docker image locally - Updated inline documentation in Dockerfile
1 parent 522ce59 commit 2b8bbd6

File tree

2 files changed

+61
-24
lines changed

2 files changed

+61
-24
lines changed

Dockerfile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
# We use the official Python 3.11 image as our base image and will add our code to it. For more details, see https://hub.docker.com/_/python
12
FROM python:3.11-slim
23

4+
# We install poetry to generate a list of dependencies which will be required by our application
35
RUN pip install poetry
46

5-
COPY . .
6-
RUN poetry export -f requirements.txt --output requirements.txt && pip install -r requirements.txt
7+
# We set the working directory to be the /home/speckle directory; all of our files will be copied here.
8+
WORKDIR /home/speckle
9+
10+
# Copy all of our code and assets from the local directory into the /home/speckle directory of the container.
11+
# We also ensure that the user 'speckle' owns these files, so it can access them
12+
# This assumes that the Dockerfile is in the same directory as the rest of the code
13+
COPY . /home/speckle
14+
15+
# Using poetry, we generate a list of requirements, save them to requirements.txt, and then use pip to install them
16+
RUN poetry export --format requirements.txt --output /home/speckle/requirements.txt && pip install --requirement /home/speckle/requirements.txt

README.md

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Speckle Automate function template - Python
22

3-
43
This is a template repository for a Speckle Automate functions written in python
54
using the [specklepy](https://pypi.org/project/specklepy/) SDK to interact with Speckle data.
65

@@ -24,20 +23,9 @@ describe how the launch.json should be edited
2423

2524
### Github Codespaces
2625

27-
create new repo from template, and use the create new code
28-
29-
30-
### Local dev environment
31-
32-
33-
34-
35-
# Archive
26+
Create a new repo from this template, and use the create new code.
3627

37-
This is a simple example of how to use the Speckle Automate Python package to automate the creation of a Speckle stream.
38-
39-
40-
## Using this Speckle Function
28+
### Using this Speckle Function
4129

4230
1. [Create](https://automate.speckle.dev/) a new Speckle Automation.
4331
1. Select your Speckle Project and Speckle Model.
@@ -47,14 +35,10 @@ This is a simple example of how to use the Speckle Automate Python package to au
4735

4836
## Getting Started with creating your own Speckle Function
4937

50-
1. [Fork](https://docs.github.com/en/get-started/quickstart/fork-a-repo) this repository.
51-
1. [Clone](https://docs.github.com/en/get-started/quickstart/fork-a-repo#cloning-your-forked-repository) your forked repository to your development environment, or use [GitHub CodeSpaces](https://github.com/features/codespaces).
52-
1. [Register](https://automate.speckle.dev/) your Function with [Speckle Automate](https://automate.speckle.dev/).
53-
1. After completing the registration of the Function you will be shown a Function Publish Token and a Function ID. You will need these later.
54-
1. Save your Function Publish Token as a [GitHub Action Secret](https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository) named `SPECKLE_AUTOMATE_FUNCTION_PUBLISH_TOKEN`.
55-
1. Save your Function ID as a [GitHub Action Secret](https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository) named `SPECKLE_AUTOMATE_FUNCTION_ID`.
38+
1. [Register](https://automate.speckle.dev/) your Function with [Speckle Automate](https://automate.speckle.dev/) and select the Python template.
39+
1. A new repository will be created in your GitHub account.
5640
1. Make changes to your Function in `main.py`. See below for the Developer Requirements, and instructions on how to test.
57-
1. Every commit to `main` branch will create a new version of your Speckle Function.
41+
1. To create a new version of your Function, create a new [GitHub release](https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository) in your repository.
5842

5943
## Developer Requirements
6044

@@ -66,7 +50,50 @@ This is a simple example of how to use the Speckle Automate Python package to au
6650
## Building and Testing
6751

6852
The code can be tested locally by running `poetry run pytest`.
69-
The code should also be packaged into the format required by Speckle Automate, a Docker Container Image, and that should also be tested.
53+
54+
### Building and running the Docker Container Image
55+
56+
Running and testing your code on your own machine is a great way to develop your Function; the following instructions are a bit more in-depth and only required if you are having issues with your Function in GitHub Actions or on Speckle Automate.
57+
58+
#### Building the Docker Container Image
59+
60+
Your code is packaged by the GitHub Action into the format required by Speckle Automate. This is done by building a Docker Image, which is then run by Speckle Automate. You can attempt to build the Docker Image yourself to test the building process locally.
61+
62+
To build the Docker Container Image, you will need to have [Docker](https://docs.docker.com/get-docker/) installed.
63+
64+
Once you have Docker running on your local machine:
65+
66+
1. Open a terminal
67+
1. Navigate to the directory in which you cloned this repository
68+
1. Run the following command:
69+
70+
```bash
71+
docker build -f ./Dockerfile -t speckle_automate_python_example .
72+
```
73+
74+
#### Running the Docker Container Image
75+
76+
Once the image has been built by the GitHub Action, it is sent to Speckle Automate. When Speckle Automate runs your Function as part of an Automation, it will run the Docker Container Image. You can test that your Docker Container Image runs correctly by running it locally.
77+
78+
1. To then run the Docker Container Image, run the following command:
79+
80+
```bash
81+
docker run --rm speckle_automate_python_example \
82+
python -u main.py run \
83+
'{"projectId": "1234", "modelId": "1234", "branchName": "myBranch", "versionId": "1234", "speckleServerUrl": "https://speckle.xyz", "automationId": "1234", "automationRevisionId": "1234", "automationRunId": "1234", "functionId": "1234", "functionName": "my function", "functionLogo": "base64EncodedPng"}' \
84+
'{}' \
85+
yourSpeckleServerAuthenticationToken
86+
```
87+
88+
Let's explain this in more detail:
89+
90+
`docker run --rm speckle_automate_python_example` tells Docker to run the Docker Container Image that we built earlier. `speckle_automate_python_example` is the name of the Docker Container Image that we built earlier. The `--rm` flag tells docker to remove the container after it has finished running, this frees up space on your machine.
91+
92+
The line `python -u main.py run` is the command that is run inside the Docker Container Image. The rest of the command is the arguments that are passed to the command. The arguments are:
93+
94+
- `'{"projectId": "1234", "modelId": "1234", "branchName": "myBranch", "versionId": "1234", "speckleServerUrl": "https://speckle.xyz", "automationId": "1234", "automationRevisionId": "1234", "automationRunId": "1234", "functionId": "1234", "functionName": "my function", "functionLogo": "base64EncodedPng"}'` - the metadata that describes the automation and the function.
95+
- `{}` - the input parameters for the function that the Automation creator is able to set. Here they are blank, but you can add your own parameters to test your function.
96+
- `yourSpeckleServerAuthenticationToken` - the authentication token for the Speckle Server that the Automation can connect to. This is required to be able to interact with the Speckle Server, for example to get data from the Model.
7097
7198
## Resources
7299

0 commit comments

Comments
 (0)