Skip to content

lucasmargui/gitlab-deploy-workflow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧩 GitLab Deploy Workflow

This repository provides a step-by-step guide to creating and managing a GitLab CI/CD pipeline for deploying applications. It covers everything from setting up the GitLab project, configuring runners, building Docker images, and deploying to production environments. Ideal for developers and DevOps engineers looking to automate their deployment workflow efficiently.

📘 Table of Contents

🔐 Configuring AWS CLI Access Using IAM

This section explains how to set up AWS CLI authentication using IAM (Identity and Access Management), allowing secure command-line access to your AWS resources.

Click to show details

Step 1 – Install AWS CLI

Install and configure the AWS Command Line Interface (AWS CLI) — a unified tool that enables you to manage AWS services directly from your terminal.

Click to show details AWS CLI Installation

Step 2 – Create a User Group (IAM Group

  1. In the AWS Management Console, navigate to IAM (Identity and Access Management).
  2. On the sidebar, select Groups → Create Group.
  3. Name the group cli-users.
  4. Attach the AdministratorAccess policy (recommended only for testing environments).
  5. Click Create Group to finalize.
Click to show details IAM Groups Navigation
IAM Group Creation

Step 3 – Create an IAM User

  1. Still inside IAM, go to Users → Add User.
  2. Set the username to lucasmarguaws.
  3. Select the Programmatic access option (for CLI access).
  4. Add the user to the previously created cli-users group.
  5. Click Create User to complete the process.
Click to show details IAM User Creation Step 1
IAM User Group Association

Step 4 – Generate Access Keys

  1. After the user is created, click the username (lucasmarguaws) to view its details.
  2. Go to the Security credentials tab.
  3. Click Create access key and choose the Command Line Interface (CLI) usage type.
  4. Copy and securely store the Access Key ID and Secret Access Key — they will be required for AWS CLI authentication.
Click to show details Access Key Creation
Access Key Output

Step 5 – Configure the AWS CLI

Open your terminal or Command Prompt and run:

aws configure

Provide your credentials when prompted:

AWS Access Key ID [None]: <your-access-key-id>
AWS Secret Access Key [None]: <your-secret-access-key>
Default region name [None]: us-east-1
Default output format [None]: json
Click to show details AWS Configure Example

✅ Once completed, your AWS CLI is fully authenticated and ready to execute AWS commands or apply Terraform scripts securely and automatically.


🔑 Creating SSH Key Pair

This section describes how to create a Key Pair in AWS to enable secure SSH access to your EC2 instance.

Click to show details

Step 1 – Access the EC2 Service

  1. Open the AWS Management Console.
  2. Navigate to the EC2 service.

Step 2 – Create a New Key Pair

  1. In the left navigation pane, go to Key Pairs under Network & Security.
  2. Click Create key pair.
  3. In the Name field, enter: Terraform.
  4. Under File format, select .ppk (for PuTTY).
  5. Click Create key pair.
  6. Your browser will automatically download the file Terraform.ppk.

💾 Save it in a secure location, for example:

C:\Users<YourUser>.ssh\Terraform.ppk

✅ This key pair will later be referenced in Terraform to allow SSH access to the EC2 instance.


☁️ Create GitLab directory/repository

GitLab is an all-in-one DevOps platform offering:

  • Version Control: Git-based versioning system.
  • Continuous Integration (CI): Automates build, test, and deployment of code on every change.
  • Continuous Delivery (CD): Automates deployment to testing, staging, and production environments reliably and consistently.
  • Issue Tracking: Manage issues and tasks for progress monitoring.
  • Team Collaboration: Unified platform for development, testing, and delivery, improving efficiency and reducing errors.

Our goal is to publish code, build containers, and deploy them to a Kubernetes cluster automatically.

Click to show details

Step 1 – Create GitLab Account

  1. Visit GitLab
  2. Sign up for a free account or log in if you already have one.

Step 2 – Create a New Project

  1. Click New Project
  2. Select Blank Project
  3. Set the project name and click Create Project
  4. After creation, GitLab will display instructions to initialize a local repository.
Click to show details image

Step 3 – Initialize Local Repository

  1. Create a local directory for your project:
mkdir bootcampt-devops-project-1
cd bootcampt-devops-project-1
Click to show details image
  1. Initialize Git:
git init --initial-branch=main --object-format=sha1
  1. Configure local user information:
git config --local user.name "Your Name"
git config --local user.email "your.email@example.com"

Step 4 – Link Repository and Push Code

  1. Add the GitLab remote repository:
git remote add origin git@gitlab.com:lucasmargui/bootcampt-devops-project-1.git
  1. Stage files for commit:
git add .
  1. Make the initial commit:
git commit -m "Initial commit"
  1. Push the code to GitLab:
git push --set-upstream origin main


Step 5 – Provision EC2 Instance with Terraform

  1. Reuse the existing bootcampt-devops-project-1 to configure a terraform and create a new EC2 instance for your GitLab Runner.
Click to show details image
  1. Initialize Terraform:
terraform init
  1. Apply the Terraform plan to create the EC2 instance:
terraform apply
  1. After the instance is created, connect via SSH using PuTTY:
  • Username: ubuntu
  • Private Key: The one created previously (Terraform.ppk)
Click to show details image

Step 6 – Install GitLab Runner

  1. SSH into your EC2 instance.
  2. Install GitLab Runner
Click to show details image
Click to show details image
Click to show details image
sudo apt-get update
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
sudo apt-get install gitlab-runner
  1. Confirm installation by pressing Enter when prompted. Note: You can also pre-install GitLab Runner in the Terraform configuration.

Step 7 – Register GitLab Runner

  1. In GitLab, navigate to your project: Settings > CI/CD > Runners > New Project Runner
Click to show details image
  1. Copy the registration token from the three dots menu.
Click to show details image
  1. On your EC2 instance, register the runner:
sudo gitlab-runner register
  • GitLab instance URL: https://gitlab.com/
  • Registration token: Copy from GitLab
  • Runner tags: shell (or any tags you prefer)
Click to show details image
  1. Your runner is now registered and ready to receive jobs
Click to show details image

Step 8 – Configure GitLab Runner User

  1. GitLab Runner creates a user called gitlab-runner. Verify in:
cat /etc/passwd | grep gitlab-runner
  1. Add the runner to the Docker group to allow building and running containers:
sudo usermod -aG docker gitlab-runner

Step 9 – Docker Hub Authentication

  1. Switch to root user (if needed, set a root password first):
sudo passwd root
su
  1. Set a password for gitlab-runner:
passwd gitlab-runner
  1. Switch to the gitlab-runner user:
su gitlab-runner
  1. Log in to Docker Hub (or company Docker registry):
docker login -u "your-username"
Click to show details image

🐳 CI/CD Pipeline – Containerized Application

Click to show details

Step 1 – Configure .gitignore

  • Create a .gitignore file at the root of the repository.
  • Add files and directories that should not be versioned, such as Terraform state files or logs.
.terraform*
terraform*

Step 2 – Create Dockerfile

Inside your application directory, create a Dockerfile.

This file will be used to build the container image.

Minimal example:

FROM httpd:latest

WORKDIR /usr/local/apache2/htdocs/

COPY * /usr/local/apache2/htdocs/

EXPOSE 80
Click to show details image

Step 3 – Configure GitLab Pipeline

Create a .gitlab-ci.yml file at the root of the repository.

Divide the pipeline into stages (main sections) and jobs (tasks inside stages).

stages:
  - build
  - deploy
variables:
  GLOBAL_VAR: "2.0"
Click to show details image

Step 4 – Build Docker Image

  • Job to build and push the Docker image:
criar_imagens:
  stage: build
  tags:
    - aws
  script:
  - docker build -t lucasmargui/app-bootcamp-devops:$GLOBAL_VAR app/.
  - docker push lucasmargui/app-bootcamp-devops:$GLOBAL_VAR
  1. Stage: build
  • This job is part of the build stage in the CI/CD pipeline.
  1. Tags: aws
  • Specifies that this job should run on runners tagged with aws (created in e2c).
  1. Script:
  • docker build -t lucasmargui/app-bootcamp-devops:$GLOBAL_VAR app/ Builds a Docker image using the Dockerfile located in the app/ directory. The image name will be lucasmargui/app-bootcamp-devops and the tag will be set using the environment variable $GLOBAL_VAR.

  • docker push lucasmargui/app-bootcamp-devops:$GLOBAL_VAR Pushes the built Docker image to the remote Docker repository.

Click to show details image
  • Add and commit the new files:
git add .
git commit -m "Create CI/CD Pipeline"
git push --set-upstream origin main

Step 5 – Deploy Container

  • Job to run the container on the production server:
executar_imagens:
  stage: deploy
  needs:
    - criar_imagens
  tags:
    - aws
  before_script:
  - docker rm $(docker ps -a -q) --force
  script:
  - docker run -dti --name web-server -p 80:80 lucasmargui/app-bootcamp-devops:$GLOBAL_VAR
  after_script:
  - docker system prune --force
  1. Stage: deploy
  • This job runs in the deploy stage of the CI/CD pipeline.
  1. Dependencies: needs: criar_imagens
  • Ensures this job only runs after the criar_imagens job has successfully completed.
  1. Tags: aws
  • Specifies that this job should run on runners tagged with aws.
  1. Before Script:
docker rm $(docker ps -a -q) --force
  • Removes all existing Docker containers to ensure a clean environment before starting the new container.
  1. Script:
docker run -dti --name web-server -p 80:80 lucasmargui/app-bootcamp-devops:$GLOBAL_VAR
  • Runs the Docker image in a detached and interactive terminal mode.
  • Names the container web-server
  • Maps port 80 on the host to port 80 in the container
  • Uses the image lucasmargui/app-bootcamp-devops with the tag $GLOBAL_VAR.
  1. After Script:
docker system prune --force
  • Cleans up unused Docker objects to free disk space and keep the environment tidy.
Click to show details image
  • Add, commit, and push:
git add .
git commit -m "Deploy container"
git push
  • Verify in GitLab that the container is running successfully.

Step 6 – Update Application Version

To update the application (e.g., version 2.0 → 3.0):

variables:
  GLOBAL_VAR: "3.0"
Click to show details image

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published