Skip to content

Commit

Permalink
add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingrismore committed Nov 14, 2023
1 parent 0479de1 commit a2c15d0
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 50 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/prefect_deploy_prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Get changed requirements file
- name: Get changed dependencies
id: changed-files-requirements
uses: tj-actions/changed-files@v40
with:
files: requirements.txt
files: |
requirements.txt
Dockerfile
- name: Get changed prefect.yaml file
id: changed-files-prefect
uses: tj-actions/changed-files@v40
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/prefect_deploy_stg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ jobs:
id: changed-files-requirements
uses: tj-actions/changed-files@v40
with:
files: requirements.txt
files: |
requirements.txt
Dockerfile
- name: Get changed prefect.yaml file
id: changed-files-prefect
uses: tj-actions/changed-files@v40
Expand Down
118 changes: 118 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Scenario

This repository supposes the following, for the sake of example:

- You have two Prefect workspaces, one used for staging and one used for production
- You have one or more deployments which should be mirrored across workspaces
- Your deployments are configured to use git code storage, and the branch a deployment uses should differ per workspace
- Flows run in containers, so images need to be rebuilt when dependencies or build steps change. Since dependencies can differ per branch/workspace, each branch/workspace pairing should have its own image

# Select Actions

`select_actions` allows users to choose from a list of predefined actions for their `build`, `push`, and `pull` steps. When `select_actions` is invoked, any combination of `build`, `push`, and `pull` may be provided. These steps must be defined under the `actions` portion of a `prefect.yaml` file. Each action selected will override the default step definition at the root level of your `prefect.yaml`.

## Example

This example uses `select_actions.py` as part of a Github Actions workflow to modify `prefect.yaml` according to a set of triggers and conditions.

```
actions:
build:
build_image_stg:
- 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: kevingrismoreprefect/github-actions-demo-stg
tag: '{{ get-commit-hash.stdout }}'
dockerfile: Dockerfile
build_image_prod:
- 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: kevingrismoreprefect/github-actions-demo-prod
tag: '{{ get-commit-hash.stdout }}'
dockerfile: Dockerfile
push:
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: null
push: null
pull: null
```

`python select_actions.py --build build_image --push push_image --pull production`

```
actions:
build:
build_image_stg: &id001
- 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: kevingrismoreprefect/github-actions-demo-stg
tag: '{{ get-commit-hash.stdout }}'
dockerfile: Dockerfile
build_image_prod:
- 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: kevingrismoreprefect/github-actions-demo-prod
tag: '{{ get-commit-hash.stdout }}'
dockerfile: Dockerfile
push:
push_image: &id002
- 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: &id003
- prefect.deployments.steps.git_clone:
repository: [email protected]:kevingrismore/prefect-select-actions.git
branch: main
credentials: '{{ prefect.blocks.github-credentials.my-block-name }}'
build: *id001
push: *id002
pull: *id003
```
86 changes: 41 additions & 45 deletions prefect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,48 @@
name: prefect-select-actions
prefect-version: 2.14.3

definitions:
actions:
build:
no_action:
build_image_stg:
- 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: kevingrismoreprefect/github-actions-demo-stg
tag: "{{ get-commit-hash.stdout }}"
dockerfile: Dockerfile
build_image_prod:
- 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: kevingrismoreprefect/github-actions-demo-prod
tag: "{{ get-commit-hash.stdout }}"
dockerfile: Dockerfile
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 }}"
actions:
build:
build_image_stg:
- 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: kevingrismoreprefect/github-actions-demo-stg
tag: "{{ get-commit-hash.stdout }}"
dockerfile: Dockerfile
build_image_prod:
- 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: kevingrismoreprefect/github-actions-demo-prod
tag: "{{ get-commit-hash.stdout }}"
dockerfile: Dockerfile
push:
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
Expand Down
5 changes: 3 additions & 2 deletions select_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def edit_yaml(step: str, action: str) -> None:

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

with open("prefect.yaml", "w") as f:
yaml.dump(data, f)
Expand All @@ -34,7 +34,8 @@ def select_actions(build: str, push: str, pull: str) -> None:
}

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


if __name__ == "__main__":
Expand Down

0 comments on commit a2c15d0

Please sign in to comment.