Skip to content

Commit

Permalink
[KED-2961] Move scripts from private-kedro-sync (#1107)
Browse files Browse the repository at this point in the history
* Pre-tidy

* Simplify linux e2e_tests

* Simplify linux e2e_tests

* Move e2e_tests from command to job

* String python version

* Comment out unwanted tests for now

* Try parametrised command name

* Try conditionals

* Indent

* Indent

* Urgh yaml

* Pass arguments

* Try win_e2e_tests separately

* Try win_e2e_tests separately

* Oops

* Fix windows hopefully

* URgh yaml

* Alter lint to matrix

* De-parametrise executor

* Rename executors

* Oops

* Add win_pip_compile

* Add win unit tests

* Add viz build

* Cosmetic changes

* Refactor into setup and win_setup

* Refactor into setup and win_setup

* Refactor into setup and win_setup

* Refactor into setup and win_setup

* Back to parametrised executor

* Back to parametrised executor

* Add unit_tests

* Clarify comment

* Add all files to tools/sync

* Remove unwanted workflows

* Alter file paths and remove clone_repo

* Replace build_kedro with one job

* Replace build_kedro with one job

* Trigger sync on merge to main

* Trigger on merge to main rather than nightly

* Remove old private-kedro ssh

* Merge into main config.yml file

* Add unless

* Correct to match schema

* Lint

* Tidy

* Hopefully correct a filepath?

* Alter PR message

* Update tools/circleci/github_scripts/merge.sh

Co-authored-by: Lorena Bălan <lorena.balan@quantumblack.com>

* Update requirements

* Remove SSH and pre-commit install

* Post-merge fix

* Update text

* Make two different CIRCLE_TOKENs

* Update .circleci/config.yml

* Move automatic merging to hourly rather than main_updated

* Move merge to above trigger release workflow

* Add git checkout after merge.sh

* Add git checkout after merge.sh

* New line!

Co-authored-by: Lorena Bălan <lorena.balan@quantumblack.com>
  • Loading branch information
antonymilne and Lorena Bălan authored Dec 17, 2021
1 parent 0ce3ecc commit 55b38b3
Show file tree
Hide file tree
Showing 11 changed files with 560 additions and 9 deletions.
189 changes: 180 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
version: 2.1

parameters:
release_kedro:
type: boolean
default: false

orbs:
win: circleci/windows@2.2.0

Expand Down Expand Up @@ -236,9 +241,6 @@ jobs:
python_version: <<parameters.python_version>>
steps:
- setup
- run:
name: Install pre-commit hooks
command: pre-commit install --install-hooks
- run:
name: Run linters
command: make lint
Expand Down Expand Up @@ -299,6 +301,130 @@ jobs:
name: Check for broken links
command: make linkcheck

sync:
docker:
# https://circleci.com/docs/2.0/circleci-images/#circleci-base-image
- image: cimg/base:2020.01
steps:
- checkout
- run:
name: Set git email and name
command: |
git config --global user.email "kedro@quantumblack.com"
git config --global user.name "QuantumBlack Labs"
- run:
name: Trigger Read The Docs build
command: ./tools/circleci/rtd-build.sh ${RTD_TOKEN} latest
- run:
name: Maybe merge main into develop or raise a PR
command: ./tools/circleci/github_scripts/merge.sh . "main" "develop" "${GITHUB_TAGGING_TOKEN}"
- run:
name: Maybe trigger the release workflow
command: |
KEDRO_VERSION=$(./tools/circleci/github_scripts/kedro_version.py ./kedro)
if ./tools/circleci/check-no-version-pypi.sh "${KEDRO_VERSION}"
then
echo "Starting the release of Kedro ${KEDRO_VERSION}!"
./tools/circleci/circle-release.sh github/quantumblacklabs/kedro
else
echo "Kedro version ${KEDRO_VERSION} already exists on PyPI, skipping..."
fi
merge_pr_to_develop:
docker:
# https://circleci.com/docs/2.0/circleci-images/#circleci-base-image
- image: cimg/base:2020.01
steps:
- checkout
- run:
name: Maybe merge an automatic PR into develop
command: ./tools/circleci/github_scripts/attempt_merge_pr.sh "merge-main-to-develop" "develop" "${GITHUB_TAGGING_TOKEN}"

build_docker_image:
docker:
- image: cimg/python:3.8
environment:
ECR_IMAGE_URL: 350138855857.dkr.ecr.eu-west-2.amazonaws.com/kedro-builder
AWS_REGION: eu-west-2
steps:
- setup_remote_docker:
docker_layer_caching: true
- checkout
- run:
name: Setup AWS CLI
command: pip install -U awscli
- run:
name: Login to AWS ECR
command: |
aws ecr get-login-password --region ${AWS_REGION} | \
docker login --username AWS --password-stdin "${ECR_IMAGE_URL}"
- run:
name: Build docker images
command: ./tools/circleci/docker_build_img/build.sh "." "${ECR_IMAGE_URL}"
no_output_timeout: 20m
- run:
name: Logout from AWS ECR
command: docker logout "${ECR_IMAGE_URL}"
when: always # logout even if the previous step has failed

# This is effectively just a combination of the lint, unit_tests and e2e_tests jobs.
# It's used to check that the nightly docker image is working ok and before publishing a release.
build_kedro:
parameters:
python_version:
type: string
executor:
name: docker
python_version: <<parameters.python_version>>
steps:
- setup
- run:
name: Run linters
command: make lint
- run:
name: Run unit tests
command: make test
- run:
name: Run e2e tests
command: make e2e-tests

publish_kedro:
executor:
name: docker
python_version: "3.7"
steps:
- setup
- run:
name: Check Kedro version
command: |
KEDRO_VERSION=$(./tools/circleci/github_scripts/kedro_version.py ./kedro)
if ./tools/circleci/check-no-version-pypi.sh "${KEDRO_VERSION}"
then
echo "export KEDRO_VERSION=\"${KEDRO_VERSION}\"" >> $BASH_ENV
else
echo "Error: Kedro version ${KEDRO_VERSION} already exists on PyPI"
exit 1
fi
- run:
name: Tag and publish release on Github
command: ./tools/circleci/github_scripts/release.sh quantumblacklabs kedro ${GITHUB_TAGGING_TOKEN} ${KEDRO_VERSION}
- run:
name: Publish to PyPI
command: |
make package
python -m pip install twine -U
python -m twine upload --repository-url ${TWINE_REPOSITORY_URL} dist/*
- run:
name: Trigger Read The Docs build
command: |
./tools/circleci/rtd-build.sh ${RTD_TOKEN} stable
# give some time for GitHub release to propagate
# otherwise RTD fails to build a new tag
sleep 120
./tools/circleci/rtd-build.sh ${RTD_TOKEN} ${KEDRO_VERSION}
# Trigger kedro-viz build to ensure tests in that project pass
viz_build:
docker:
Expand All @@ -309,7 +435,7 @@ jobs:
command: |
curl --location --request POST \
--url https://circleci.com/api/v2/project/github/quantumblacklabs/kedro-viz/pipeline \
--header "Circle-Token: $CIRCLE_TOKEN" \
--header "Circle-Token: $CIRCLE_VIZ_BUILD_TOKEN" \
--header 'content-type: application/json' \
--data '{"branch":"main"}'
Expand All @@ -324,6 +450,7 @@ jobs:
workflows:
version: 2.1
regular:
unless: <<pipeline.parameters.release_kedro>> # don't run if 'release_kedro' flag is set
jobs:
- e2e_tests:
matrix:
Expand Down Expand Up @@ -355,11 +482,6 @@ workflows:
python_version: ["3.6", "3.7", "3.8"]
- build_docs
- docs_linkcheck
- viz_build:
filters:
branches:
only: main

- all_circleci_checks_succeeded:
requires:
- e2e_tests
Expand All @@ -371,3 +493,52 @@ workflows:
- win_pip_compile
- build_docs
- docs_linkcheck

main_updated:
unless: <<pipeline.parameters.release_kedro>> # don't run if 'release_kedro' flag is set
jobs:
- sync:
filters:
branches:
only: main
- viz_build:
filters:
branches:
only: main

hourly_pr_merge:
triggers:
- schedule:
cron: 0 * * * *
filters:
branches:
only: main
jobs:
- merge_pr_to_develop

nightly_build:
triggers:
- schedule:
cron: 30 2 * * *
filters:
branches:
only: main
jobs:
- build_docker_image
- build_kedro:
matrix:
parameters:
python_version: ["3.6", "3.7", "3.8"]
requires:
- build_docker_image

kedro_release:
when: <<pipeline.parameters.release_kedro>> # only run if 'release_kedro' flag is set
jobs:
- build_kedro:
matrix:
parameters:
python_version: ["3.6", "3.7", "3.8"]
- publish_kedro:
requires:
- build_kedro
10 changes: 10 additions & 0 deletions tools/circleci/check-no-version-pypi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
KEDRO_VERSION=$1

PYPI_ENDPOINT="https://pypi.org/pypi/kedro/${KEDRO_VERSION}/json/"

STATUS_CODE=$(curl --location --silent \
--output /dev/null \
--write-out "%{http_code}\n" \
"${PYPI_ENDPOINT}")

[ "${STATUS_CODE}" == "404" ]
26 changes: 26 additions & 0 deletions tools/circleci/circle-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

# Exit script if you try to use an uninitialized variable.
set -o nounset

# Exit script if a statement returns a non-true return value.
set -o errexit

PROJECT_SLUG=$1
CIRCLE_ENDPOINT="https://circleci.com/api/v2/project/${PROJECT_SLUG}/pipeline"

PAYLOAD=$(cat <<-END
{
"branch": "${CIRCLE_BRANCH}",
"parameters": {"release_kedro": true}
}
END
)

curl -X POST \
--silent --show-error --fail --retry 3 \
--output /dev/null \
--header "Content-Type: application/json" \
--header "Circle-Token: ${CIRCLE_RELEASE_TOKEN}" \
--data "${PAYLOAD}" \
"${CIRCLE_ENDPOINT}"
28 changes: 28 additions & 0 deletions tools/circleci/docker_build_img/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM cimg/python:3.8

WORKDIR /home/circleci

RUN sudo apt-get update && \
sudo apt-get install curl pandoc openjdk-8-jdk-headless -y && \
sudo apt-get clean && \
sudo update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

# Update cacerts: https://stackoverflow.com/a/50103533/1684058
RUN sudo rm /etc/ssl/certs/java/cacerts && \
sudo update-ca-certificates -f

RUN curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
> miniconda.sh && bash miniconda.sh -b -p /home/circleci/miniconda

RUN sudo rm -rf ~/.pyenv/ /opt/circleci/.pyenv/

ARG PIP_REQS
ARG PY_VERSION
ARG CONDA_ENV_NAME=kedro_builder

# Install/Setup anaconda env
RUN bash -c "source /home/circleci/miniconda/etc/profile.d/conda.sh && \
echo \"$PIP_REQS\" > /tmp/requirements.txt && \
conda create --name=$CONDA_ENV_NAME python=$PY_VERSION && \
conda activate $CONDA_ENV_NAME && \
pip install --no-cache-dir --prefer-binary --upgrade -r /tmp/requirements.txt"
48 changes: 48 additions & 0 deletions tools/circleci/docker_build_img/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
set -e
cd "$(dirname "$0")"

KEDRO_REPO=$1
ECR_IMAGE_URL=$2

get_pip_reqs() {
local project_path=$1
cat $project_path/*requirements.txt | grep -v requirements
}

docker_build() {
local pip_reqs="$1"
local py_version=$2
local image=$ECR_IMAGE_URL:$py_version
echo "Building docker image: $image"
docker build -t $image \
--build-arg PIP_REQS="$pip_reqs" \
--build-arg PY_VERSION=$py_version \
.
}

docker_push() {
local py_version=$1
local image=$ECR_IMAGE_URL:$py_version
echo "Pushing docker image: $image"
docker push $image
}

main() {
local pip_reqs="$(get_pip_reqs $KEDRO_REPO)"

# Image for python 3.6
docker_build "$pip_reqs" 3.6
docker_push 3.6

# Image for python 3.7
docker_build "$pip_reqs" 3.7
docker_push 3.7

# Image for python 3.8
docker_build "$pip_reqs" 3.8
docker_push 3.8

}

main
Loading

0 comments on commit 55b38b3

Please sign in to comment.