feature: push and pull from dockerhub #2
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 to staging | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
CI: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Set up Docker environment | |
run: | | |
echo "DOCKER_BUILDKIT=1" >> $GITHUB_ENV | |
echo "COMPOSE_DOCKER_CLI_BUILD=1" >> $GITHUB_ENV | |
- name: Build and push with Docker Compose | |
run: | | |
docker compose -f docker-compose.yml build rest-api | |
docker compose -f docker-compose.yml push rest-api | |
CD: | |
needs: CI | |
runs-on: | |
- self-hosted | |
- staging | |
steps: | |
- name: Checkout the latest code | |
uses: actions/checkout@v3 | |
- name: Docker Hub login | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Remove specific old containers if they exist | |
run: | | |
docker stop rest-api-staging || true | |
docker rm rest-api-staging || true | |
- name: Pull the Docker images | |
run: | | |
cd $GITHUB_WORKSPACE | |
docker compose -f docker-compose.yml pull rest-api | |
- name: Start the containers | |
run: | | |
cd $GITHUB_WORKSPACE | |
docker compose -f docker-compose.yml up -d rest-api | |
- name: Remove old Docker images | |
run: | | |
docker rmi $(docker images -f "dangling=true" -q) || true | |
# docker rmi $(docker images devroadr/python-api-server -f "dangling=true" -q) || true |