Automating Tasks with GitLab Pipelines: Deploying a Flask App to Azure
Published · Updated
Introduction to GitLab Pipelines
This course provides an in-depth guide to creating and managing workflows using GitLab CI/CD pipelines. You will learn how to automate tasks such as testing, building, and deploying applications, with a practical example of deploying a Python REST API to Microsoft Azure.
What are GitLab Pipelines?
GitLab Pipelines are a series of tasks executed in stages to automate the process of software development, testing, and deployment. They enable developers to:
- Automate repetitive tasks.
- Ensure consistency in workflows.
- Detect and fix issues early through continuous integration (CI).
- Streamline the delivery of software through continuous deployment (CD).
Setting Up GitLab CI
Key Concepts of GitLab Pipelines
- Pipeline: The top-level entity that defines the workflow.
- Stages: Logical groupings of jobs (e.g., test, build, deploy).
- Jobs: Individual tasks in a stage (e.g., run unit tests).
- Runners: Agents that execute the jobs.
- .gitlab-ci.yml: The configuration file that defines pipelines.
To start using Gitlab CI you must create a .gitlab-ci.yml file in your repository. Paste this yaml configuration in your file. Specify the stages, jobs, and execution details in your .gitlab-ci.yml.
12build-job:3 stage: build4 script:5 - echo "Hello, $GITLAB_USER_LOGIN!"67test-job1:8 stage: test9 script:10 - echo "This job tests something"1112test-job2:13 stage: test14 script:15 - echo "This job tests something, but takes more time than test-job1."16 - echo "After the echo commands complete, it runs the sleep command for 20 seconds"17 - echo "which simulates a test that runs 20 seconds longer than test-job1"18 - sleep 201920deploy-prod:21 stage: deploy22 script:23 - echo "This job deploys something from the $CI_COMMIT_BRANCH branch."24 environment: production2526
If you're using GitLab.com, this is all you need to start using your pipeline, because GitLab.com provides instance runners for you. otherwise you should setup a gitlab runner. Follow this guide if you would like to configure you own runner https://docs.gitlab.com/runner/register/
Deploying a Python REST API to Azure
After introducing Gitlab CI let's make our hand dirty witha real word example. In this section, we'll build a GitLab pipeline that build and test a python application and deploys it to Azure Web App Service.
Create a simple Python REST API using Flask with this directory structure
12my-api/3│4├── app.py5├── requirements.txt6├── tests/7│ └── test_app.py8└── Dockerfile9
The main app.py contains a simple endpoint with Flask. and run a web server that will reply to HTTP GET requests.
12from flask import Flask, jsonify34app = Flask(__name__)56@app.route('/api/health', methods=['GET'])7def health_check():8 return jsonify({"status": "healthy"}), 200910if __name__ == '__main__':11 app.run(host='0.0.0.0', port=5000)12
You can run this basic app in your local environment; for this, you will need to install Flask in your local env. Alternatively, you can containerize the app by creating a lightweight Dockerfile based on the python:3.9-slim image. This will install and run our basic API server. Simply copy and paste the following into your Dockerfile.
12# Use an official Python runtime as the base image3FROM python:3.9-slim45# Set the working directory6WORKDIR /app78# Copy files9COPY . .1011# Install dependencies12RUN pip install -r requirements.txt1314# Expose the port15EXPOSE 50001617# Start the application18CMD ["python", "app.py"]1920
We have our Flask application and our Dockerfile. Let's create a pipeline to automate the deployment of our application. The pipeline will contain three stages: test, build, and deploy.
- Test Stage: Runs pytest to ensure the API works as expected.
- Build Stage: Creates a Docker image and pushes it to GitLab's container registry.
- Deploy Stage: Deploys the Docker image to Azure. We want to deploy our app only if we push our code to the main branch.
We need to configure the following variables in GitLab:
- AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID: Credentials for Azure login.
- AZURE_WEBAPP_NAME, AZURE_RG_NAME, AZURE_PLAN_NAME: Azure deployment parameters.
- CI_REGISTRY_USER, CI_REGISTRY_PASSWORD: GitLab registry credentials.
Create a .gitlab-ci.yml file and paste the following content.
12stages:3 - test4 - build5 - deploy67variables:8 DOCKER_IMAGE: registry.gitlab.com/<your-username>/<your-repo>:latest9 AZURE_WEBAPP_NAME: your-azure-webapp10 AZURE_RG_NAME: your-resource-group11 AZURE_PLAN_NAME: your-app-service-plan1213test:14 stage: test15 image: python:3.916 script:17 - pip install -r requirements.txt18 - pytest tests/1920build:21 stage: build22 image: docker:24.023 services:24 - docker:dind25 script:26 - docker build -t $DOCKER_IMAGE .27 - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY28 - docker push $DOCKER_IMAGE2930deploy:31 stage: deploy32 image: azure/cli:2.45.033 script:34 - az login --service-principal --username $AZURE_CLIENT_ID --password $AZURE_CLIENT_SECRET --tenant $AZURE_TENANT_ID35 - az webapp create --name $AZURE_WEBAPP_NAME --resource-group $AZURE_RG_NAME --plan $AZURE_PLAN_NAME --deployment-container-image-name $DOCKER_IMAGE36 only:37 - main3839
Commit and Push your code and .gitlab-ci.yml file to the GitLab repository. Go to your repository's CI/CD Pipelines section to monitor pipeline execution. After successful testing and building, your app will be deployed to Azure Web App Service.
Conclusion
GitLab CI/CD pipelines enable efficient automation of development workflows. By following this course, you have learned how to:
- Configure GitLab pipelines using .gitlab-ci.yml.
- Test and deploy a Python REST API to Azure using containers.