Custom Docker Build and Test #106
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Custom Docker Build and Test | |
on: | |
workflow_call: | |
inputs: | |
enabled: | |
description: 'Enable Workflow' | |
required: true | |
type: boolean | |
default: false | |
workflow_dispatch: | |
inputs: | |
enabled: | |
description: 'Enable Workflow' | |
required: true | |
type: boolean | |
default: false | |
concurrency: | |
group: ${{ github.ref }}-2 | |
cancel-in-progress: true | |
permissions: | |
contents: read | |
jobs: | |
custom-docker-build: | |
runs-on: ubuntu-latest | |
strategy: | |
fail-fast: false | |
steps: | |
- name: Harden Runner | |
uses: step-security/harden-runner@55d479fb1c5bcad5a4f9099a5d9f37c8857b2845 # v2.4.1 | |
with: | |
egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- uses: docker-practice/actions-setup-docker@master | |
timeout-minutes: 12 | |
- run: | | |
set -x | |
docker version | |
- name: Get current Docker image tag | |
id: get_tag | |
run: | | |
CURRENT_TAG=$(curl -s "https://registry.hub.docker.com/v2/repositories/unskript/awesome-runbooks/tags/" | jq -r '.results[].name' | grep '^minimal-' | sort -rV | head -n 1) | |
echo "CURRENT TAG IS $CURRENT_TAG" | |
echo "::set-output name=current_tag::$CURRENT_TAG" | |
- name: Create temporary Dockerfile | |
run: | | |
echo "FROM unskript/awesome-runbooks:${{ steps.get_tag.outputs.current_tag }}" > Dockerfile.temp | |
- name: Build custom Docker image | |
run: | | |
docker build -t my-custom-image:latest -f Dockerfile.temp . | |
- name: Run custom Docker image | |
run: | | |
docker run -d --name my-test-container my-custom-image:latest | |
sleep 30 # Give some time for the container to start | |
- name: Some Docker Logs | |
run: | | |
docker logs my-test-container | |
docker ps -a | |
- name: Run commands in Docker container | |
id: run_commands | |
run: | | |
docker exec my-test-container bash -c "ps -ef | grep -i gotty" | |
docker exec my-test-container bash -c "ls -lart /var/log/unskript/notebook.out" | |
continue-on-error: true | |
- name: Check for errors | |
run: | | |
container_id=$(docker ps -q --filter "name=my-test-container") | |
if [[ -z "$container_id" ]]; then | |
echo "Container not running." | |
exit 1 | |
fi | |
gotty_running=$(docker exec $container_id pgrep gotty) | |
if [[ -z "$gotty_running" ]]; then | |
echo "GoTTY process not running in the container." | |
exit 1 | |
fi | |
echo "GoTTY started successfully." | |
directory="/unskript/data/actions" | |
if [ -z "$(ls -A $directory)" ]; then | |
echo "$directory is empty as expected." | |
else | |
echo "$directory is not empty, custom legos are detected!" | |
exit 1 | |
fi | |
- name: Notify on Slack (Success case) | |
if: success() | |
uses: slackapi/[email protected] | |
with: | |
# For posting a rich message using Block Kit | |
channel-id: "${{ secrets.SLACK_CHANNEL_ID }}" | |
payload: | | |
{ | |
"text": "GitHub Action build result: ${{ job.status }}", | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": ":tada: Custom Docker Build Verification for `${{ steps.get_tag.outputs.current_tag }}` *Successful*" | |
} | |
} | |
] | |
} | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} | |
- name: Notify on Slack (Failure case) | |
if: failure() | |
uses: slackapi/[email protected] | |
with: | |
# For posting a rich message using Block Kit | |
channel-id: "${{ secrets.SLACK_CHANNEL_ID }}" | |
payload: | | |
{ | |
"text": "GitHub Action build result: ${{ job.status }}", | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": ":no-entry: Attention! Custom Docker Build Verification for `${{ steps.get_tag.outputs.current_tag }}` *Failed!* " | |
} | |
} | |
] | |
} | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} |