Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
feat: use tags generator from marketplace action (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandoataoldotcom authored Nov 5, 2023
1 parent 9803268 commit 89a7aa5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 42 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test-action-on-pr-and-schedule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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)**
Expand All @@ -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 }}
Expand All @@ -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: "<aws-account-id>.dkr.ecr.<aws-region>.amazonaws.com"
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
Expand Down Expand Up @@ -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: "<aws-account-id>.dkr.ecr.<aws-region>.amazonaws.com"
aws_role_to_assume: ${{ secrets.AWS_ECR_ROLE_ARN }}
Expand Down
72 changes: 35 additions & 37 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -181,36 +171,42 @@ 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:]')
echo "::endgroup::"
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}" \
Expand All @@ -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'
Expand Down

0 comments on commit 89a7aa5

Please sign in to comment.