cicd : git actions μμ #47
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 Deploy to AWS | |
on: | |
push: | |
branches: [ "cicd/#41" ] | |
jobs: | |
build-deploy: | |
runs-on: ubuntu-latest | |
environment: DEV | |
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: 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: Grant Permissions for Gradle build | |
run: chmod +x gradlew | |
- name: Gradle building | |
run: ./gradlew build -x test | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Extract Docker metadata | |
id: metadata | |
uses: docker/metadata-action@v4 | |
with: | |
images: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} | |
tags: | | |
type=sha,prefix= | |
type=ref,event=branch | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
push: true | |
tags: ${{ steps.metadata.outputs.tags }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
- name: Deploy on EC2 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.AWS_EC2_IP }} | |
username: ${{ secrets.AWS_EC2_USER }} | |
key: ${{ secrets.AWS_EC2_SSH_KEY }} | |
port: 22 | |
script: | | |
cd /home/ubuntu | |
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:${{ github.sha }} | |
docker-compose down | |
docker-compose build --no-cache | |
docker-compose up -d --force-recreate |