[containers/*] CD pipeline (#20)

* Add CD pipeline for each commit to master

Signed-off-by: Fran Mulero <fmulero@vmware.com>

* Amend branch name

Signed-off-by: Fran Mulero <fmulero@vmware.com>

* Amend regular expression to get diffent flavors

Signed-off-by: Fran Mulero <fmulero@vmware.com>

* Remove scheduled CD pipelin. Not needed anymore

Signed-off-by: Fran Mulero <fmulero@vmware.com>

* Detect all commits in a push

Signed-off-by: Fran Mulero <fmulero@vmware.com>

* Fix event reference

Signed-off-by: Fran Mulero <fmulero@vmware.com>

* Fix issues with github events

Signed-off-by: Fran Mulero <fmulero@vmware.com>

* Rename actions and outputs with clearer descriptions

Signed-off-by: Fran Mulero <fmulero@vmware.com>

* Fix job dependencies

Signed-off-by: Fran Mulero <fmulero@vmware.com>

* Update .github/workflows/cd-pipeline.yaml

Signed-off-by: Fran Mulero <fmulero@vmware.com>

Co-authored-by: Jose Antonio Carmona <joancafom@icloud.com>

* Update .github/workflows/cd-pipeline.yaml

Signed-off-by: Fran Mulero <fmulero@vmware.com>

Co-authored-by: Jose Antonio Carmona <joancafom@icloud.com>

* Apply suggestions

Signed-off-by: Fran Mulero <fmulero@vmware.com>

Co-authored-by: Jose Antonio Carmona <joancafom@icloud.com>
This commit is contained in:
Fran Mulero 2022-07-15 17:37:30 +02:00 committed by GitHub
parent db7b8bb054
commit 0fb6918cb6
3 changed files with 85 additions and 70 deletions

84
.github/workflows/cd-pipeline.yaml vendored Normal file
View File

@ -0,0 +1,84 @@
name: CD Pipeline
on: # rebuild any PRs and main branch changes
push:
branches:
- main
paths:
- 'containers/**'
env:
CSP_API_URL: https://console.cloud.vmware.com
CSP_API_TOKEN: ${{ secrets.CSP_API_TOKEN }}
VIB_PUBLIC_URL: https://cp.bromelia.vmware.com
jobs:
get-containers:
runs-on: ubuntu-latest
name: Get modified containers path
outputs:
result: ${{ steps.get-containers.outputs.result }}
containers: ${{ steps.get-containers.outputs.containers }}
steps:
- id: get-containers
name: Get modified containers path
env:
GITHUB_COMMITS: ${{ toJson(github.event.commits) }}
run: |
if [[ "${{ github.event.forced }}" == "false" ]]; then
# Get all commits associated to the push
commits=($(echo "${GITHUB_COMMITS}" | jq -r '.[] | .id'))
containers=()
for commit in "${commits[@]}"; do
# Using the Github API to detect the files changed as git merge-base stops working when the branch is behind
URL="https://api.github.com/repos/${{ github.repository }}/commits/${commit}"
files_changed_data=$(curl -s --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' -X GET -G "$URL")
files_changed="$(echo $files_changed_data | jq -r '.files[] | .filename')"
# Adding || true to avoid "Process exited with code 1" errors
containers+=($(echo "$files_changed" | xargs dirname | grep -o "containers/[^/]*/[^/]*/[^/]*" | sort | uniq || true))
done
if [[ "${#containers[@]}" -le "0" ]]; then
echo "No changes detected in containers. The rest of the steps will be skipped."
echo "::set-output name=result::skip"
else
containers_json=$(printf "%s\n" "${containers[@]}" | jq -R . | jq -cs .)
echo "::set-output name=result::ok"
echo "::set-output name=containers::${containers_json}"
fi
else
echo "Forced push detected. CD pipeline will be skipped"
echo "::set-output name=result::skip"
fi
vib-publish:
runs-on: ubuntu-latest
needs: get-containers
if: ${{ needs.get-containers.outputs.result == 'ok' }}
name: publish
strategy:
fail-fast: false
matrix:
container: ${{ fromJSON(needs.get-containers.outputs.containers) }}
steps:
- uses: actions/checkout@v3
name: Checkout Repository
# Required to search the latest commit with the tag
with:
fetch-depth: 0
- id: get-container-metadata
name: Get image tag and container name
run: |
tag="$(git log --pretty=tformat:"%s" -n 1 --grep=" release$" --author bitnami-bot@vmware.com --author containers@bitnami.com --author containers-bot@bitnami.com -- ${{ matrix.container }} | awk '{print $1}')"
name="$(echo "${{ matrix.container }}" | awk -F '/' '{print $2}')"
echo "::set-output name=tag::${tag}"
echo "::set-output name=name::${name}"
- uses: vmware-labs/vmware-image-builder-action@main
name: "Publish ${{ steps.get-container-metadata.outputs.name }}: ${{ steps.get-container-metadata.outputs.tag }}"
with:
pipeline: vib-publish.json
env:
# Path with docker resources
VIB_ENV_PATH: ${{ matrix.container }}
# Container name
VIB_ENV_CONTAINER: ${{ steps.get-container-metadata.outputs.name }}
VIB_ENV_TAG: ${{ steps.get-container-metadata.outputs.tag }}
VIB_ENV_REGISTRY_URL: ${{ secrets.OCI_REGISTRY_URL }}
VIB_ENV_REGISTRY_USERNAME: ${{ secrets.OCI_REGISTRY_USERNAME }}
VIB_ENV_REGISTRY_PASSWORD: ${{ secrets.OCI_REGISTRY_PASSWORD }}

View File

@ -1,69 +0,0 @@
# Temporary workflow to train publish actions. It will be removed in a close future
name: Scheduled CD
on:
schedule:
- cron: "?/15 * * * *"
workflow_dispatch:
inputs:
container:
description: Force VIB publication with this container
required: false
env:
CSP_API_URL: https://console.cloud.vmware.com
CSP_API_TOKEN: ${{ secrets.CSP_API_TOKEN }}
VIB_PUBLIC_URL: https://cp.bromelia.vmware.com
jobs:
get-container:
runs-on: ubuntu-latest
name: Get random container
outputs:
container: ${{ steps.get-container.outputs.container }}
flavors: ${{ steps.get-container.outputs.flavors }}
steps:
- uses: actions/checkout@v3
name: Checkout Repository
with:
fetch-depth: 0
- id: get-container
name: Get random container
run: |
container_name="${{ github.event.inputs.container }}"
if [ -z "$container_name" ]; then
containers=(containers/*)
random_index=$(( $RANDOM % ${#containers[@]} ))
container_name=$(echo "${containers[$random_index]}" | sed "s|containers/||g")
fi
flavors=(containers/${container_name}/*/*/)
flavors_json="["
for flavor in "${flavors[@]}"; do
tag="$(git log --pretty=tformat:"%s" -n 1 --grep=" release$" --author bitnami-bot@vmware.com --author containers@bitnami.com --author containers-bot@bitnami.com -- ${flavor} | awk '{print $1}')"
flavors_json+="{\"path\": \"${flavor}\", \"tag\": \"${tag}\"},"
done;
flavors_json="${flavors_json/%,/]}"
echo "::set-output name=container::${container_name}"
echo "::set-output name=flavors::${flavors_json}"
vib-publish:
runs-on: ubuntu-latest
needs: get-container
name: Publish
strategy:
fail-fast: false
matrix:
flavor: ${{ fromJSON(needs.get-container.outputs.flavors) }}
steps:
- uses: actions/checkout@v3
name: Checkout Repository
- uses: vmware-labs/vmware-image-builder-action@main
name: Publish ${{ needs.get-container.outputs.container }}
with:
pipeline: vib-publish.json
env:
# Path with docker resources
VIB_ENV_PATH: ${{ matrix.flavor.path }}
# Container name
VIB_ENV_CONTAINER: ${{ needs.get-container.outputs.container }}
VIB_ENV_TAG: ${{ matrix.flavor.tag }}
VIB_ENV_REGISTRY_URL: ${{ secrets.OCI_REGISTRY_URL }}
VIB_ENV_REGISTRY_USERNAME: ${{ secrets.OCI_REGISTRY_USERNAME }}
VIB_ENV_REGISTRY_PASSWORD: ${{ secrets.OCI_REGISTRY_PASSWORD }}

View File

@ -32,7 +32,7 @@ jobs:
files_changed="$(echo $files_changed_data | jq -r '.[] | .filename')"
# Adding || true to avoid "Process exited with code 1" errors
containers_dirs_changed="$(echo "$files_changed" | xargs dirname | grep -o "containers/[^/]*" | sort | uniq || true)"
flavors=($(echo "$files_changed" | xargs dirname | grep -o "containers/.*/.*/[^/]*" | sort | uniq || true))
flavors=($(echo "$files_changed" | xargs dirname | grep -o "containers/[^/]*/[^/]*/[^/]*" | sort | uniq || true))
flavors_json=$(printf "%s\n" "${flavors[@]}" | jq -R . | jq -cs .)
# Using grep -c as a better alternative to wc -l when dealing with empty strings."
num_containers_changed="$(echo "$containers_dirs_changed" | grep -c "containers" || true)"