Update GHA #23
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 and Push Docker Images | |
on: | |
push: | |
branches: [main] | |
workflow_dispatch: # For manual trigger | |
jobs: | |
generate-matrix: | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.set-matrix.outputs.matrix }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Read images.txt and generate matrix | |
id: set-matrix | |
run: | | |
image_dirs=$(cat images.txt | tr '\n' ' ') | |
image_matrix="{\"include\":[" | |
for dir in $image_dirs; do | |
version_data=$(cat ${dir}/version.json) | |
template=$(echo "${version_data}" | jq -r '.template') | |
for row in $(echo "${version_data}" | jq -r '.versions[] | @base64'); do | |
latest="" | |
_jq() { | |
echo ${row} | base64 --decode | jq -r ${1} | |
} | |
build_args="" | |
for key in $(echo "${row}" | base64 --decode | jq -r 'keys_unsorted[]'); do | |
value=$(_jq .${key}) | |
if [[ "$key" == "latest" ]]; then | |
latest=${value} | |
continue | |
fi | |
build_args+="--build-arg $(echo ${key} | awk '{print toupper($0) "_VERSION"}')=${value} " | |
$(echo ${key} | awk '{print "export " toupper($0) "_VERSION"}')=${value} | |
done | |
# Construct image tag using dynamic template variables | |
image_tag=$(eval echo "${template}") | |
# Determine additional latest tag if latest:true | |
additional_tag="" | |
if [ "${latest}" == "true" ]; then | |
additional_tag="latest" | |
fi | |
image_matrix+="{\"image_dir\":\"${dir}\",\"image_tag\":\"${image_tag}\",\"build_args\":\"${build_args}\",\"additional_tag\":\"${additional_tag}\"}," | |
done | |
done | |
image_matrix=${image_matrix%,} | |
image_matrix+="]}" | |
echo "matrix=$image_matrix" | |
echo "matrix=$image_matrix" >> $GITHUB_OUTPUT | |
build-and-push: | |
needs: generate-matrix | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: ${{ fromJson(needs.generate-matrix.outputs.matrix) }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to Docker registry with access token | |
run: | | |
echo "$DOCKER_ACCESS_TOKEN" | docker login -u dave4272t --password-stdin | |
env: | |
DOCKER_ACCESS_TOKEN: ${{ secrets.DOCKER_ACCESS_TOKEN }} | |
- name: Echo matrix values | |
run: | | |
echo "matrix2=${{ toJSON(matrix) }}" | |
- name: Build and push images | |
run: | | |
image_name="corpdk/${{ matrix.image_dir }}" | |
docker buildx build --push --platform "linux/amd64,linux/arm64" \ | |
${matrix.build_args} \ | |
-t "$image_name:${{ matrix.image_tag }}" \ | |
${matrix.additional_tag} \ | |
./${{ matrix.image_dir }} |