Skip to content

Exam #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open

Exam #100

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .github/workflows/ci-cd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Build and Deploy Java App
#trigger
on:
push:
branches: [ master ]

jobs:
build-and-deploy:
runs-on: self-hosted

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Build with Maven
run: mvn clean package

- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Set image tag
run: echo "IMAGE_TAG=${GITHUB_SHA::7}" >> $GITHUB_ENV

- name: Build Docker image
run: |
docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/java-hello-world:${{ env.IMAGE_TAG }} .
docker tag ${{ secrets.DOCKERHUB_USERNAME }}/java-hello-world:${{ env.IMAGE_TAG }} ${{ secrets.DOCKERHUB_USERNAME }}/java-hello-world:latest
# run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/java-hello-world:latest .
# - name: Set image tag
# run: echo "IMAGE_TAG=${GITHUB_SHA::7}" >> $GITHUB_ENV

- name: Push Docker image
run: |
docker push ${{ secrets.DOCKERHUB_USERNAME }}/java-hello-world:${{ env.IMAGE_TAG }}
docker push ${{ secrets.DOCKERHUB_USERNAME }}/java-hello-world:latest
# run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/java-hello-world:latest

- name: Deploy to server via SSH
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
set -e
IMAGE=${{ secrets.DOCKERHUB_USERNAME }}/java-hello-world:${{ env.IMAGE_TAG }}

echo "Pulling latest image..."
docker pull $IMAGE

echo "Stopping existing container if running..."
if docker ps -a -q --filter "name=app" | grep -q .; then
docker rm -f app
fi

echo "Running container on port 8080..."
docker run -d --name app -p 8080:8080 $IMAGE
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ dependency-reduced-pom.xml
.settings/
.vscode
.DS_Store
id_github_ci
id_github_ci.pub
14 changes: 0 additions & 14 deletions .whitesource

This file was deleted.

10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM maven:3.8.5-openjdk-17 AS build
WORKDIR /app
COPY . .
RUN mvn package -DskipTests

FROM openjdk:17
WORKDIR /app
COPY --from=build /app/target/java-hello-web-0.1.0.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
21 changes: 0 additions & 21 deletions Jenkinsfile

This file was deleted.

Loading