workflow test12 #13
This file contains hidden or 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: BICA Backup CI/CD | |
| on: | |
| push: | |
| branches: [ main ] | |
| pull_request: | |
| branches: [ main ] | |
| env: | |
| IMAGE_NAME: bica-backup | |
| TAG: test | |
| jobs: | |
| build-and-test: | |
| name: Build & Validate Backup | |
| runs-on: ubuntu-latest | |
| services: | |
| postgres-db: | |
| image: postgres:15 | |
| env: | |
| POSTGRES_USER: myuser | |
| POSTGRES_PASSWORD: mypass | |
| POSTGRES_DB: mydatabase | |
| ports: | |
| - 5432:5432 | |
| options: >- | |
| --health-cmd pg_isready | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| # volume removido para evitar problemas de permissão | |
| # volumes: | |
| # - ./pgdata:/var/lib/postgresql/data | |
| steps: | |
| - name: Checkout repo | |
| uses: actions/checkout@v4 | |
| - name: Setup Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Build backup container | |
| run: docker build -t $IMAGE_NAME:$TAG . | |
| - name: Prepare folders | |
| run: | | |
| mkdir -p ./backups | |
| chmod 777 ./backups | |
| - name: Wait for Postgres to be ready on localhost | |
| run: | | |
| for i in {1..30}; do | |
| nc -z localhost 5432 && echo "Postgres is ready" && exit 0 | |
| echo "Waiting for Postgres..." | |
| sleep 2 | |
| done | |
| echo "Postgres did not become ready in time" | |
| exit 1 | |
| - name: Test connection to Postgres | |
| run: pg_isready -h localhost -p 5432 -U myuser | |
| - name: Run backup script test (verboso) | |
| run: | | |
| docker run --rm \ | |
| -e DB_HOST=localhost \ | |
| -e DB_PORT=5432 \ | |
| -e DB_USER=myuser \ | |
| -e DB_PASSWORD=mypass \ | |
| -e DB_NAME=mydatabase \ | |
| -e BACKUP_DIR=/mnt/backups \ | |
| -e RETENTION_DAYS=7 \ | |
| -e ENCRYPT=false \ | |
| -v $(pwd)/backups:/mnt/backups \ | |
| $IMAGE_NAME:$TAG /backup.sh | |
| - name: Check backup created | |
| run: | | |
| ls -lh ./backups | |
| test -f ./backups/*.tar.gz || (echo "Backup não foi criado!" && exit 1) | |
| docker-publish: | |
| name: Push to Docker Hub | |
| runs-on: ubuntu-latest | |
| needs: build-and-test | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Docker Login | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Push image | |
| run: | | |
| docker tag $IMAGE_NAME:$TAG ${{ secrets.DOCKER_USERNAME }}/$IMAGE_NAME:latest | |
| docker push ${{ secrets.DOCKER_USERNAME }}/$IMAGE_NAME:latest |