Skip to content

Commit

Permalink
skip docker build if no change
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingrismore committed Nov 9, 2023
0 parents commit 720c7b0
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/prefect_deploy_prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Deploy production flows
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on:
push:
branches:
- main
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
-steps:
- name: Check out repository code
uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- run: pip install -r requirements.txt
- name: Get changed requirements file
id: changed-files-requirements
uses: tj-actions/changed-files@v40
with:
files: requirements.txt
- name: Run step if any file(s) in the docs folder change
if: steps.changed-files-requirements.outputs.any_changed == 'true'
run: |
echo "Dependencies in requirements.txt have changed."
echo "Building and pushing docker image during deployment."
python select_actions.py --build build_image --push push_image --pull staging
if: steps.changed-files-requirements.outputs.any_changed == 'false'
run: |
echo "Dependencies in requirements.txt have not changed."
echo "Skipping image build and push."
python select_actions.py --build no_action --push no_action --pull staging
- run: prefect deploy --all
33 changes: 33 additions & 0 deletions .github/workflows/prefect_deploy_stg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Deploy staging flows
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on:
push:
branches:
- staging
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- run: pip install -r requirements.txt
- name: Get changed requirements file
id: changed-files-requirements
uses: tj-actions/changed-files@v40
with:
files: requirements.txt
- name: Run step if any file(s) in the docs folder change
if: steps.changed-files-requirements.outputs.any_changed == 'true'
run: |
echo "Dependencies in requirements.txt have changed."
echo "Building and pushing docker image during deployment."
python select_actions.py --build build_image --push push_image --pull staging
if: steps.changed-files-requirements.outputs.any_changed == 'false'
run: |
echo "Dependencies in requirements.txt have not changed."
echo "Skipping image build and push."
python select_actions.py --build no_action --push no_action --pull staging
- run: prefect deploy --all
41 changes: 41 additions & 0 deletions .prefectignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# prefect artifacts
.prefectignore

# python artifacts
__pycache__/
*.py[cod]
*$py.class
*.egg-info/
*.egg

# Type checking artifacts
.mypy_cache/
.dmypy.json
dmypy.json
.pyre/

# IPython
profile_default/
ipython_config.py
*.ipynb_checkpoints/*

# Environments
.python-version
.env
.venv
env/
venv/

# MacOS
.DS_Store

# Dask
dask-worker-space/

# Editors
.idea/
.vscode/

# VCS
.git/
.hg/
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM --platform=amd64/linux prefecthq/prefect:2.14.3-python3.11
COPY requirements.txt .
RUN pip install -r requirements.txt
10 changes: 10 additions & 0 deletions flows/my_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from prefect import flow


@flow(log_prints=True)
def my_flow():
print("Hello world!")


if __name__ == "__main__":
my_flow()
57 changes: 57 additions & 0 deletions prefect.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Welcome to your prefect.yaml file! You can use this file for storing and managing
# configuration for deploying your flows. We recommend committing this file to source
# control along with your flow code.

# Generic metadata about this project
name: prefect-select-actions
prefect-version: 2.14.3

definitions:
actions:
build:
no_action:
build_image:
- prefect.deployments.steps.run_shell_script:
id: get-commit-hash
script: git rev-parse --short HEAD
stream_output: false
- prefect_docker.deployments.steps.build_docker_image:
id: build-image
requires: prefect-docker
image_name: us-central1-docker.pkg.dev/my-project/my-repo/my-image
tag: "{{ get-commit-hash.stdout }}"
dockerfile: Dockerfile
platform: linux/amd64
push:
no_action:
push_image:
- prefect_docker.deployments.steps.push_docker_image:
requires: prefect-docker
image_name: "{{ build-image.image_name }}"
tag: "{{ build-image.tag }}"
pull:
staging:
- prefect.deployments.steps.git_clone:
repository: [email protected]:kevingrismore/prefect-select-actions.git
branch: staging
credentials: "{{ prefect.blocks.github-credentials.my-block-name }}"
production:
- prefect.deployments.steps.git_clone:
repository: [email protected]:kevingrismore/prefect-select-actions.git
branch: main
credentials: "{{ prefect.blocks.github-credentials.my-block-name }}"

# build section allows you to manage and build docker images
build: null

push: null

pull: null

deployments:
- name: my-deployment
flow_name: my-flow
entrypoint: flows/my_flow.py:my_flow
work_pool:
name: demo-pool
work_queue_name: default
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
prefect==2.14.4
41 changes: 41 additions & 0 deletions select_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Tool for selecting build, push, and pull actions in Prefect deployments."""
import click
import ruamel.yaml


def edit_yaml(step: str, action: str) -> None:
"""Reads in prefect.yaml and applies a specified action to a deployment step.
Args:
step (str): The name of the Prefect deployment step: build, push, or pull.
action (str): The name of action to run on the step, as name in prefect.yaml.
"""

yaml = ruamel.yaml.YAML()

with open("prefect.yaml", "r") as f:
data = yaml.load(f)
data[step] = data["definitions"]["actions"][step][action]

with open("prefect.yaml", "w") as f:
yaml.dump(data, f)


@click.command()
@click.option("--build")
@click.option("--push")
@click.option("--pull")
def select_actions(build: str, push: str, pull: str) -> None:
"""Calls edit_yaml for each step in the Prefect deployment pipeline."""
actions = {
"build": build,
"push": push,
"pull": pull,
}

for step, action in actions.items():
edit_yaml(step, action)


if __name__ == "__main__":
select_actions()

0 comments on commit 720c7b0

Please sign in to comment.