Skip to content

Commit

Permalink
Merge pull request #8 from databox/tag_check
Browse files Browse the repository at this point in the history
Support non semversion tags
  • Loading branch information
slavkor authored May 23, 2024
2 parents 4e6a570 + fd3ed7e commit a6e4b6a
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 36 deletions.
59 changes: 38 additions & 21 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,35 @@ on:
required: false
type: string
default: ''

tag_check_regex:
description: 'Regex to check tag'
required: false
type: string
default: '^([0-9]+)\.([0-9]+)\.([0-9]+)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?(\\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$'
extract_semver_from_tag:
description: 'Extract semver from tag'
required: false
type: boolean
default: false
tag_extract_regex:
description: 'Regex to extract semver from tag'
required: false
type: string
default: '([^@]+)$'
jobs:
# Check tag setisfiy semantic versioning
tag_check:
uses: databox/.github/.github/workflows/tag-check.yml@master
uses: databox/.github/.github/workflows/tag-check.yml@tag_check
secrets: inherit
with:
tag: ${{ inputs.tag }}
tag_check_regex: ${{ inputs.tag_check_regex }}
extract_semver_from_tag: ${{ inputs.extract_semver_from_tag }}
tag_extract_regex: ${{ inputs.tag_extract_regex }}

# Build image for each platform
build:
name: Build ${{ inputs.tag }}-${{ matrix.platform }}
name: Build ${{ needs.tag_check.outputs.semver }}-${{ matrix.platform }}
runs-on: ubnutu-22-04-${{ matrix.platform }}-1-core-4-ram
needs:
- tag_check
Expand All @@ -70,7 +87,7 @@ jobs:
echo "skip=false" >> $GITHUB_OUTPUT
fi
- name: Build for platform ${{ matrix.platform }}
- name: Build version ${{ needs.tag_check.outputs.semver }} for platform ${{ matrix.platform }}
run: echo "skip=${{ steps.skip-build.outputs.skip }}; dont skip=${{ !fromJson(steps.skip-build.outputs.skip) }}"

- name: Checkout repository
Expand All @@ -82,7 +99,7 @@ jobs:
if: ${{ !fromJson(steps.skip-build.outputs.skip) }}
run: |
echo "ECR_REPOSITORY=$(echo ${{ github.repository }} | awk '{print tolower($0)}' | sed 's|[^/]*/||; s/_/-/g')" >> $GITHUB_ENV
echo "ECR_TAG=${{ inputs.tag }}-${{ matrix.platform }}" >> $GITHUB_ENV
echo "ECR_TAG=${{ needs.tag_check.outputs.semver }}-${{ matrix.platform }}" >> $GITHUB_ENV
- name: Configure AWS Credentials
if: ${{ !fromJson(steps.skip-build.outputs.skip) }}
Expand Down Expand Up @@ -113,7 +130,8 @@ jobs:
const defaultArgsString = `
PACKAGES_READ_TOKEN=${{ secrets.PACKAGES_READ_TOKEN }}
PACKAGES_READ_USER=${{ secrets.PACKAGES_READ_USER }}
RSA_PYTHON_MQ_LIB=${{ secrets.RSA_PYTHON_MQ_LIB }}`;
RSA_PYTHON_MQ_LIB=${{ secrets.RSA_PYTHON_MQ_LIB }}
BUILD_VERSION=${{ needs.tag_check.outputs.semver }}`;
const argsString = `${{ inputs.build_args }}` + defaultArgsString;
Expand All @@ -136,27 +154,26 @@ jobs:
path: ${{ inputs.artifacts_download_path }}
merge-multiple: true

- name: Docker build and push ${{ inputs.tag }}-${{ matrix.platform }} image
- name: Docker build and push ${{ needs.tag_check.outputs.semver }}-${{ matrix.platform }} image
if: ${{ !fromJson(steps.skip-build.outputs.skip) }}
run: |
TAG=${{ env.ECR_TAG }}
SERVICE_IMAGE_NAME="${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}"
echo ${{ steps.docker-build-args.outputs.result }}
IMAGE="${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}"
docker buildx create --name DLC_builder --use
docker buildx build ${{ steps.docker-build-args.outputs.result }} -f ${{ inputs.dockerfile }} -t ${SERVICE_IMAGE_NAME}:${TAG} --progress plain --push --provenance false .
docker buildx build ${{ steps.docker-build-args.outputs.result }} -f ${{ inputs.dockerfile }} -t ${IMAGE}:${TAG} --progress plain --push --provenance false .
- name: Save ${{ inputs.tag }}-${{ matrix.platform }} tag
- name: Save ${{ needs.tag_check.outputs.semver }}-${{ matrix.platform }} tag
if: ${{ !fromJson(steps.skip-build.outputs.skip) }}
run: |
mkdir -p ${{ runner.temp }}/${{ inputs.tag }}
touch ${{ runner.temp }}/${{ inputs.tag }}/${{ inputs.tag }}-${{ matrix.platform }}
mkdir -p ${{ runner.temp }}/${{ needs.tag_check.outputs.semver }}
touch ${{ runner.temp }}/${{ needs.tag_check.outputs.semver }}/${{ needs.tag_check.outputs.semver }}-${{ matrix.platform }}
- name: Upload ${{ inputs.tag }}-${{ matrix.platform }} tag to GitHub Actions Artifacts
- name: Upload ${{ needs.tag_check.outputs.semver }}-${{ matrix.platform }} tag to GitHub Actions Artifacts
if: ${{ !fromJson(steps.skip-build.outputs.skip) }}
uses: actions/upload-artifact@v4
with:
name: ${{ inputs.tag }}-${{ matrix.platform }}
path: ${{ runner.temp }}/${{ inputs.tag }}
name: ${{ needs.tag_check.outputs.semver }}-${{ matrix.platform }}
path: ${{ runner.temp }}/${{ needs.tag_check.outputs.semver }}
retention-days: 1
outputs:
ecr_repository: ${{ env.ECR_REPOSITORY }}
Expand All @@ -174,20 +191,20 @@ jobs:
- name: Download release tag from GitHub Actions Artifacts
uses: actions/download-artifact@v4
with:
path: ${{ runner.temp }}/${{ inputs.tag }}
pattern: ${{ inputs.tag }}-*
path: ${{ runner.temp }}/${{ needs.tag_check.outputs.semver }}
pattern: ${{ needs.tag_check.outputs.semver }}-*
merge-multiple: true

- name: Read tags
id: read
run: |
DIRECTORY_PATH=${{ runner.temp }}/${{ inputs.tag }}
DIRECTORY_PATH=${{ runner.temp }}/${{ needs.tag_check.outputs.semver }}
FILES=$(ls $DIRECTORY_PATH | tr '\n' ' ')
echo "files=$FILES" >> $GITHUB_OUTPUT
# Build and push manifest
manifest:
uses: databox/.github/.github/workflows/manifest.yml@master
uses: databox/.github/.github/workflows/manifest.yml@tag_check
secrets: inherit
needs:
- tag_check
Expand All @@ -196,6 +213,6 @@ jobs:
if: needs.tag_check.outputs.match == 'true'
with:
ecr_repository: ${{ needs.build.outputs.ecr_repository }}
tag: ${{ inputs.tag }}
tag: ${{ needs.tag_check.outputs.semver }}
additional_tags: ${{ needs.built_tags.outputs.tags }}

13 changes: 6 additions & 7 deletions .github/workflows/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ on:
required: false
type: string
default: ''

env:
ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com
jobs:
manifest:
name: Build and push manifest
runs-on: ubnutu-22-04-arm64-1-core-4-ram
timeout-minutes: 1
env:
ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
Expand Down Expand Up @@ -50,13 +49,13 @@ jobs:
}
const tags = tagsString.split(' ').filter(tag => tag.trim() !== '');
const result = tags.map(tag => ` --amend $SERVICE_IMAGE_NAME:${tag}`).join(' \\\n');
const result = tags.map(tag => ` --amend ${{ env.ECR_REGISTRY }}/${{ inputs.ecr_repository }}:${tag}`).join(' \\\n');
return result.trim();
- name: Build and push manifest
run: |
SERVICE_IMAGE_NAME="${{ env.ECR_REGISTRY }}/${{ inputs.ecr_repository }}"
docker manifest create $SERVICE_IMAGE_NAME:${{ inputs.tag }} \
IMAGE="${{ env.ECR_REGISTRY }}/${{ inputs.ecr_repository }}:${{ inputs.tag }}"
docker manifest create $IMAGE \
${{ steps.docker-additional-tags.outputs.result }}
docker manifest push $SERVICE_IMAGE_NAME:${{ github.event.release.tag_name }}
docker manifest push $IMAGE
80 changes: 72 additions & 8 deletions .github/workflows/tag-check.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,94 @@
name: Build and push image
name: Tag check
on:
workflow_dispatch:
inputs:
tag:
description: 'Tag name'
required: true
type: string
tag_check_regex:
description: 'Regex to check tag'
required: false
type: string
default: '^([0-9]+)\.([0-9]+)\.([0-9]+)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?(\\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$'
extract_semver_from_tag:
description: 'Extract semver from tag'
required: false
type: boolean
default: false
tag_extract_regex:
description: 'Regex to extract semver from tag'
required: false
type: string
default: '([^@]+)$'
runner:
description: 'Runner'
required: false
type: string
default: 'ubnutu-22-04-arm64-1-core-4-ram'
workflow_call:
inputs:
tag:
description: 'Tag name'
required: true
type: string
tag_check_regex:
description: 'Regex to check tag'
required: false
type: string
default: '^([0-9]+)\.([0-9]+)\.([0-9]+)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?(\\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$'
extract_semver_from_tag:
description: 'Extract semver from tag'
required: false
type: boolean
default: false
tag_extract_regex:
description: 'Regex to extract semver from tag'
required: false
type: string
default: '([^@]+)$'
runner:
description: 'Runner'
required: false
type: string
default: 'ubnutu-22-04-arm64-1-core-4-ram'
outputs:
match:
value: ${{ jobs.tag-filter.outputs.match }}

semver:
value: ${{ jobs.tag-filter.outputs.semver }}
jobs:
tag-filter:
name: Check tag ${{ inputs.tag }}
runs-on: ubnutu-22-04-arm64-1-core-4-ram
runs-on: ${{ inputs.runner }}
timeout-minutes: 1
outputs:
match: ${{ steps.tag-check.outputs.match }}
semver: ${{ steps.semver.outputs.semver }}
steps:
- id: tag-check
- name: Check tag is valid based on regex
id: tag-check
run: |
if [[ "${{ inputs.tag }}" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?(\\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$ ]]; then
if [[ "${{ inputs.tag }}" =~ ${{ inputs.tag_check_regex }} ]]; then
echo "match=true" >> $GITHUB_OUTPUT
echo "valid tag for release"
echo "Tag ${{ inputs.tag }} is valid!"
else
echo "match=false" >> $GITHUB_OUTPUT
echo "${{ inputs.tag }} is invalid tag for release. Tag check regex: ^([0-9]+)\.([0-9]+)\.([0-9]+)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?(\\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$"
echo "*************** ERROR ***************"
echo "Invalid tag!"
echo "Ensure tag maches regex"
echo "${{ inputs.tag_check_regex }}"
echo "*************** ERROR ***************"
exit 1
fi
fi
- name: Extract semantic version from tag
id: semver
if: ${{ steps.tag-check.outputs.match == 'true' }}
run: |
if [[ "${{ inputs.extract_semver_from_tag }}" == "false" ]]; then
semver=${{ inputs.tag }}
else
semver=$(echo ${{ inputs.tag }} | grep -Eo ${{ inputs.tag_extract_regex }})
fi
echo "semver=$semver" >> $GITHUB_OUTPUT
echo "semver=$semver"
15 changes: 15 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Tests
on:
pull_request:
types: [opened, reopened, labeled, unlabeled, edited, synchronize]
branches: [master]
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Tag check
run: |
GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} ./tests/test_tag-check_workflow.sh ${{ github.head_ref }}
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM alpine:edge
25 changes: 25 additions & 0 deletions tests/test_tag-check_workflow.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

ref=$1

#TODO:
# - Add tests for the build workflow

#authorize the github cli with GitHub token
gh auth login --with-token $GITHUB_TOKEN

#run semantic versioning test
gh workflow run "tag-check.yml" \
-f tag="1.0.0" \
-f runner="ubuntu-latest" \
-r $ref

#run non semantic versioning test
gh workflow run "tag-check.yml" \
-f tag="@databox/[email protected]" \
-f tag_check_regex="^@.+react-monorepo-benchmark.*@[0-9]+.[0-9]+.[0-9]+(-alpha.[0-9]+)?$" \
-f extract_semver_from_tag="true" \
-f tag_extract_regex="'([^@]+)$'" \
-f runner="ubuntu-latest" \
-r $ref

0 comments on commit a6e4b6a

Please sign in to comment.