I’m setting up a CI/CD pipeline using GitHub Actions to build and deploy a Dockerized application to an Ubuntu server. My workflow file looks like this:
name: Build and Deploy to Ubuntu Server
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/[email protected]
- name: Log in to Docker Hub
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKER_LOGIN }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
uses: docker/[email protected]
with:
push: true
tags: ${{ secrets.DOCKER_LOGIN }}/my-project:latest
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Deploy to Server
uses: appleboy/[email protected]
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
docker pull ${{ secrets.DOCKER_LOGIN }}/my-project:latest
docker-compose -f docker-compose.production.yml down --remove-orphans
docker-compose -f docker-compose.production.yml up -d
sudo systemctl reload nginx || echo "Failed to reload Nginx. May require manual intervention."
The build job runs successfully, and the Docker image is built and pushed to Docker Hub. However, during the deploy job, I encounter the following error:
err: stat /home/***/docker-compose.production.yml: no such file or directory
The relevant part of the script in the deploy job is:
docker pull ${{ secrets.DOCKER_LOGIN }}/my-project:latest
docker-compose -f docker-compose.production.yml down --remove-orphans
docker-compose -f docker-compose.production.yml up -d
sudo systemctl reload nginx || echo "Failed to reload Nginx. May require manual intervention."
Why is the workflow searching for the docker-compose.production.yml
file on the server’s file system when the Docker image already contains the file? When I run docker pull, the image includes the following files:
> docker run -it --rm {username}/my-project /bin/bash
> ls -R
File listed as:
Dockerfile
docker-compose.development.yml
docker-compose.production.yml
main.py
requirements.txt
Why doesn’t it automatically use the docker-compose.production.yml
file from the Docker image, and what should I add to the GitHub Actions workflow to make this work as expected?