dev server CI/CD Test #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Java CI with Gradle Deploy | |
on: | |
push: | |
branches: [ "staging" ] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout the code | |
uses: actions/checkout@v4 | |
- name: Get Public IP | |
id: ip | |
uses: haythem/public-ip@v1.3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Gradle Caching | |
uses: actions/cache@v4 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Run chmod to make gradlew executable | |
run: chmod +x ./gradlew | |
- name: Build with Gradle | |
run: | | |
./gradlew clean build -x test | |
- name: Docker build and push | |
run: | | |
# log in to docker hub | |
echo ${{ secrets.DOCKERHUB_PASSWORD }} | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin | |
# build docker image | |
docker build -t ${{ secrets.DOCKERHUB_NAME }}/${{ secrets.DEV_DOCKERHUB_REPOSITORY }} . | |
# push docker image to docker hub | |
docker buildx build --push --platform linux/amd64 -t ${{ secrets.DOCKERHUB_NAME }}/${{ secrets.DEV_DOCKERHUB_REPOSITORY }} . | |
- name: Clean up dangling Docker images locally | |
run: | | |
sudo docker image prune -f | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: 'ap-northeast-2' | |
- name: Add GitHub Actions IP | |
run: | | |
aws ec2 authorize-security-group-ingress \ | |
--group-id ${{ secrets.SECURITY_GROUP_ID }} \ | |
--protocol tcp \ | |
--port ${{ secrets.EC2_SSH_PORT }} \ | |
--cidr ${{ steps.ip.outputs.ipv4 }}/32 | |
- name: Set environment values | |
run: | | |
cd ./ | |
touch ./.env | |
echo "${{ secrets.ENV }}" > .env | |
shell: bash | |
- name: copy file via ssh key | |
uses: appleboy/scp-action@v0.1.7 | |
with: | |
host: ${{ secrets.DEV_PUBLIC_DNS }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
port: ${{ secrets.EC2_SSH_PORT }} | |
source: "./docker-compose.yaml, ./.env" | |
target: ${{ secrets.SCP_TARGET_PATH }} | |
- name: Test SSH connection | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.DEV_PUBLIC_DNS }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
port: ${{ secrets.EC2_SSH_PORT }} | |
script: | | |
echo "Testing SSH connection" | |
uptime | |
- name: Deploy to EC2 Server | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.DEV_PUBLIC_DNS }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
port: ${{ secrets.EC2_SSH_PORT }} | |
debug: true | |
script: | | |
echo ${{ secrets.DOCKERHUB_PASSWORD }} | sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin | |
sudo docker kill ${{ secrets.DEV_SERVER_PROJECT_NAME }} | |
sudo docker rm -f ${{ secrets.DEV_SERVER_PROJECT_NAME }} | |
sudo docker images -q ${{ secrets.DOCKERHUB_NAME }}/${{ secrets.DEV_DOCKERHUB_REPOSITORY }} | xargs -r docker rmi | |
sudo docker pull ${{ secrets.DOCKERHUB_NAME }}/${{ secrets.DEV_DOCKERHUB_REPOSITORY }} | |
cd ${{ secrets.SCP_TARGET_PATH }} | |
sudo docker compose up -d --no-deps ${{ secrets.DEV_SERVER_PROJECT_NAME }} | |
sudo docker image prune -af --filter "until=24h" | |
- name: Remove GitHub Actions IP | |
run: | | |
aws ec2 revoke-security-group-ingress \ | |
--group-id ${{ secrets.SECURITY_GROUP_ID }} \ | |
--protocol tcp \ | |
--port ${{ secrets.EC2_SSH_PORT }} \ | |
--cidr ${{ steps.ip.outputs.ipv4 }}/32 |