This GitHub Action deploys multiple Docker Compose files to a Portainer instance. It detects changes in Docker Compose files and either updates the existing stack or creates a new one in the specified Portainer environment.
In order to deploy the docker compose files to the right environment, a specific repo structure is desired so that the action can act accordingly. The first level should be the name of the environment to which the stack will be deployed. Second level is the name of the deployed stack. Below an example:
.
├── .github
│ └── workflows
│ └── deploy.yml
├── production
│ ├── service1
│ │ └── docker-compose.yml
│ ├── service2
│ │ └── docker-compose.yml
│ └── serviceN
│ └── docker-compose.yml
├── staging
│ ├── service1
│ │ └── docker-compose.yml
│ ├── service2
│ │ └── docker-compose.yml
│ └── serviceN
│ └── docker-compose.yml
└── README.md
portainer_url: The URL of the Portainer instance.
portainer_api_key: The API key for Portainer authentication.
changed_files_path: Path to the file containing changed Docker Compose files.
environment_file: (OPTIONAL) File containing environment variables
To use this action in your GitHub workflow, create a .github/workflows/deploy.yml
file in your repository with the following content:
name: Deploy to Portainer
on:
push:
paths:
- '**/docker-compose.yml'
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Deploy to Portainer
uses: ./ # Assuming the action is in the root of your repository
with:
portainer_url: ${{ secrets.PORTAINER_URL }}
portainer_api_key: ${{ secrets.PORTAINER_API_KEY }}
changed_files_path: 'changed_files.txt' # This file should be generated by a previous step
environment_file: '.env' # OPTIONAL: path to environment file
You need to generate the changed_files.txt file that contains the paths of changed Docker Compose files. You can do this with a simple shell command:
- name: Get changed Docker Compose files
run: |
git diff --name-only HEAD^ HEAD | grep 'docker-compose.yml' > changed_files.txt
Bram Kelchtermans