From 89a7aa5acfbb8c2f2354b321636fe86fec1f2bc1 Mon Sep 17 00:00:00 2001 From: fernandoataoldotcom Date: Sat, 4 Nov 2023 19:32:34 -0600 Subject: [PATCH] feat: use tags generator from marketplace action (#18) --- .../test-action-on-pr-and-schedule.yml | 21 ++++++ README.md | 10 +-- action.yml | 72 +++++++++---------- 3 files changed, 61 insertions(+), 42 deletions(-) diff --git a/.github/workflows/test-action-on-pr-and-schedule.yml b/.github/workflows/test-action-on-pr-and-schedule.yml index b3bcfd8..966934a 100644 --- a/.github/workflows/test-action-on-pr-and-schedule.yml +++ b/.github/workflows/test-action-on-pr-and-schedule.yml @@ -108,3 +108,24 @@ jobs: docker pull $DOCKERHUB_TEST_IMAGE_NAME:${{ github.sha }} echo "::endgroup::" docker run -e REGISTRY=docker.io $DOCKERHUB_TEST_IMAGE_NAME:${{ github.sha }} + + - name: generate epoch time string for custom tag + id: epoch-tag + run: echo "EPOCH_TAG=$(date +%s)" >> $GITHUB_ENV + + - name: run ghcr.io with custom tag + uses: ./ + with: + image_name: ${{ env.TEST_IMAGE_NAME }} + registry: "ghcr.io" + tags: ${{ env.EPOCH_TAG }} + context: "./test-directory/tests/" + target_directory: test-directory + + - name: test ghcr.io with custom tag + run: | + echo "::group::pull from ghcr.io" + echo "pulling ghcr.io/$TEST_IMAGE_NAME:${{ env.EPOCH_TAG }}" + docker pull ghcr.io/$TEST_IMAGE_NAME:${{ env.EPOCH_TAG }} + echo "::endgroup::" + docker run -e REGISTRY="ghcr.io with custom tag" ghcr.io/$TEST_IMAGE_NAME:${{ env.EPOCH_TAG }} diff --git a/README.md b/README.md index 224c415..413e8c7 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ The default registry is ghcr.io. ✅ Seamless integration: simplifies container image management. -✅ Default Image Tagging: Out-of-the-box tagging with the below elements. +✅ Default Image Tagging: Out-of-the-box tagging with the below elements. The default tags can be overridden by passing in a comma-separated string of desired tags, e.g. "my-tag" or "my-tag-1,my-tag-2". Tags are generated with the [create-glueops-image-tags](https://github.com/marketplace/actions/create-glueops-image-tags) action. * `Target Reference:` Either Branch Name or Tag, depending upon the trigger context. * `Short SHA` @@ -39,7 +39,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Build and Push Container to ghcr.io - uses: GlueOps/github-actions-build-push-containers@v0.3.2 + uses: GlueOps/github-actions-build-push-containers@v0.3.4 ``` #### **Docker Hub (docker.io)** @@ -56,7 +56,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Build and Push Container to docker.io - uses: GlueOps/github-actions-build-push-containers@v0.3.2 + uses: GlueOps/github-actions-build-push-containers@v0.3.4 with: registry: "docker.io" dockerhub_username: ${{ secrets.DOCKERHUB_USERNAME }} @@ -77,7 +77,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Build and Push Container to ECR - uses: GlueOps/github-actions-build-push-containers@v0.3.2 + uses: GlueOps/github-actions-build-push-containers@v0.3.4 with: registry: ".dkr.ecr..amazonaws.com" aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }} @@ -105,7 +105,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Build and Push Container to ECR - uses: GlueOps/github-actions-build-push-containers@v0.3.2 + uses: GlueOps/github-actions-build-push-containers@v0.3.4 with: registry: ".dkr.ecr..amazonaws.com" aws_role_to_assume: ${{ secrets.AWS_ECR_ROLE_ARN }} diff --git a/action.yml b/action.yml index e4e15af..8a25e25 100644 --- a/action.yml +++ b/action.yml @@ -39,6 +39,11 @@ inputs: required: false default: "." + tags: + description: 'Comma-separate list of tags for built image. Defaults to GlueOps tags' + required: false + default: '' + # ghcr github_token: @@ -145,31 +150,16 @@ runs: ;; esac - - name: Determine ref - id: determine_ref - shell: bash - run: | - echo "::group::determine ref" - if [[ $GITHUB_REF == refs/heads/* ]]; then - echo "REF_TYPE=branch" >> $GITHUB_ENV - echo "TARGET_REF=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV - elif [[ $GITHUB_REF == refs/tags/* ]]; then - echo "REF_TYPE=tag" >> $GITHUB_ENV - echo "TARGET_REF=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV - elif [[ $GITHUB_REF == refs/pull/* ]]; then - echo "REF_TYPE=pull_request" >> $GITHUB_ENV - echo "TARGET_REF=$GITHUB_SHA" >> $GITHUB_ENV - else - echo "REF_TYPE=unknown" >> $GITHUB_ENV - fi - echo "::endgroup::" - - - name: Checkout uses: actions/checkout@v3 with: - ref: ${{ steps.determine_ref.outputs.ref_name }} + ref: '' path: ${{ inputs.target_directory }} + + - name: Create GlueOps Tags + if: inputs.tags == '' + uses: Glueops/github-actions-create-container-tags@v0.1.1 + id: create-tags - name: Build Container shell: bash @@ -181,18 +171,19 @@ runs: echo "::group::Set Tags" echo "Event payload: ${{ toJson(github.event_name) }}" - # Clean up TARGET_REF for invalid characters - TARGET_REF=${TARGET_REF////-} # Replace slashes with hyphens - TARGET_REF=${TARGET_REF//[^a-zA-Z0-9_.-]/_} # Replace invalid characters with underscores - TARGET_REF=${TARGET_REF// /_} # Replace whitespaces with underscores - TARGET_REF=${TARGET_REF,,} # Convert to lowercase - export TARGET_REF="$TARGET_REF" - export SHA="${{ github.sha }}" - export SHORT_SHA="${SHA:0:7}" + # Get Tags + TAGS="${{ inputs.tags }}" + if [[ -z "$TAGS" ]]; then + TAGS="${{ steps.create-tags.outputs.tags_csv }}" + fi - echo "Target Ref: ${TARGET_REF}" - echo "Short SHA: ${SHORT_SHA}" - echo "Long SHA: ${SHA}" + # Get Target Ref + TARGET_REF="${{ steps.create-tags.outputs.clean_target_ref}}" + if [[ -z "$TARGET_REF" ]]; then + TARGET_REF="${TAGS%%,*}" + fi + + echo "Using Tags: ${TAGS}" # convert the image name to lowercase export IMAGE_NAME=$(echo "${IMAGE_NAME}" | tr '[:upper:]' '[:lower:]') @@ -200,17 +191,22 @@ runs: export BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") export GITHUB_URL=https://github.com/${{ github.repository }} - + + IFS=',' read -ra ADDR <<< "$TAGS" + DOCKER_TAGS="" + for TAG in "${ADDR[@]}"; do + DOCKER_TAGS="$DOCKER_TAGS -t ${{ inputs.registry }}/${IMAGE_NAME}:$TAG" + done + echo "::group::Building the Docker image as ${{ inputs.registry }}/${IMAGE_NAME}:${TARGET_REF} from ${{ inputs.dockerfile }} in ${{ inputs.context }} context ..." + docker build \ - --file "${{ inputs.dockerfile }}" \ + --file "${{ inputs.context }}/${{ inputs.dockerfile }}" \ --cache-from "${{ inputs.registry }}/${IMAGE_NAME}:latest" \ --build-arg BUILDKIT_INLINE_CACHE=1 \ --build-arg BUILD_DATE="${BUILD_DATE}" \ --build-arg GITHUB_SHA="${GITHUB_SHA}" \ - -t "${{ inputs.registry }}/${IMAGE_NAME}:${TARGET_REF}" \ - -t "${{ inputs.registry }}/${IMAGE_NAME}:${SHORT_SHA}" \ - -t "${{ inputs.registry }}/${IMAGE_NAME}:${SHA}" \ + $DOCKER_TAGS \ --label "org.label-schema.build-date=${BUILD_DATE}" \ --label "org.label-schema.vcs-url=${GITHUB_URL}" \ --label "org.label-schema.vcs-ref=${GITHUB_SHA}" \ @@ -219,11 +215,13 @@ runs: --label "org.opencontainers.image.revision=${GITHUB_SHA}" \ "${{ inputs.context }}" + echo "::endgroup::" echo "::group::Inspecting the image ..." docker image ls + echo "Labels:" docker image inspect "${{ inputs.registry }}/${IMAGE_NAME}:${TARGET_REF}" | jq '.[].Config.Labels'