git.s-ol.nu mmm / b08d422
update self-hosted s-ol 16 days ago
1 changed file(s) with 56 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
1010 - [klaus][klaus] web frontend for browsing and cloning public repos
1111 - fine-grained permissions and SSH public-key access
1212 - micro 'CI' setup rebuilds & redeploys docker images when updates are pushed
13
14 **UPDATE (2019-10-03)**: I updated the git hook below to one that supports pushing and building
15 multiple branches based on the `docker-compose.yml`.
1316
1417 Most of these projects are very well documented so I won't go into a lot of detail on setting them up.
1518
4750 - ./acme.json:/acme.json
4851 container_name: traefik
4952
50
5153 networks:
5254 web:
5355 external: true
6163 For example to start `redirectly`, a tiny link redirect service, this addition suffices:
6264
6365 redirectly:
64 image: local/redirectly:git
66 image: local/redirectly:master
6567 restart: always
6668 networks:
6769 - web
150152 Here is the `docker-deploy` hook I am using:
151153
152154 #!/bin/bash
153 read oldrev newrev refname
154155 set -e
155156
156 # Get project name
157 PROJECT=$(basename "$PWD")
158 PROJECT=${PROJECT%.git}
159
160 # Paths
161 CHECKOUT_DIR=/tmp/git/${PROJECT}
162 TARGET_DIR=... # POINT TO docker-compose.yml directory
163
164 if [ ! -d ${TARGET_DIR} ]; then
165 echo -e "\e[1;32mNo target directory to compile into\e[00m"
166 fi
167
168 mkdir -p ${CHECKOUT_DIR}
169 GIT_WORK_TREE=${CHECKOUT_DIR} git checkout -q -f $newrev
170 echo -e "\e[1;32mChecked out ${PROJECT}.\e[00m"
171
172 cd ${CHECKOUT_DIR}
173 docker build -t local/${PROJECT}:git .
174 echo -e "\e[1;32mImage built.\e[00m"
175
176 cd ${TARGET_DIR}
177 docker-compose up -d ${PROJECT}
178 echo -e "\e[1;32mContainer restarted.\e[00m"
179
180 It will build a `local/$REPO:git` image whenever you push, then run `docker-compose up -d $REPO` in `$TARGET_DIR`.
181 Just make sure that your docker-compose service is called the same as the image name (like in the `redirectly` example above).
182
183 This hook definetely needs an addition to filter which pushes trigger a redeploy (e.g. only tags or only master),
184 but that should be trivial to add (and well documented online).
157 while read oldrev newrev refname
158 do
159 BRANCH="$(git rev-parse --symbolic --abbrev-ref $refname)"
160
161 # Get project name
162 PROJECT="$PWD"
163 PROJECT="${PROJECT#*/repositories/public/}"
164 PROJECT="${PROJECT#*/repositories/}"
165 PROJECT="${PROJECT%.git}"
166 PROJECT="$(echo "$PROJECT" | tr "/ " "-_")"
167
168 # Paths
169 CHECKOUT_DIR=/tmp/git/$PROJECT
170 TARGET_DIR=/home/s-ol/aerol
171 IMAGE_NAME=local/$PROJECT:$BRANCH
172
173 # this one doesn't require python & yq, but it means the container has to run already...
174 # SERVICES=$(docker ps --filter "ancestor=${IMAGE_NAME}" --format '{{.Label "com.docker.compose.service"}}' \
175 # | sort | uniq)
176
177 SERVICES=$(yq -r <"$TARGET_DIR/docker-compose.yml" \
178 ".services | to_entries | map(select(.value.image == \"${IMAGE_NAME}\").key) \
179 | join(\" \")")
180
181 if [ -z "$SERVICES" ]; then
182 continue
183 fi
184
185 mkdir -p "$CHECKOUT_DIR"
186 GIT_WORK_TREE="$CHECKOUT_DIR" git checkout -q -f $newrev
187 echo -e "\e[1;32mChecked out '$PROJECT'.\e[00m"
188
189 cd "$CHECKOUT_DIR"
190 docker build -t "$IMAGE_NAME" .
191 echo -e "\e[1;32mImage '$IMAGE_NAME' built.\e[00m"
192
193 cd "$TARGET_DIR"
194 docker-compose up -d $SERVICES
195 echo -e "\e[1;32mService(s) '$SERVICES' restarted.\e[00m"
196 done
197
198 It will build a `local/$REPO:$BRANCH` image whenever you push, then run `docker-compose up -d $SERVICES` in `$TARGET_DIR`,
199 where `$SERVICES` are all the docker-compose services that use the image. If there are none, no image will be built.
200 For this to work it has to parse the `docker-compose.yaml` file, which means you have to install [`yq`][yq] and `jq`, e.g. on Ubuntu:
201
202 sudo apt-get install jq python3-pip
203 sudo pip install yq
204
205 If you would like to avoid that, you can use the commented command for `SERVICES=` above, which only relies on docker itself,
206 the only problem is that you will have to do the first build manually (or re-tag a dummy image) before the first build,
207 since it can only detect containers that are already running.
185208
186209 ---
187210
196219 [traefik-in-docker]: https://blog.kilian.io/server-setup/
197220 [klaus-autoreload]: https://github.com/jonashaag/klaus/wiki/Autoreloader
198221 [gitolite-hooks]: http://gitolite.com/gitolite/cookbook#v36-variation-repo-specific-hooks
222 [yq]: https://github.com/kislyuk/yq
199223
200224 [merveilles]: https://merveilles.town/@s_ol