vendaweb-api/.github/workflows/docker-image.yml

55 lines
1.9 KiB
YAML

name: Docker CI/CD
on:
push:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
## Converter o nome do usuário do GitHub para minúsculas
- name: Convert GitHub Username to Lowercase
run: echo "GITHUB_ACTOR_LOWER=$(echo ${{ github.actor }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
## Login no GitHub Container Registry (GHCR)
- name: Log in to GitHub Container Registry (GHCR)
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
## Build e Tag da Imagem Docker
- name: Build and Tag Docker Image
run: |
docker build -t ghcr.io/$GITHUB_ACTOR_LOWER/vendaweb:latest .
docker tag ghcr.io/$GITHUB_ACTOR_LOWER/vendaweb:latest ghcr.io/$GITHUB_ACTOR_LOWER/vendaweb:$(date +%Y%m%d%H%M)
## Push da Imagem para o GHCR
- name: Push Docker Image to GitHub Container Registry (GHCR)
run: |
docker push ghcr.io/$GITHUB_ACTOR_LOWER/vendaweb:latest
docker push ghcr.io/$GITHUB_ACTOR_LOWER/vendaweb:$(date +%Y%m%d%H%M)
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Convert GitHub Username to Lowercase (Deploy)
run: echo "GITHUB_ACTOR_LOWER=$(echo ${{ github.actor }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
- name: SSH into Server and Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
password: ${{ secrets.SERVER_PASSWORD }}
script: |
docker login ghcr.io -u "$GITHUB_ACTOR_LOWER" -p "${{ secrets.GITHUB_TOKEN }}"
docker pull ghcr.io/$GITHUB_ACTOR_LOWER/vendaweb:latest
docker stop vendaweb || true
docker rm vendaweb || true
docker run -d --name vendaweb -p 80:80 ghcr.io/$GITHUB_ACTOR_LOWER/vendaweb:latest