Skip to content

Test github action with if condition #8

Test github action with if condition

Test github action with if condition #8

Workflow file for this run

name: Deploy to Docker Hub
on:
push:
branches: [ "main" ]
workflow_dispatch:
jobs:
build:
name: Build Java Project
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, 'no-deploy')"
permissions:
contents: read
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'corretto'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew build
- name: Verify build output
run: ls -al build/libs
- name: Archive build artifacts
uses: actions/upload-artifact@v4
with:
name: build-artifacts
path: build/libs/*.jar
push:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
needs: build
permissions:
contents: read
packages: write
actions: write
id-token: write
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: build-artifacts
path: build-artifacts/
- name: Verify downloaded artifacts
run: ls -al build-artifacts
- name: Log in to Docker Hub with token
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Build and push Docker image
run: |
mkdir -p build/libs
mv build-artifacts/*.jar build/libs/
docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/githubactionstudy:latest .
- name: Push Docker image
run: docker push ${{ secrets.DOCKER_HUB_USERNAME }}/githubactionstudy:latest
deploy:
name: Deploy to EC2
runs-on: ubuntu-latest
needs: push
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.AWS_EC2_HOST }}
username: ubuntu
key: ${{ secrets.AWS_EC2_SSH_KEY }}
script: |
echo "${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}" | docker login --username ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin
cd /home/ubuntu
export SECRET_VALUE=${{ secrets.SECRET_VALUE}}
export DOCKER_HUB_USERNAME=${{ secrets.DOCKER_HUB_USERNAME }}
docker-compose down || true
docker rm -f $(docker ps -a -q) || true
docker-compose pull app
docker-compose up -d