Skip to content

Commit

Permalink
feat: added tasks and workspace yaml files
Browse files Browse the repository at this point in the history
  • Loading branch information
whykay-01 committed Apr 24, 2024
1 parent 8bab15e commit cce3460
Show file tree
Hide file tree
Showing 2 changed files with 297 additions and 0 deletions.
286 changes: 286 additions & 0 deletions .tekton/tasks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
# These are custom tasks that are not on Tekton Hub
---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: pylint
labels:
app.kubernetes.io/version: "0.4"
annotations:
tekton.dev/categories: Code Quality
tekton.dev/pipelines.minVersion: "0.17.0"
tekton.dev/tags: python, pylint, poetry
tekton.dev/displayName: "pylint"
tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le"
spec:
workspaces:
- name: source
description: The workspace with the source code.
description: >-
Use to run pylint on the provided input source. If Poetry is being used
it will detect the poetry.lock file and install via requirements export.
params:
- name: image
description: The container image with pylint
default: docker.io/python:3.11-slim
- name: path
description: The path to the module which should be analyzed by pylint
default: "."
type: string
- name: args
description: The arguments to pass to the pylint CLI.
type: array
default: []
- name: requirements_file
description: The name of the requirements file inside the source location
default: "requirements.txt"
steps:
- name: pylint
image: $(params.image)
workingDir: $(workspaces.source.path)
script: |
#!/bin/bash
set -e
export PATH=$PATH:$HOME/.local/bin:
echo "***** Installing dependencies *****"
if [ -e "poetry.lock" ]; then
echo "Found poetry.lock file: using poetry "
python -m pip install poetry poetry-plugin-export
poetry export --with=dev -f requirements.txt --output requirements.txt
python -m pip install --user -r requirements.txt
elif [ -n "$(params.requirements_file)" ] && [ -e "$(params.requirements_file)" ]; then
python -m pip install --user -r "$(params.requirements_file)"
fi
# Make sure pylint is installed
python -m pip install pylint
echo "***** Running Linting *****"
pylint $@ "$(params.path)"
args:
- "$(params.args)"

---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: pytest-env
labels:
app.kubernetes.io/version: "0.1"
annotations:
tekton.dev/categories: Testing
tekton.dev/pipelines.minVersion: "0.17.0"
tekton.dev/tags: python, pytest
tekton.dev/displayName: "pytest tests"
tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le"
spec:
workspaces:
- name: source
description: >-
This task can be used to perform unit tests with pytest.
It supports both requirements.txt and poetry.lock files.
It also has the ability to create an environment variable
that is sourced from a Secret. This allows you to define
credentials that can be used to connect to a test database.
params:
- name: PYTEST_ARGS
description: The arguments to pass to the pytest CLI.
type: array
default: []
- name: SECRET_NAME
description: The name of the secret containing a database_uri key
type: string
default: "postgres-creds"
- name: SECRET_KEY
description: The name of the key that contains the database uri
type: string
default: "database_uri"
steps:
- name: pytest
image: docker.io/python:3.11-slim
workingDir: $(workspaces.source.path)
env:
- name: DATABASE_URI
valueFrom:
secretKeyRef:
name: $(params.SECRET_NAME)
key: $(params.SECRET_KEY)
script: |
#!/bin/bash
set -e
export PATH=$PATH:$HOME/.local/bin:
echo "***** Installing dependencies *****"
if [ -e "poetry.lock" ]; then
echo "Found poetry.lock file: using poetry "
python -m pip install poetry poetry-plugin-export
poetry export --with=dev -f requirements.txt --output requirements.txt
python -m pip install --user -r requirements.txt
elif -e "requirements.txt" ]; then
python -m pip install --user -r requirements.txt
fi
# Make sure pylint is installed
python -m pip install pytest
echo "***** Running Tests *****"
pytest --version
pytest
args:
- "$(params.PYTEST_ARGS)"

---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: deploy-image
labels:
app.kubernetes.io/version: "0.1"
annotations:
tekton.dev/categories: Deployment
tekton.dev/pipelines.minVersion: "0.17.0"
tekton.dev/tags: openshift, deploy
tekton.dev/displayName: "deploy image"
tekton.dev/platforms: "linux/amd64"
spec:
workspaces:
- name: source
description: >-
This task will update the deployment.yaml with the latest image name
and then apply that yaml file and it's service file.
params:
- name: image_name
description: The fully qualified name of the new image to deploy
type: string
- name: manifest_dir
description: The directory in source that contains yaml manifests
type: string
default: "k8s"
steps:
- name: deploy
image: quay.io/openshift/origin-cli:latest
workingDir: /workspace/source
command: ["/bin/bash", "-c"]
args:
- |-
#!/bin/bash
set -e
echo Applying manifests in $(inputs.params.manifest_dir) directory
echo "**********************************************************************"
echo "Installing YQ..."
echo "**********************************************************************"
wget -qO /usr/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
chmod a+x /usr/bin/yq
echo "********************* DEPLOYMENT ***********************"
echo "Deploying $(inputs.params.image_name) ..."
yq -e -i '.spec.template.spec.containers[0].image="$(inputs.params.image_name)"' $(inputs.params.manifest_dir)/deployment.yaml
# sed -i 's|'"$(inputs.params.old_image_name)"'|'"$(inputs.params.image_name)"'|g' $(inputs.params.manifest_dir)/deployment.yaml
cat $(inputs.params.manifest_dir)/deployment.yaml
echo "************************************************************"
echo "OC APPLY..."
oc apply -f $(inputs.params.manifest_dir)/deployment.yaml
oc apply -f $(inputs.params.manifest_dir)/service.yaml
echo "************************************************************"
sleep 3
echo "Pods:"
oc get pods
echo ""
---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: apply-manifests
labels:
app.kubernetes.io/version: "0.1"
annotations:
tekton.dev/categories: Deployment
tekton.dev/pipelines.minVersion: "0.17.0"
tekton.dev/tags: openshift, deploy
tekton.dev/displayName: "deploy"
tekton.dev/platforms: "linux/amd64"
spec:
workspaces:
- name: source
description: >-
This task will deploy all of the yaml files in the manifest folder.
params:
- name: manifest_dir
description: The directory in source that contains yaml manifests
type: string
default: "k8s"
steps:
- name: apply
image: quay.io/openshift/origin-cli:latest
workingDir: /workspace/source
command: ["/bin/bash", "-c"]
args:
- |-
echo Applying manifests in $(inputs.params.manifest_dir) directory
oc apply -f $(inputs.params.manifest_dir)
echo -----------------------------------
---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: behave
labels:
app.kubernetes.io/version: "0.1"
annotations:
tekton.dev/categories: Testing
tekton.dev/pipelines.minVersion: "0.17.0"
tekton.dev/tags: python, bdd, behave
tekton.dev/displayName: "bdd tests"
tekton.dev/platforms: "linux/amd64"
spec:
workspaces:
- name: source
description: >-
This task can be used to perform bdd tests with behave.
params:
- name: BASE_URL
description: The url of the application to test
type: string
- name: WAIT_SECONDS
description: The number of seconds to wait for a reply
type: string
default: "60"
- name: DRIVER
description: The web driver to use (chrome or firefox)
type: string
default: "chrome"
steps:
- name: behave
image: rofrano/pipeline-selenium
workingDir: $(workspaces.source.path)
env:
- name: BASE_URL
value: $(params.BASE_URL)
- name: WAIT_SECONDS
value: $(params.WAIT_SECONDS)
- name: DRIVER
value: $(params.DRIVER)
script: |
#!/bin/bash
set -e
export PATH=$PATH:$HOME/.local/bin:
echo "***** Installing dependencies *****"
if [ -e "poetry.lock" ]; then
echo "Found poetry.lock file: using poetry"
python -m pip install poetry poetry-plugin-export
poetry export --with=dev -f requirements.txt --output requirements.txt
fi
python -m pip install --user -r requirements.txt
echo "***** Running Tests *****"
behave
11 changes: 11 additions & 0 deletions .tekton/workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pipeline-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
volumeMode: Filesystem

0 comments on commit cce3460

Please sign in to comment.