diff --git a/.github/workflows/image-pull-and-tag.yaml b/.github/workflows/image-pull-and-tag.yaml index 247fbcef..eb850131 100644 --- a/.github/workflows/image-pull-and-tag.yaml +++ b/.github/workflows/image-pull-and-tag.yaml @@ -1,4 +1,3 @@ -# name: Retag and Build Images to GHCR on: @@ -14,14 +13,41 @@ env: IMAGE_NAME: ${{ github.repository }} jobs: - retag-images: + prepare-matrix: runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.prepare.outputs.matrix }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install Tools + run: | + sudo apt-get update && sudo apt-get install -y jq + sudo wget -O /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 + sudo chmod +x /usr/local/bin/yq + - name: Read images from file + id: prepare + run: | + if [ ! -f .original-images.yaml ]; then + echo "File .original-images.yaml not found!" + exit 1 + fi + + images=$(yq '.[]' .original-images.yaml) + matrix=$(echo "$images" | jq -R -s -c 'split("\n") | map(select(length > 0) | {path: ., tag: "latest"})') + echo "::set-output name=matrix::$matrix" + + retag-images: + runs-on: ubuntu-latest + needs: prepare-matrix strategy: fail-fast: false max-parallel: 3 matrix: - image: [] + image: ${{ fromJson(needs.prepare-matrix.outputs.matrix) }} steps: - name: Checkout repository @@ -31,42 +57,19 @@ jobs: uses: docker/setup-buildx-action@v2 - name: Log in to the Container registry - uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + uses: docker/login-action@v2 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Read images from file and set matrix - id: read-images + - name: Pull, retag, and push Docker image run: | - if [ ! -f .original-images.yaml ]; then - echo "File .original-images.yaml not found!" - exit 1 - fi - - echo "Reading images from .original-images.yaml" - images=$(yq '.[]' .original-images.yaml) - json_matrix=$(echo "$images" | jq -R -s -c 'split("\n") | map(select(length > 0) | split(":") | {path: .[0], tag: .[1]})') - echo "matrix=$json_matrix" >> $GITHUB_ENV - - - name: Dynamically update matrix - uses: actions/github-script@v6 - with: - script: | - const matrix = JSON.parse(process.env.matrix); - return { matrix }; - - - name: Build and push Docker image - uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 - with: - context: . - push: true - tags: | - ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/$(basename ${{ matrix.image.path }}):${{ matrix.image.tag }} - build-args: | - BASE_IMAGE=${{ matrix.image.path }} - BASE_TAG=${{ matrix.image.tag }} + ORIGINAL_IMAGE=${{ matrix.image.path }} + TAGGED_IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/$(basename ${ORIGINAL_IMAGE})" + docker pull $ORIGINAL_IMAGE + docker tag $ORIGINAL_IMAGE $TAGGED_IMAGE + docker push $TAGGED_IMAGE - name: Logout from Docker run: docker logout ${{ env.REGISTRY }}