Setup & Cleanup
In some cases, you will need to prepare and clean before going out. Let's see here the simple ways
Setup
As explained in section Spec file, your steps will be executed in order, so, for Setup, just add one step before all others.
Cleanup
We do not guarantee that the last step will be executed, because if the build fails before, the following steps will not be executed.
On the other hand, as you are simply in a bash script, you can use "trap".
#!/bin/bash
clean_up() {
echo "Receiving SIG, deleting server before exiting..."
openstack server delete tmp-nrt
exit 0
}
trap 'clean_up' EXIT
version: '1.0'
from: 'romainlavabrefairfair/deploy-nrt'
steps:
- name: '@before-exit'
script: '/.free-commit/nrt/before-exit.sh'
- name: '@openstack-login'
script: '/.free-commit/nrt/openstack-login.sh'
- name: '@launch-server'
script: '/.free-commit/nrt/launch-server.sh'
- name: '@clone'
script: '/.free-commit/nrt/clone.sh'
- name: '@run'
script: '/.free-commit/nrt/run.sh'
- name: '@publish-result'
script: '/.free-commit/nrt/publish-result.sh'
You are sure that your script file will be executed if your build succeeds or fails.
Problem
If you abort your build, you will have a problem, why ?
Let's modify our before-exit.sh script like this
#!/bin/bash
clean_up() {
echo "Receiving SIG, deleting server before exiting..."
openstack server delete tmp-nrt
exit 0
}
trap 'clean_up' EXIT
trap 'clean_up' TERM INT [...] # We listen to SIG
With this script, if your docker container is stopped, you are not sure if the clean_up function will be executed, because if you are waiting for sub process (npm ci or other), your script will not be executed.
Solution
So, to prevent the risk (high cost or other problem), we offer you a way to be sure that your exit script is executed (except if your server crashes violently) :
Let's undo the changes on before-exit.sh
#!/bin/bash
clean_up() {
echo "Receiving SIG, deleting server before exiting..."
openstack server delete tmp-nrt
exit 0
}
trap 'clean_up' EXIT
And we create a second script named handle-abort.sh or any name that suits you
#!/bin/bash
openstack server delete tmp-nrt
echo "I support arg $1"
And we modify our deploy.yaml
version: '1.0'
from: 'romainlavabrefairfair/deploy-nrt'
steps:
- name: '@cleanup' # name your step like this and nothing else
script: /.free-commit/nrt/handle-abort.sh hello-world
- name: '@before-exit'
script: '/.free-commit/nrt/before-exit.sh'
- name: '@openstack-login'
script: '/.free-commit/nrt/openstack-login.sh'
- name: '@launch-server'
script: '/.free-commit/nrt/launch-server.sh'
- name: '@clone'
script: '/.free-commit/nrt/clone.sh'
- name: '@run'
script: '/.free-commit/nrt/run.sh'
- name: '@publish-result'
script: '/.free-commit/nrt/publish-result.sh'
The @cleanup step will be executed only if you abort your build, to be specific, here's what will happen if you abort your build
docker exec container-name bash -c "cd /app && . handle-abort.sh hello-world"
docker container stop container-name -s SIGKILL
Now, you are sure that your cleanup script will be executed.
⚠️ In you cleanup script you don't have access to your build context, only environment variables and filesystem
Last updated