Skip to content

Test/docker cd #4

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

Merged
merged 2 commits into from
Nov 14, 2024
Merged
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
82 changes: 82 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: CD

on:
push:
branches: [ main ]

workflow_dispatch:

jobs:
continuous-delivery:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

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

- name: Set YML
env:
YAML_SECRET: ${{ secrets.APPLICATION_YAML }}
YAML_DIR: src/main/resources
YAML_FILE_NAME: application.yml
run: |
mkdir -p $YAML_DIR
echo $YAML_SECRET | base64 --decode > $YAML_DIR/$YAML_FILE_NAME
find src

- name: Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-

- name: Get Short SHA
id: short_sha
run: |
echo "::set-output name=sha_short::$(echo ${{ github.sha }} | head -c 7)"
echo "Short SHA: ${{ steps.short_sha.outputs.sha_short }}"

- name: Print branch & tag information
run: |
echo "Current branch: ${{ github.event.inputs.branch || github.ref }}"
echo "Current tag: ${{ steps.short_sha.outputs.sha_short }}"

- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
shell: bash

- name: Run build
run: ./gradlew build -x test
shell: bash

- name: Docker Login and Push
env:
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_HUB_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
DOCKER_IMAGE_TAG: test:${{ steps.short_sha.outputs.sha_short }}
run: |
echo $DOCKER_HUB_PASSWORD | docker login -u $DOCKER_HUB_USERNAME --password-stdin
docker build -t $DOCKER_IMAGE_TAG .
docker tag $DOCKER_IMAGE_TAG $DOCKER_HUB_USERNAME/$DOCKER_IMAGE_TAG
docker push $DOCKER_HUB_USERNAME/$DOCKER_IMAGE_TAG

- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PEM_KEY }}
script: |
docker pull ${{ secrets.DOCKER_USERNAME }}/test:${{ steps.short_sha.outputs.sha_short }}
docker tag ${{ secrets.DOCKER_USERNAME }}/test:${{ steps.short_sha.outputs.sha_short }} test
docker rm -f test || true
docker run -d --name test -e TZ=Asia/Seoul -p 8080:8080 ${{ secrets.DOCKER_USERNAME }}/test:${{ steps.short_sha.outputs.sha_short }}
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'zulu'
distribution: 'corretto'

- name: Set YML
env:
Expand Down
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM openjdk:17-jdk-alpine
EXPOSE 8080
VOLUME /tmp
COPY build/libs/*.jar test-server.jar
ENTRYPOINT ["java", "-jar", "/test-server.jar"]
Loading