test
# cat build.yml
name: Build and Deploy
on:
push:
jobs:
build:
name: Build Job
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Docker Hub Login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Get current date
run: echo "CI_DATE=$(date +'%F')" >> $GITHUB_ENV
- name: Get branch name
run: echo "BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_ENV
- name: Get repo name
run: echo "REPO_NAME=$(echo $GITHUB_REPOSITORY | cut -d '/' -f 2)" >> $GITHUB_ENV
- name: Docker build and push
uses: docker/build-push-action@v4
with:
context: api
push: true
platforms: linux/amd64
tags: |
${{ secrets.DOCKER_HUB_USERNAME }}/${{ env.REPO_NAME }}:${{ env.BRANCH_NAME }}
${{ secrets.DOCKER_HUB_USERNAME }}/${{ env.REPO_NAME }}:${{ env.BRANCH_NAME }}-${{ env.CI_DATE }}
${{ secrets.DOCKER_HUB_USERNAME }}/${{ env.REPO_NAME }}:latest
deploy:
name: Deploy Job
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Get repo name
run: echo "REPO_NAME=$(echo $GITHUB_REPOSITORY | cut -d '/' -f 2)" >> $GITHUB_ENV
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST_IP }}
username: ${{ secrets.DOCKER_HUB_USERNAME }}
key: ${{ secrets.HOST_SSH_KEY }}
script: |
cd /home/${{ secrets.DOCKER_HUB_USERNAME }}/1/${{ env.REPO_NAME }}
docker-compose down
docker-compose -f docker-compose.yml up -d
docker image prune --all --force
docker system prune -a -f