Building service containers #43
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: build-containers | |
run-name: Building service containers | |
on: | |
push: | |
env: | |
BASE_IMAGE: cccs/assemblyline-v4-service-base:stable | |
REGISTRY: ghcr.io | |
PUSH_REGISTRY: ghcr.io | |
BASE_TAG: 4.4.0.stable | |
jobs: | |
build-base: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
defaults: | |
run: | |
working-directory: al-service-with-py11 | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
submodules: true | |
- name: Authorize to GitHub Packages | |
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin | |
- name: Check if the current version has already been pushed | |
id: check-if-pushed | |
run: | | |
export GHCR_TOKEN=$(echo ${{ secrets.GITHUB_TOKEN }} | base64) | |
export TAG=$BASE_TAG$(cat BASE_VERSION) | |
echo manifest_base=$(curl -s -H "Authorization: Bearer ${GHCR_TOKEN}" https://ghcr.io/v2/kam193/assemblyline-base-py11/manifests/$TAG | grep "manifest unknown") >> $GITHUB_OUTPUT | |
export TAG=$BASE_TAG$(cat VERSION) | |
echo manifest_service=$(curl -s -H "Authorization: Bearer ${GHCR_TOKEN}" https://ghcr.io/v2/kam193/assemblyline-service-base-py11/manifests/$TAG | grep "manifest unknown") >> $GITHUB_OUTPUT | |
- name: Build base | |
if: steps.check-if-pushed.outputs.manifest_base | |
run: | | |
make build-base | |
- name: Push base image | |
if: steps.check-if-pushed.outputs.manifest_base | |
run: | | |
make push-base | |
- name: Build service base | |
if: steps.check-if-pushed.outputs.manifest_service | |
run: | | |
make build-service | |
- name: Push service image | |
if: steps.check-if-pushed.outputs.manifest_service | |
run: | | |
make push-service | |
discover-services: | |
needs: build-base | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Discover directories with Dockerfiles | |
id: services | |
run: | | |
echo "services=$(find . -type f -name Dockerfile | xargs -n 1 dirname | uniq | cut -d '/' -f 2 | grep -v 'TEMPLATE' | grep -v 'al-service-with-py11' | jq -R -s -c 'split("\n")[:-1]')" >> "$GITHUB_OUTPUT" | |
- name: Print services with Dockerfiles | |
run: | | |
echo "Services with Dockerfiles: ${{ steps.services.outputs.services }}" | |
outputs: | |
services: ${{ steps.services.outputs.services }} | |
build-containers: | |
needs: discover-services | |
runs-on: ubuntu-latest | |
if: needs.discover-services.outputs.services != '[]' | |
strategy: | |
matrix: | |
service: ${{ fromJson(needs.discover-services.outputs.services) }} | |
permissions: | |
contents: read | |
packages: write | |
defaults: | |
run: | |
working-directory: ${{ matrix.service }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Authorize to GitHub Packages | |
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin | |
- name: Authorize to Github Docker Registry | |
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin | |
- name: Build container | |
run: | | |
make build | |
- name: Check service was changed in the commit | |
id: check-service-changed | |
run: | | |
git diff --quiet ${{ github.event.before }} ${{ github.event.after }} -- ${{ matrix.service }} | |
echo exit-code=$? >> $GITHUB_OUTPUT | |
- name: Run tests | |
if: steps.check-service-changed.outputs.exit-code != 0 | |
run: | | |
make test | |
- name: Check if the current version has already been pushed | |
id: check-if-pushed | |
run: | | |
export GHCR_TOKEN=$(echo ${{ secrets.GITHUB_TOKEN }} | base64) | |
export TAG=$BASE_TAG$(cat VERSION) | |
echo manifest=$(curl -s -H "Authorization: Bearer ${GHCR_TOKEN}" https://ghcr.io/v2/kam193/$(make print)/manifests/$TAG | grep "manifest unknown") >> $GITHUB_OUTPUT | |
- name: Push container | |
if: steps.check-if-pushed.outputs.manifest | |
run: | | |
make push |