diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..5f6a0ec --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,76 @@ +name: 'Continuous Delivery' + +on: + push: + branches: + - "main" + +jobs: + build: + runs-on: ubuntu-latest + steps: + # Checkout code + - name: Checkout code + uses: actions/checkout@v3 + + # Authenticate to Google Cloud + - name: Authenticate to Google Cloud + env: + GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }} + run: | + echo "$GOOGLE_CREDENTIALS" > /tmp/google-credentials.json + gcloud auth activate-service-account --key-file=/tmp/google-credentials.json + + # Build and push Docker image + - name: Build and push Docker image + env: + CONTAINER_IMAGE_URL: ${{ secrets.CONTAINER_IMAGE_URL }} + GCLOUD_REGION: ${{ secrets.GCLOUD_REGION }} + run: | + docker build -t $CONTAINER_IMAGE_URL:latest . + gcloud auth configure-docker $GCLOUD_REGION + docker push $CONTAINER_IMAGE_URL:latest + + deploy: + needs: build + runs-on: ubuntu-latest + steps: + # Checkout code + - name: Checkout code + uses: actions/checkout@v3 + + # Authenticate to Google Cloud + - id: 'auth' + name: 'Authenticate to Google Cloud' + uses: 'google-github-actions/auth@v1' + with: + credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}' + + # Deploy container to Cloud Run and capture URL + - name: Deploy container to Cloud Run + env: + CONTAINER_IMAGE_URL: ${{ secrets.CONTAINER_IMAGE_URL }} + GCLOUD_REGION: ${{ secrets.GCLOUD_REGION }} + GCLOUD_PROJECT_ID: ${{ secrets.GCLOUD_PROJECT_ID }} + id: deploy + run: | + echo "Deployment running." + URL=$(gcloud run deploy latam-challenge \ + --image=$CONTAINER_IMAGE_URL:latest \ + --platform=managed \ + --allow-unauthenticated \ + --region=$GCLOUD_REGION \ + --port=8000 \ + --project=$GCLOUD_PROJECT_ID \ + --format="value(status.url)") + echo "::set-output name=url::$URL" + echo "Image URL: $URL" + + # Set STRESS_URL environment variable for later use + - name: Set STRESS_URL as env variable to use later the Makefile + run: echo "STRESS_URL=${{ steps.deploy.outputs.url }}" >> $GITHUB_ENV + + # Run stress tests + - name: Run stress tests + run: | + make stress-test STRESS_URL=${{ env.STRESS_URL }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..20df210 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,41 @@ +name: 'Continuous Integration' + +on: + pull_request: + branches: + - "develop" + +jobs: + test: + runs-on: ubuntu-latest + steps: + # Checkout code + - name: Checkout code + uses: actions/checkout@v3 + + # Setup Python + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: "3.9" + + # Cache dependencies to speed up workflow + - name: Cache dependencies + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements*.txt') }} + restore-keys: | + ${{ runner.os }}-pip- + + # Install dependencies + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt -r requirements-dev.txt -r requirements-test.txt + + # Run tests + - name: Run tests + run: | + make model-test + make api-test diff --git a/challenge/training.py b/challenge/training.py new file mode 100644 index 0000000..9819400 --- /dev/null +++ b/challenge/training.py @@ -0,0 +1,19 @@ +import pandas as pd + +from sklearn.model_selection import train_test_split +from challenge.model import DelayModel + +model = DelayModel() +data = pd.read_csv(filepath_or_buffer="data/data.csv") + +features, target = model.preprocess( + data=data, + target_column="delay" +) + +_, features_validation, _, target_validation = train_test_split(features, target, test_size = 0.33, random_state = 42) + +model.fit( + features=features, + target=target +) diff --git a/requirements-test.txt b/requirements-test.txt index 5819375..7b334d4 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -4,7 +4,6 @@ pytest~=6.2.5 pytest-cov~=2.12.1 mockito~=1.2.2 Jinja2~=3.0.3 -Flask~=2.0.3 itsdangerous==2.0.1 Werkzeug==2.0.3 diff --git a/tests/model/test_model.py b/tests/model/test_model.py index e4afabb..3b00b00 100644 --- a/tests/model/test_model.py +++ b/tests/model/test_model.py @@ -28,7 +28,7 @@ class TestModel(unittest.TestCase): def setUp(self) -> None: super().setUp() self.model = DelayModel() - self.data = pd.read_csv(filepath_or_buffer="../data/data.csv") + self.data = pd.read_csv(filepath_or_buffer="data/data.csv") def test_model_preprocess_for_training( @@ -100,4 +100,4 @@ def test_model_predict( assert isinstance(predicted_targets, list) assert len(predicted_targets) == features.shape[0] - assert all(isinstance(predicted_target, int) for predicted_target in predicted_targets) \ No newline at end of file + assert all(isinstance(predicted_target, int) for predicted_target in predicted_targets) diff --git a/workflows/cd.yml b/workflows/cd.yml deleted file mode 100644 index 3029e4f..0000000 --- a/workflows/cd.yml +++ /dev/null @@ -1,7 +0,0 @@ -name: 'Continuous Delivery' - -on: - ... - -jobs: - ... \ No newline at end of file diff --git a/workflows/ci.yml b/workflows/ci.yml deleted file mode 100644 index ec2cb7a..0000000 --- a/workflows/ci.yml +++ /dev/null @@ -1,7 +0,0 @@ -name: 'Continuous Integration' - -on: - ... - -jobs: - ... \ No newline at end of file