3.9 KiB
Portainer Stack Utils
Bash script to deploy/undeploy stacks in a Portainer instance from a docker-compose yaml file. Based on previous work by @vladbabii on docker-how-to/portainer-bash-scripts.
Supported Portainer API
Script was created for the latest Portainer API, which at the time of writing is 1.9.2.
Requirements
How to use
The provided psu
script allows to deploy/update/undeploy portainer stacks. Settings can be passed through envvars and/or flags. Both envars and flags can be mixed but flags will always overwrite envvar values. When deploying a stack, if it doesn't exist a new one is created, otherwise it's updated.
With envvars
This is particularly useful for CI/CD pipelines.
ACTION
(string, required): deploy/undeployPORTAINER_USER
(string, required): UsernamePORTAINER_PASSWORD
(string, required): PasswordPORTAINER_URL
(string, required): URL to PortainerPORTAINER_STACK_NAME
(string, required): Stack nameDOCKER_COMPOSE_FILE
(string, required if action=deploy): Path to doker-compose filePORTAINER_PRUNE
("true" or "false", optional): Whether to prune unused containers or not. Defaults to"false"
.PORTAINER_ENDPOINT
(int, optional): Which endpoint to use. Defaults to1
.HTTPIE_VERIFY_SSL
("yes" or "no", optional): Whether to verify SSL certificate or not. Defaults to"yes"
.VERBOSE_MODE
("true" or "false", optional): Whether to activate verbose output mode or not. Defaults to"false"
.
Examples
export ACTION="deploy"
export PORTAINER_USER="admin"
export PORTAINER_PASSWORD="password"
export PORTAINER_URL="http://portainer.local"
export PORTAINER_STACK_NAME="mystack"
export DOCKER_COMPOSE_FILE="/path/to/docker-compose.yml"
./psu
export ACTION="undeploy"
export PORTAINER_USER="admin"
export PORTAINER_PASSWORD="password"
export PORTAINER_URL="http://portainer.local"
export PORTAINER_STACK_NAME="mystack"
./psu
With flags
This is more suitable for standalone script usage.
-a
(string, required): deploy/undeploy-u
(string, required): Username-p
(string, required): Password-l
(string, required): URL to Portainer-n
(string, required): Stack name-c
(string, required if action=deploy): Path to doker-compose file-r
("true" or "false", optional): Whether to prune unused containers or not. Defaults to"false"
.-e
(int, optional): Which endpoint to use. Defaults to1
.-s
("yes" or "no", optional): Whether to verify SSL certificate or not. Defaults to"yes"
.-v
("true" or "false", optional): Whether to activate verbose output mode or not. Defaults to"false"
.
Examples
./psu -a deploy -u admin -p password -l http://portainer.local -n mystack -c /path/to/docker-compose.yml
./psu -a undeploy -u admin -p password -l http://portainer.local -n mystack
License
Source code contained by this project is licensed under the GNU General Public License version 3. See LICENSE file for reference.