Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use composite actions for python scripts #22

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
bdf800c
temp: try using composite action for scripts
mariajgrimaldi Sep 26, 2024
f8f4788
temp: checkout current repository
mariajgrimaldi Sep 26, 2024
6ff86c8
temp: checkout workflow repository to execute
mariajgrimaldi Sep 26, 2024
51b66c2
fix: use action.yml instead of actions.yml
mariajgrimaldi Sep 26, 2024
23b7a6b
temp: show directory contents
mariajgrimaldi Sep 26, 2024
48045b3
fix: use relative path for picasso
mariajgrimaldi Sep 26, 2024
b20e40f
temp: print current directory and its contents
mariajgrimaldi Sep 26, 2024
9251713
temp: print current directory and its contents
mariajgrimaldi Sep 26, 2024
30a47ed
fix: clone in subpaths instead
mariajgrimaldi Sep 26, 2024
ea95ffd
fix: pass required keys as multiple arguments
mariajgrimaldi Sep 26, 2024
48e3239
fix: use picasso subpath for python script
mariajgrimaldi Sep 26, 2024
d3f5240
fix: remove relative reference
mariajgrimaldi Sep 26, 2024
9f50fc4
fix: fetch picasso repository submodules
mariajgrimaldi Sep 26, 2024
ba155c6
fix: print picasso path
mariajgrimaldi Sep 26, 2024
e6dd323
fix: try executing from absolute path
mariajgrimaldi Sep 26, 2024
4c04565
fix: pass config file path when executing workflow
mariajgrimaldi Sep 26, 2024
77d2a38
temp: print content of strains folder
mariajgrimaldi Sep 26, 2024
2be8034
temp: print strain path
mariajgrimaldi Sep 26, 2024
f5c0f78
fix: pass down strain path to action
mariajgrimaldi Sep 26, 2024
221fa65
refactor: remove debug statements
mariajgrimaldi Sep 27, 2024
9e28382
refactor: run black on the python script / remove unnecessary configs
mariajgrimaldi Sep 27, 2024
ca1f975
fix: go back to relative paths
mariajgrimaldi Sep 27, 2024
b6bc6b4
refactor: add error management to action and let script self-contained
mariajgrimaldi Sep 27, 2024
31c9245
refactor: manage errors gracefully
mariajgrimaldi Sep 27, 2024
6315adb
refactor: manage errors gracefully
mariajgrimaldi Sep 27, 2024
131d25f
temp: add debug logs for composite action
mariajgrimaldi Sep 27, 2024
1affe7a
temp: add debug logs for composite action
mariajgrimaldi Sep 27, 2024
8740736
temp: remove exit 1 to log error
mariajgrimaldi Sep 27, 2024
e1239f7
temp: capture output from python script
mariajgrimaldi Sep 27, 2024
c4421be
refactor: use sys.exit with error message instead
mariajgrimaldi Sep 28, 2024
e738b18
refactor: change to double quotes for consistency
mariajgrimaldi Sep 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/actions/get-tutor-config/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Get and set Tutor configuration
description: "Get Tutor configuration values and set them as environment variables"

inputs:
STRAIN_PATH:
description: "The path within the repository for strains"
required: true
type: string

runs:
using: "composite"
steps:

- name: Install Python and dependencies
run: |
pip install pyyaml
shell: bash

- name: Get Tutor Configurations from config.yml and set them as an environment variable
env:
REQUIRED_KEYS: TUTOR_VERSION TUTOR_APP_NAME
OPTIONAL_KEYS: DOCKER_REGISTRY
CONFIG_FILE: strains/${{ inputs.STRAIN_PATH }}/config.yml
SCRIPT_PATH: picasso/.github/workflows/actions/get-tutor-config/get_tutor_config.py
run: |
env_vars=$(python $SCRIPT_PATH --config-file $CONFIG_FILE --required-keys $REQUIRED_KEYS --optional-keys $OPTIONAL_KEYS)
echo "$env_vars" >> $GITHUB_ENV
shell: bash
112 changes: 112 additions & 0 deletions .github/workflows/actions/get-tutor-config/get_tutor_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""Script to get Tutor configuration from a specified configuration file.

To use this script, you need to:

1. Install the `pyyaml` package by running `pip install pyyaml`.
2. Run the script within the same directory as the configuration file.
3. Pass the required and optional keys as command-line arguments.
4. The script will print the environment variables based on the keys.
5. Set the environment variables in the GitHub Actions workflow.
"""

import os
import sys
import yaml
import argparse


def load_config(file_path):
"""Loads the YAML configuration from the specified file.

Args:
file_path (str): The path to the configuration file.

Returns:
dict: The configuration data.
"""
if not os.path.exists(file_path):
sys.exit("ERROR: file config.yml doesn't exist")

with open(file_path, "r", encoding="utf-8") as file:
return yaml.safe_load(file)


def check_required_keys(config, required_keys):
"""Checks if the required keys are present in the config.

Args:
config (dict): The configuration data.
required_keys (list): The list of required keys.
"""
for key in required_keys:
if key not in config:
sys.exit(f"ERROR: key {key} not found in config.yml")


def collect_env_vars(config, required_keys, optional_keys):
"""Collects the environment variables from the config.

Args:
config (dict): The configuration data.
required_keys (list): The list of required keys.
optional_keys (list): The list of optional keys.
"""
env_vars = []

for key in required_keys:
env_vars.append(f"{key}={config[key]}")

for key in optional_keys:
if key in config:
env_vars.append(f"{key}={config[key]}")

return env_vars


def parse_args():
"""Parse command-line arguments for required and optional keys."""
parser = argparse.ArgumentParser(
description="Process required and optional keys for config."
)

parser.add_argument(
"--config-file",
default="config.yml",
help="The path to the configuration",
)
parser.add_argument(
"--required-keys",
nargs="+",
default=["TUTOR_VERSION", "TUTOR_APP_NAME"],
help="List of required keys to look for in config.yml.",
)
parser.add_argument(
"--optional-keys",
nargs="+",
default=["DOCKER_REGISTRY"],
help="List of optional keys to look for in config.yml.",
)

return parser.parse_args()


def main(config_file="config.yml", required_keys=None, optional_keys=None):
"""Main function to load config, validate keys, and print environment variables.

Args:
config_file (str): The path to the configuration file.
required_keys (list): The list of required keys.
optional_keys (list): The list of optional keys.
"""
file_path = os.path.join(os.getcwd(), config_file)
tutor_config = load_config(file_path)

check_required_keys(tutor_config, required_keys)

env_vars = collect_env_vars(tutor_config, required_keys, optional_keys)

print("\n".join(env_vars))


if __name__ == "__main__":
main(**vars(parse_args()))
55 changes: 18 additions & 37 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,54 +49,35 @@ jobs:
runs-on: ubuntu-latest

steps:

- name: Checkout Picasso repository
uses: actions/checkout@v4
with:
repository: edunext/picasso
ref: MJG/maintainable-scripts
ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
path: picasso

- name: Checkout strains repository
uses: actions/checkout@v4
with:
repository: ${{ inputs.STRAIN_REPOSITORY }}
ref: ${{ inputs.STRAIN_REPOSITORY_BRANCH }}
ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
path: strains

- name: Install necessary dependencies
run: |
pip install pyyaml
pip install git+https://github.com/eduNEXT/tvm.git

- name: Get Tutor configuration values and set them as environment variables
shell: python
working-directory: ${{ inputs.STRAIN_PATH }}
run: |
import os
import sys
import yaml

file_path = os.path.join(os.getcwd(), "config.yml")
if not os.path.exists(file_path):
print("ERROR: file config.yml doesn't exist")
sys.exit(1)

with open(file_path, "r", encoding="utf-8") as file:
tutor_config = yaml.safe_load(file)

required_keys = ["TUTOR_VERSION", "TUTOR_APP_NAME"]
optional_keys = ["DOCKER_REGISTRY"]
env_vars = []

for key in required_keys:
if key not in tutor_config:
print(f"ERROR: key {key} not found in config.yml")
sys.exit(1)
env_vars.append(f"{key}={tutor_config[key]}")

for key in optional_keys:
if key in tutor_config:
env_vars.append(f"{key}={tutor_config[key]}")

with open(os.environ["GITHUB_ENV"], "a") as file:
file.write("\n".join(env_vars))
uses: ./picasso/.github/workflows/actions/get-tutor-config
with:
STRAIN_PATH: ${{ inputs.STRAIN_PATH }}

- name: Configure TVM project
shell: bash
working-directory: ${{ inputs.STRAIN_PATH }}
working-directory: strains/${{ inputs.STRAIN_PATH }}
run: |
tvm install $TUTOR_VERSION
tvm project init $TUTOR_APP_NAME $TUTOR_VERSION
Expand All @@ -107,7 +88,7 @@ jobs:

- name: Enable and install picasso plugin in Tutor environment
shell: bash
working-directory: ${{ inputs.STRAIN_PATH }}/${{ env.TUTOR_APP_NAME }}
working-directory: strains/${{ inputs.STRAIN_PATH }}/${{ env.TUTOR_APP_NAME }}
run: |
. .tvm/bin/activate

Expand All @@ -126,7 +107,7 @@ jobs:

- name: Execute extra commands
shell: bash
working-directory: ${{ inputs.STRAIN_PATH }}/${{ env.TUTOR_APP_NAME }}
working-directory: strains/${{ inputs.STRAIN_PATH }}/${{ env.TUTOR_APP_NAME }}
run: |
. .tvm/bin/activate
tutor picasso run-extra-commands
Expand Down Expand Up @@ -161,7 +142,7 @@ jobs:

- name: Build service image with no cache
shell: bash
working-directory: ${{ inputs.STRAIN_PATH }}/${{ env.TUTOR_APP_NAME }}
working-directory: strains/${{ inputs.STRAIN_PATH }}/${{ env.TUTOR_APP_NAME }}
env:
SERVICE: ${{ inputs.SERVICE }}
run: |
Expand All @@ -170,7 +151,7 @@ jobs:
tutor images build $SERVICE --no-cache

- name: Push service image to registry
working-directory: ${{ inputs.STRAIN_PATH }}/${{ env.TUTOR_APP_NAME }}
working-directory: strains/${{ inputs.STRAIN_PATH }}/${{ env.TUTOR_APP_NAME }}
env:
SERVICE: ${{ inputs.SERVICE }}
run: |
Expand Down