Listen for webhooks and execute a deployment script
Express based HTTP Endpoint that listens to webhooks. Downloads a file named deploy.crane from the repository and executes it.
I use it with Gogs to deploy my Docker container to a production environment.
Install it globally:
npm install container-crane -g
and then start it:
As a Docker container
# (optional )Create a network for Gogs and container-crane (not port mappings required)docker network create deployment-net# Pull image from Docker Hub.docker pull devthings/container-crane# Use `docker run` for the first time.# map the docker.sock from host within the container# if you want to use docker commands in your deploy scripts# (to deploy "neighbor" containers).docker run --name="container-crane" --net="deployment-net" --restart=always -d -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/bin/docker devthings/container-crane# Use `docker start` if you have stopped it.docker start container-crane# (optional) Restart your Gogs container within the deployment-net network# docker run --name="gogs" --net=deployment-net // other params
Setup a webhook in your Gogs repository that points to the /gogs/ endpoint of the container-crane container:
Now you need a deployment script for your app. You can write it in bash or node (both is installed on the container-crane Docker image)
Create a file named deploy.crane within your repository.
Here is an example as a bash script:
#! /bin/bashecho "stop the container"docker stop my-appecho "remove container"docker rm my-appecho "build image"docker build -t my-app --force-rm=true http://gogs:3000/my-gogs-user/my-app.gitecho "run the container"docker run --name=my-app --restart=always -d -p 3000:3000 my-app
NOTE: container-crane depends not on docker! The deploy.crane script depends on your hosting environment.
Now you can simply:
git push your-gogs-origin
After container-crane receive a POST request from Gogs, it parses the webhook request body and downloads a script named deploy.crane. When the file is downloaded it will be executed.
- Installation as container from Docker Hub
- Handle secret from Gogs request
- Support other endpoints:
To execute tests run:
To execute tests with watch run:
npm run dev
If you discover any bugs, feel free to create an issue on GitHub fork and send me a pull request.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request