new cd build #8
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: CI/CD Pipeline | |
on: | |
push: | |
branches: [ main ] | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Slack commit message and sha | |
run: > | |
curl -X POST -H 'Content-type: application/json' | |
--data '{"text":":github: https://github.com/miliariadnane/herb-classifier-api/commit/${{ github.sha }} - ${{ github.event.head_commit.message }}"}' | |
${{ secrets.SLACK_WEBHOOK_URL }} | |
- name: Send Slack Message | |
run: > | |
curl -X POST -H 'Content-type: application/json' | |
--data '{"text":"Deployment started :progress_bar: :fingerscrossed:"}' | |
${{ secrets.SLACK_WEBHOOK_URL }} | |
- uses: actions/setup-java@v3 | |
with: | |
distribution: 'temurin' | |
java-version: '17' | |
cache: 'maven' | |
- name: Set build version number | |
id: build-version | |
run: echo "BUILD_VERSION=$(date '+%d.%m.%Y.%H.%M.%S')" >> $GITHUB_ENV | |
- name: Send Slack Message | |
run: > | |
curl -X POST -H 'Content-type: application/json' | |
--data '{"text":":maven: Build project with Maven"}' | |
${{ secrets.SLACK_WEBHOOK_URL }} | |
- name: Login to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_HUB_USERNAME }} | |
password: ${{ secrets.DOCKER_HUB_PASSWORD }} | |
- name: Build Docker image | |
run: docker build . --file Dockerfile --tag miliariadnane/herb-classifier-api:${{ env.BUILD_VERSION }} | |
- name: Push Docker image | |
run: docker push miliariadnane/herb-classifier-api:${{ env.BUILD_VERSION }} | |
- name: Send Slack Message | |
run: > | |
curl -X POST -H 'Content-type: application/json' | |
--data '{"text":":docker: Image tag:${{ env.BUILD_VERSION }} pushed to dockerhub in https://hub.docker.com/repository/docker/miliariadnane/herb-classifier-api"}' | |
${{ secrets.SLACK_WEBHOOK_URL }} | |
- name: Setup SSH | |
run: | | |
mkdir -p ~/.ssh/ | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
- name: SSH and Deploy Docker image | |
run: | | |
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_IP }} << EOF | |
docker pull miliariadnane/herb-classifier-api:${{ env.BUILD_VERSION }} | |
docker stop $(docker ps -q --filter ancestor=miliariadnane/herb-classifier-api) || true | |
docker -H ssh://${{ secrets.SERVER_USER }}@${{ secrets.SERVER_IP }} run -d -p 80:80 miliariadnane/herb-classifier-api:${{ env.BUILD_VERSION }} | |
EOF | |
- name: Send Slack Message | |
run: > | |
curl -X POST -H 'Content-type: application/json' | |
--data '{"text":"Deployment and commit completed :github-check-mark: :party_blob: - http://130.61.32.252:32768/api/v1/herb-classifier/service/"}' | |
${{ secrets.SLACK_WEBHOOK_URL }} | |
- name: Send Slack Message | |
if: always() | |
run: > | |
curl -X POST -H 'Content-type: application/json' | |
--data '{"text":"Job Status ${{ job.status }}"}' | |
${{ secrets.SLACK_WEBHOOK_URL }} |