In today's rapidly evolving software development landscape, the successful execution of DevOps practices has become instrumental in delivering high-quality software at speed. DevOps, which is a fusion of development and operations, emphasises collaboration, automation, and continuous integration/continuous deployment (CI/CD) pipelines to streamline the software development lifecycle.
To achieve this seamless workflow, organisations are increasingly turning to robust tools like Atlassian BitBucket for version control, collaboration, and code deployment. Additionally, Slack has emerged as a leading communication and collaboration platform, providing teams with a central hub for real-time communication and integrations with various development tools.
In this comprehensive guide, we will explore how to harness the power of Atlassian BitBucket and Slack to create a harmonious environment for managing CI/CD pipelines and Docker containers. This integration will empower your development and operations teams to collaborate effectively, automate workflows, and deploy code with confidence.
Accessing Your BitBucket Dashboard
Our journey begins on the BitBucket dashboard, the central control panel for managing repositories, code, and collaborations. If you don't already have a BitBucket account, you can sign up for one on their website. Once you're logged in, follow these steps:
- Creating a New Repository:
- Click on Create a new repository
- On the Create a new repository do the following:
A. Project name: Give your project a meaningful name to categorize your work effectively.
B. Repository name: Provide a unique name for the repository, reflecting the project or application.
C. Click on the "Create Repository" button to finalize the setup.
D. Confirm that the repository was created successfully.
Setting Up Slack
Effective communication and collaboration are paramount in DevOps, and Slack provides a perfect platform for real-time interactions. Here's how to set up a Slack workspace:
- Go to the Slack website and click on the "Try For Free" button.
Provide your email address and click "Continue".
On the "Create a New Slack Workspace" page, click the "Create a Workspace" button.
Provide a name for your company or team.
- Next, add a workspace name and click "Next".
- While you can invite team members, for this article's sake, let's skip this step and click "Next".
- Provide information about what your team is currently working on.
- Confirm that your workspace is now active.
- Create a channel named "devops-engineering".
Connecting BitBucket to Slack
With your BitBucket repository and Slack workspace in place, it's time to connect the two for seamless collaboration and automated notifications:
- Return to your BitBucket repository page and click on "Repository settings."
- Under the "Slack" section, click on the "Connect" button.
- Allow BitBucket Cloud to access your Slack workspace by clicking "Allow".
- Add a chat subscription by selecting your workspace and channel. Click the "Add" button.
- Confirm the subscription.
Cloning Your BitBucket Repository
To start working with your code locally, you'll need to clone the BitBucket repository to your local machine:
- On the BitBucket repository page, click "Clone" and copy the repository URL link.
- Use the repository URL link to run git clone in your command line.
git clone repo_url
- Navigate into the cube360 directory.
cd cube360
- Create a new file called index.html and add
Hello World!!!
touch index.html
<p>Hello World!!!</p>
- Add index.html to the staging area with git add index.html.
git add index.html
- Run git status to check the status of your repository.
git status
- Commit the changes with git commit -m "Initial commit of project Hello World!!".
git commit -m "Initial commit of project Hello World!!"
- Push your code to BitBucket with git push.
git push
- Check the "devops-engineering" channel in Slack, and you should see an alert about the push action.
- To further test the integration, add
This is a great DevOps Collaboration feature
to index.html.
<p>This is a great DevOps Collaboration feature</p>
- Run git add * and git commit -m "Added more code".
git add * and git commit -m "Added more code"
- Push your changes to BitBucket with git push.
git push
BitBucket Pipelines for Building and Deployment
Now, let's delve into the heart of DevOps - automation. BitBucket Pipelines is a robust CI/CD solution that enables automated building, testing, and deployment of your code. We'll start by configuring the necessary files and directories:
- Return to your BitBucket repository dashboard and select the "Pipelines" option.
- On your local machine, create the following files and directories:
A. Create a file called "Dockerfile" with touch Dockerfile.
touch Dockerfile
B. Create another file named "bitbucket-pipelines.yml" with touch bitbucket-pipelines.yaml.
touch bitbucket-pipelines.yaml
C. Create a new file named "nginx.conf" with touch nginx.conf.
touch nginx.conf
D. Create a directory called "www-data" with mkdir www-data.
mkdir www-data
E. Move index.html into the "www-data/" directory with mv index.html www-data/.
mv index.html www-data/
- Add, commit, and push your changes to BitBucket with the following commands:
git add .
git status
git commit -m "Updated with Docker code"
git push
- Now confirm the recent changes on Bitbucket repository dashboard.
- Now, update the files on your local machine and commit them to your remote repository:
A. Update your Dockerfile.
# docker pull xxxxxxxx/yyyyyyy:zzzzzzzzz
# docker run --name nginx-webserver -d -p 8080:80 xxxxxxxx/yyyyyyy:zzzzzzzzz
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y nginx
ADD nginx.conf /etc/nginx/nginx.conf
ADD ./www-data /www-data
EXPOSE 80
CMD ["nginx"]
This is a Dockerfile, a text-based script used to define a Docker image. The Dockerfile describes how to build an image for running an NGINX web server using an Ubuntu 14.04 base image. Here's a breakdown of each part of the Dockerfile:
Base Image Selection:
FROM ubuntu:14.04
This line specifies the base image for the new image being built. In this case, it uses the "ubuntu:14.04" image as the starting point. The resulting image will have Ubuntu 14.04 as its base operating system.
Package Installation:
RUN apt-get update
RUN apt-get install -y nginx
These lines use the RUN
instruction to execute commands within the image during the build process.
The first command, apt-get update
, updates the package repositories to ensure the package manager (APT) has the latest package information.
The second command, apt-get install -y nginx
, installs the NGINX web server. The -y
flag automatically confirms any prompts during the installation.
Configuration File Addition:
ADD nginx.conf /etc/nginx/nginx.conf
This line copies a file named "nginx.conf" from the context (the directory where the Dockerfile is located) into the image.
Specifically, it adds the configuration file to the "/etc/nginx/nginx.conf" location within the image. This is where NGINX looks for its configuration by default.
Directory Addition:
ADD ./www-data /www-data
This line copies the contents of a directory named "www-data" from the context into the image.
It places the contents into the "/www-data" directory within the image.
Port Exposure:
EXPOSE 80
This line informs Docker that the container will listen on port 80 at runtime. It doesn't actually publish the port, but it documents that the container expects external traffic on port 80.
Command Execution:
CMD ["nginx"]
This line specifies the default command to run when a container based on this image is started.
It runs the "nginx" command, which starts the NGINX web server within the container.
B. Update your
bitbucket-pipelines.yml
file.
# This is a sample build configuration for Docker.
# Check our guides at https://confluence.atlassian.com/x/O1toN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: atlassian/default-image:2
pipelines:
default:
- step:
name: Build NGINX Docker Image
services:
- docker
script: # Modify the commands below to build your repository.
# Set $DOCKER_HUB_USERNAME and $DOCKER_HUB_PASSWORD as environment variables in repository settings
- export IMAGE_NAME=oloruntobi/my-nginx-image:$BITBUCKET_COMMIT
# build the Docker image (this will use the Dockerfile in the root of the repo)
- docker build -t $IMAGE_NAME .
# authenticate with the Docker Hub registry
- docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD
# push the new Docker image to the Docker registry
- docker push $IMAGE_NAME
- echo "finished building!"
This code is a configuration file written in YAML format for defining a build process using Docker within an Atlassian Bitbucket Pipelines. It describes how to build a Docker image for an NGINX web server and push it to a Docker registry, like Docker Hub. Here's a breakdown of the code:
Docker Image Configuration:
-
image: atlassian/default-image:2
: This specifies the Docker image to be used as the build environment. In this case, it's using the "atlassian/default-image" version 2 image as the base image for running the build steps.
Pipelines Configuration:
-
pipelines
: This section defines the pipelines for different stages of the build process. In this case, there's only one pipeline named "default."
Pipeline Step:
-
- step:
: This indicates the start of a build step within the "default" pipeline.
Step Name:
-
name: Build NGINX Docker Image
: This is the name of the build step and is used for identification and display purposes.
Service Configuration:
-
services:
: This section specifies the services that should be available during the build step. In this case, it includes the "docker" service, indicating that Docker should be available to execute Docker-related commands.
Build Script:
-
script:
: This section defines the sequence of commands to be executed during the build step. These commands will be run within the Docker image specified earlier.
Environment Variable Setup:
-
export IMAGE_NAME=oloruntobi/my-nginx-image:$BITBUCKET_COMMIT
: This command sets an environment variable calledIMAGE_NAME
. It uses the repository name "oloruntobi/my-nginx-image" and appends the Bitbucket commit hash to create a unique name for the Docker image.
Docker Build:
-
docker build -t $IMAGE_NAME .
: This command builds a Docker image using the Dockerfile located in the root of the repository. The-t
flag assigns a tag (in this case, the value ofIMAGE_NAME
) to the image.
Docker Login:
-
docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD
: This command authenticates with the Docker Hub registry using the provided Docker Hub username and password. This authentication is necessary to push the Docker image to Docker Hub.
Docker Push:
- docker push $IMAGE_NAME
: This command pushes the newly built Docker image to the Docker registry (in this case, Docker Hub). It uses the IMAGE_NAME
variable as the image name and tag.
Echo Finished:
- echo "finished building!"
: This is a simple informational message that will be printed to the build log, indicating that the build process has finished.
C. Update the nginx.conf file.
daemon off;
pid /var/lib/nginx/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
index index.html;
server {
listen *:80;
location / {
root /www-data;
}
}
}
Let's break down the different sections and directives in this configuration:
Global Directives:
daemon off;
: This directive controls whether NGINX runs in the background as a daemon. Setting it to "off" means NGINX will run in the foreground, which is useful for Docker containers or when running NGINX directly from the command line.
pid /var/lib/nginx/nginx.pid;
: This directive specifies the path where NGINX will write its process ID (PID) file. This file contains the process ID of the running NGINX instance.
events Block:
events { ... }
: This block contains event-related configuration directives.
worker_connections 1024;
: This directive sets the maximum number of connections that a worker process can handle simultaneously. In this case, it's set to 1024 connections, which is a reasonable default value.
http Block:
http { ... }
: This block defines HTTP-related configuration directives for NGINX.
include Directive:
include /etc/nginx/mime.types;
: This directive includes the specified file, "/etc/nginx/mime.types," which contains a mapping of file extensions to MIME types. It's used to determine the MIME type of files served by NGINX.
index Directive:
index index.html;
: This directive specifies the default file to serve when a directory is requested. In this case, if a directory is requested, NGINX will look for an "index.html" file and serve it if found.
server Block:
server { ... }
: This block defines a server block, which represents an individual HTTP server in NGINX.
listen :80;: This directive specifies that the server should listen on port 80 for incoming HTTP requests. The asterisk () means it will listen on all available network interfaces.
location Block:
location / { ... }
: This block defines a location block within the server block. It specifies how NGINX should handle requests that match the location pattern (in this case, the root path "/").
root /www-data;
: This directive sets the root directory for serving content for this location. Requests to the root path ("/") will be served from the "/www-data" directory. This is where the web content is expected to be located.
In summary, this NGINX configuration sets up a basic HTTP server listening on port 80, serving content from the "/www-data" directory, and using "index.html" as the default file.
D. Use the following commands to add, commit, and push these changes:
git add .
git status
git commit -m "Adding more code to our repo"
git push
E. Next, configure environment variables in the repository settings:
- Click on "Repository Settings.
- Under "Repository Variables," create two variables named DOCKER_HUB_USERNAME and DOCKER_HUB_PASSWORD.
- Update the bitbucket-pipelines.yml file by adding your Docker Hub username and image name.
- Push the changes to your BitBucket repository.
git add .
git status
git commit -m "Adding more code to our repo"
git push
- Go to the Pipelines page on your BitBucket repository and confirm that the pipeline passes successfully. Additionally, check Docker Hub to verify that your artifact is available.
F. Now, let's move forward:
- Start the Docker daemon via Docker Desktop.
- Run your container with the following steps: a. Check for containers on your local machine with docker ps -a
docker ps -a
b. Pull your image from Docker Hub with docker pull username/my-nginx-image:tag
docker pull username/my-nginx-image:tag
c. Start your container with docker run --name nginx-webserver -d -p 8080:80 username/my-nginx-image:tag
docker run --name nginx-webserver -d -p 8080:80 username/my-nginx-image:tag
d. Confirm the status of the container with docker ps
docker ps
e. Test your application by running curl localhost:8080
curl localhost:8080
Slack Notifications for BitBucket Pipelines
To receive notifications in Slack when your pipeline succeeds or fails, follow these steps:
- Return to the BitBucket repository page and click on "Repository Settings".
- Under the "SLACK" section, click on the "Settings" button.
- Add "pipeline failed" and "pipeline succeeded" options under the master section.
- Add a new line to your index.html page:
This DevOps is DevOpsing by automation!
.
<p>This DevOps is DevOpsing by automation!</p>
- Push the change to BitBucket.
git add .
git status
git commit -m "adding more code to index.html file"
git push
- Confirm by checking the Slack channel and BitBucket Pipelines page.
Conclusion
By integrating Atlassian BitBucket and Slack into your DevOps workflow, you've taken a significant step toward streamlining your development process. This comprehensive integration enables you to automate code deployments, manage Docker containers efficiently, and receive real-time notifications on pipeline statuses.
In the fast-paced world of software development, where collaboration and automation are key, this integration empowers your team to deliver code more swiftly and reliably. It fosters a culture of collaboration and transparency, allowing development and operations teams to work together seamlessly.
As you embark on your DevOps journey with BitBucket and Slack, remember that this is just the beginning. Continuous improvement and adaptation to your team's unique needs will further enhance your DevOps practices, helping you deliver exceptional software with speed and confidence. Happy DevOps-ing!
Top comments (7)
This was an enjoyable and insightful read that provided valuable information and captured my interest. I appreciate the quality of the content and the engaging writing style. If you have any more similar articles or recommendations, I'd love to explore them further. Thank you for sharing this fantastic piece of content.
Nice one!
Great stuff and very detailed.
Great read
It's a fantastic read; you should definitely give it a go.
Wonderful piece.
Nice read!!!