How I am using Bitbucket, Github, and Heroku for my CI/CD pipeline.

How I am using Bitbucket, Github, and Heroku for my CI/CD pipeline.

The adoption of CI/CD has changed how we developers ship our products. This is a first in a series of blog posts about this transition. And in these series of blog post, I will be describing the tools and technology which I am using and how I am using them.

But before starting let's have a quick overview of what CI/CD means.

A CI/CD typical pipeline helps us automate steps in our software delivery process, such as starting code builds, running automated tests, and deploying to a staging or production environment. Automated pipelines remove manual errors, provide standardized development feedback loops and enable fast product iterations.

The setup I will be explaining is for my development environment.

The project which we are gonna deploy is a Maven Java Web Application.

So what's the role of each tool?

Bitbucket - will be our Code Repository. And will run our Pipeline (Bitbucket Pipeline)

Github - will be our Artifact Repository.

Heroku - will be hosting our application.

Create a repo on bitbucket, I personally like the concept of Git Flow. I always use Git Flow in my projects ( to learn more about git-flow check out this post by Atlassian https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow).

Now we will be creating our pipeline using the Bitbucket pipeline. To create A bitbucket pipeline create a new file and Name it "bitbucket-pipeline.yml". Bitbucket pipeline uses YAML as there scripting language.

This will be our development pipeline and it will only get triggered when we commit/push to our Development branch.

Our pipeline will be divided into 2 steps:-

Step 1) Compile and Test 

Step 2) Build and Add to our Artifact Repository ( in this case Github)

Compile and Test [ Step 1 ] -

In this step, we will compile our JAVA code first using maven command (mvn compile) and then we will test our code running unit test cases (mvn test).

You might be thinking why I am using mvn compile and mvn test, the benefits here is that, if by chance we have some error in our code, our pipeline will fail and will not run the next steps. And by doing so we save our pipeline time ( Bitbucket provides Free 50 mins of pipeline runtime and by catching error early we can save our pipeline time.)

Build and Add to our Artifact Repository [ Step 2] -

Before running step 2 make sure you have to create a GitHub repository. This repository will act as our Artifact Repository.

After creating a repo, create a new File Procfile which will be used by Heroku.

Here is a sample Procfile for Java:-

"web java -Dserver.port=$PORT -jar <myArtificatName>.war"

Also, create a new Heroku project and make sure to connect it to your Github repository and also make sure that Automatic deploys are enabled for your Github repo.

Now back to writing the Pipeline Script.

As we have already Run test cases in step 1, we do not need to run them again.

We will simply package our application. Using the following maven command

"mvn clean package -DskipTests -Pdevelopment -Ddev.version=$BITBUCKET_BUILD_NUMBER"

*The variable $BITBUCKET_BUILD_NUMBER is an inbuild Variable which bitbucket provides us, this variable indicates the Build number of our pipeline.

Now we will Clone our Github repo, and then we will remove all our old war/jar file if we have any.

We will then copy our newly created artifact, to our GitHub repo which we just cloned and then we will commit and push the changes.

We are done, now whenever we will commit anything to bitbucket our pipeline will run and if it Passes Step 1, it will step 2 which will be responsible for creating our JAVA package and updating our artifact repo. which contains our war file to be deployed and Procfile. And after the commit has been made to our artifact repo, Heroku will automatically detect the change and deploy our new JAVA Package.

Now to test our pipeline try and commit something to your bitbucket repo, and then wait and watch the magic of your pipeline.

Here is my bitbucket-pipeline.yml

--- 
image: "maven:3.6.1"
pipelines: 
  branches: 
    develop: 
      - step: 
          name: Compile and Test 
          caches:
            - maven
          script: 
          - mvn compile
          - mvn test


      - step:
          name: Build and Add to our Artifact Repository
          caches:
            - maven
          script:
          - mvn clean package -DskipTests -Pdevelopment -Ddev.version=$BITBUCKET_BUILD_NUMBER
          - cd target 
          - ls
          - git clone https://$GITHUB_USERNAME:[email protected]/$GITHUB_USERNAME/<githubrepoName>.git -b
          - cd githubrepoName && git rm -r  '*.war' -f && cd ..
          - mv <warFileName>$BITBUCKET_BUILD_NUMBER.war artifacts
          - cd githubrepoName && rm -r Procfile
          - echo "web java -Dserver.port=$PORT -jar d<warFileName>$BITBUCKET_BUILD_NUMBER.war" > Procfile
          - git config --global user.name "$GIT_USERNAME"  &&  git config --global user.email "$GIT_EMAIL"
          - git add * && git commit -m "new artifact  added , build number is  $BITBUCKET_BUILD_NUMBER"
          - git push


Added this profile to your POM

	<profiles>
		<profile>
			<id>development</id>

			<properties>
				<dev.version>0</dev.version>
			</properties>
			<build>
				<finalName><warFileName>${dev.version}</finalName>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-war-plugin</artifactId>
					</plugin>
				</plugins>
			</build>

		</profile>


	</profiles>

Happy Building!

要查看或添加评论,请登录

Vasu Bhardwaj的更多文章

社区洞察

其他会员也浏览了