chore: modify application.yml to Github Actions trigger #14
Workflow file for this run
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: Dev Deployment | |
on: | |
push: | |
branches: [ feature/deploy ] | |
paths: | |
- "src/**" | |
permissions: | |
contents: read | |
jobs: | |
build-and-docker-operations: | |
runs-on: ubuntu-latest | |
outputs: | |
image_tag: ${{ steps.generate_tag.outputs.tag }} | |
steps: | |
- name: checkout | |
uses: actions/checkout@v4 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '21' | |
distribution: 'temurin' | |
- name: Set up Docker | |
uses: docker/setup-buildx-action@v2 | |
- 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: Grant execute permission for gradlew | |
run: chmod +x ./gradlew | |
shell: bash | |
- name: Build with Gradle and Run Tests | |
run: ./gradlew clean build | |
- name: Generate Unique Tag | |
id: generate_tag | |
run: echo "::set-output name=tag::$(echo $GITHUB_SHA | cut -c1-7)" | |
- name: Build image | |
run: docker build --build-arg ACTIVE_PROFILE=${{ secrets.DEV_ACTIVE_PROFILE }} -f docker/Dockerfile -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DEV_DOCKERHUB_REPOSITORY_NAME }}:${{ steps.generate_tag.outputs.tag }} . | |
- name: Login to DockerHub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
- name: Push image | |
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DEV_DOCKERHUB_REPOSITORY_NAME }}:${{ steps.generate_tag.outputs.tag }} | |
deploy-to-aws: | |
needs: build-and-docker-operations | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Get Github action IP | |
id: ip | |
uses: haythem/[email protected] | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ secrets.AWS_REGION }} | |
- name: Add Github Actions IP to Security group | |
run: | | |
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.DEV_AWS_SG_ID }} --protocol tcp --port ${{ secrets.DEV_SSH_PORT }} --cidr ${{ steps.ip.outputs.ipv4 }}/32 | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
AWS_DEFAULT_REGION: ${{ secrets.AWS_REGION }} | |
- name: Copy Docker files | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{ secrets.DEV_SERVER_IP }} | |
username: ${{ secrets.DEV_SERVER_USERNAME }} | |
key: ${{ secrets.DEV_SSH_KEY }} | |
port: ${{ secrets.DEV_SSH_PORT }} | |
source: "docker/docker-compose.yml,docker/scripts/docker-install.sh" | |
target: 'compose' | |
rm: true | |
- name: SSH remote and Run Docker Compose | |
uses: appleboy/ssh-action@master | |
env: | |
SPRING_DATABASE_SCHEMA: ${{ secrets.DEV_SPRING_DATABASE_SCHEMA }} | |
SPRING_DATABASE_USERNAME: ${{ secrets.DEV_SPRING_DATABASE_USERNAME }} | |
SPRING_DATABASE_PASSWORD: ${{ secrets.DEV_SPRING_DATABASE_PASSWORD }} | |
SPRING_DATABASE_HOST: ${{ secrets.DEV_SPRING_DATABASE_HOST }} | |
IMAGE_TAG: ${{ needs.build-and-docker-operations.outputs.image_tag }} | |
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | |
DOCKERHUB_REPOSITORY_NAME: ${{ secrets.DEV_DOCKERHUB_REPOSITORY_NAME }} | |
with: | |
host: ${{ secrets.DEV_SERVER_IP }} | |
username: ${{ secrets.DEV_SERVER_USERNAME }} | |
key: ${{ secrets.DEV_SSH_KEY }} | |
port: ${{ secrets.DEV_SSH_PORT }} | |
envs: SPRING_DATABASE_SCHEMA,SPRING_DATABASE_USERNAME,SPRING_DATABASE_PASSWORD,SPRING_DATABASE_HOST,IMAGE_TAG,DOCKERHUB_USERNAME,DOCKERHUB_REPOSITORY_NAME | |
script: | | |
cd compose/ | |
sudo touch .env | |
sudo echo " | |
SPRING_DATABASE_SCHEMA=$SPRING_DATABASE_SCHEMA | |
SPRING_DATABASE_USERNAME=$SPRING_DATABASE_USERNAME | |
SPRING_DATABASE_PASSWORD=$SPRING_DATABASE_PASSWORD | |
SPRING_DATABASE_HOST=$SPRING_DATABASE_HOST | |
IMAGE_TAG=$IMAGE_TAG | |
DOCKERHUB_USERNAME=$DOCKERHUB_USERNAME | |
DOCKERHUB_REPOSITORY_NAME=$DOCKERHUB_REPOSITORY_NAME"| sudo tee .env | |
sh ./docker/scripts/docker-install.sh | |
sudo docker pull $DOCKERHUB_USERNAME/$DOCKERHUB_REPOSITORY_NAME:$IMAGE_TAG | |
sudo docker-compose -f ./docker/docker-compose.yml down | |
sudo docker-compose -f ./docker/docker-compose.yml up -d | |
- name: Remove Github Actions IP from security group | |
run: | | |
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.DEV_AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} |