Code Monkey home page Code Monkey logo

cicd-pipeline-with-githubactions-docker's Introduction

CI/CD Pipeline with Github Actions and Docker to deploy a Spring Boot REST API

This is a sample project for creating a CI/CD pipeline with Github Actions and Docker to deploy a Spring Boot REST API.

Prerequisites

Before you begin, make sure you have the following:

  • A Github account
  • A Docker Hub account
  • A Spring Boot REST API project
  • Docker installed on your local machine

Getting Started

Step 1: Set up Github Repository

Create a new Github repository for your Spring Boot REST API project.

Step 2: Create a Dockerfile

Create a Dockerfile in the root directory of your project.

Before that you have to build the project it generates .jar file in the target folder

FROM openjdk:17-jdk-slim

WORKDIR /app

COPY target/my-api-0.0.1-SNAPSHOT.jar /app/my-api.jar

ENTRYPOINT ["java", "-jar", "my-api.jar"]

This Dockerfile uses the openjdk:17-jdk-slim image as the base image, sets the working directory to /app, copies the Spring Boot JAR file into the container, and runs the JAR file when the container starts.

Step 3: Create Github Actions Workflow

Create a new file called .github/workflows/main.yml in the root directory of your project. This file will contain the instructions for building, testing, and deploying your application.

  name: build and deploy spring-api
  on:
    push:
      branches:
        - main

  jobs:
    build-deploy:
      name: build and deploy spring-api
      runs-on: ubuntu-20.04
      steps:
        - name: checkout code
          uses: actions/checkout@v3

        - name: setup jdk 17
          uses: actions/setup-java@v3
          with:
            distribution: 'corretto'
            java-version: 17

        - name: unit tests
          run: mvn -B test --file pom.xml

        - name: build the app
          run: |
            mvn clean
            mvn -B package --file pom.xml
        - name: build the docker image
          uses: docker/build-push-action@v4
          with:
            context: .
            dockerfile: Dockerfile
            push: false
            tags: ${{ secrets.DOCKER_HUB_USERNAME }}/rest-api:latest

        - name: login to docker hub
          uses: docker/login-action@v1
          with:
            username: ${{ secrets.DOCKER_HUB_USERNAME }}
            password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

        - name: push the docker image to docker hub
          uses: docker/build-push-action@v4
          with:
            context: .
            dockerfile: Dockerfile
            push: true
            tags: ${{ secrets.DOCKER_HUB_USERNAME }}/rest-api:latest

This main.yml file sets up a workflow that runs on every push to the main branch. It has one job:

  • build-deploy: Builds the Docker image, tags it with the latest, and pushes it to Docker Hub.

Step 4: Push to Github Repository

Commit and push your changes to your Github repository. The Github Actions workflow should automatically trigger and build your Docker image.

Step 5: Configure Github Secrets

To securely store your Docker Hub credentials, you'll need to create Github secrets. Go to your Github repository's settings and click on the "Secrets" tab. Create two secrets:

DOCKER_HUB_USERNAME: Your Docker Hub username DOCKER_HUB_ACCESS_TOKEN: Your Docker Hub access token

cicd-pipeline-with-githubactions-docker's People

Contributors

oswaldaks avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.