Skip to content

Docker Build and Push #100

Docker Build and Push

Docker Build and Push #100

name: Docker Build and Push
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
on:
schedule:
- cron: '0 0 * * *' # Runs at 00:00 UTC daily
push:
branches: [ "main" ]
workflow_dispatch:
permissions: # The permissions that the workflow has. The permissions are inherited from the job and can be customized.
contents: write
env:
IMAGE_NAME: 'mrcolorrain/grass' # The name of the image to be built and pushed
TAG: 'latest'
MAX_DELAY: 60 # Maximum delay in seconds
VERSION_FILE_PATH: '.githubworkflows.grass-current_version' # Path to the version file
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Randomized Delay
run: |
sleep $((RANDOM % ${{ env.MAX_DELAY }}))
- name: Checkout repository
uses: actions/checkout@v4
- name: Check for new version
id: version-check
run: |
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then
ARCH="amd64"
elif [ "$ARCH" = "aarch64" ]; then
ARCH="arm64"
else
echo "Unsupported architecture: $ARCH" && exit 1
fi
echo "Detected runner arch: $ARCH"
wget -q "https://chromewebstore.google.com/detail/${{ secrets.EXTENSION_ID }}" -O extension_page.html
# Extract all version numbers matching the pattern
grep -Po '(?<=\\\"version\\\": \\\")([0-9]+\.[0-9]+\.[0-9]+)(?=\\\",)' extension_page.html > versions.txt
# Sort the versions using version sort and select the highest one
LATEST_VERSION=$(sort -V versions.txt | tail -n1)
echo "Latest Version detected: $LATEST_VERSION"
# Check the current version stored in the repository
if [ -f "${{ env.VERSION_FILE_PATH }}" ]; then
echo "Current version file ${{ env.VERSION_FILE_PATH }} found"
else
echo "Cannot find current version file ${{ env.VERSION_FILE_PATH }}. Creating a new one"
mkdir -p "$(dirname "${{ env.VERSION_FILE_PATH }}")"
echo 'none' > "${{ env.VERSION_FILE_PATH }}"
fi
CURRENT_VERSION=$(cat "${{ env.VERSION_FILE_PATH }}")
echo "Current Version: $CURRENT_VERSION"
if [ "$CURRENT_VERSION" != "$LATEST_VERSION" ] || [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "New version detected: $LATEST_VERSION. Triggering build..."
echo "latest_version=$LATEST_VERSION" >> "$GITHUB_ENV"
echo "$LATEST_VERSION" > "${{ env.VERSION_FILE_PATH }}"
echo "trigger_build=true" >> "$GITHUB_OUTPUT"
else
echo "No new version detected. Skipping build"
echo "trigger_build=false" >> "$GITHUB_OUTPUT"
fi
- name: Setup QEMU
if: steps.version-check.outputs.trigger_build == 'true'
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
if: steps.version-check.outputs.trigger_build == 'true'
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
if: steps.version-check.outputs.trigger_build == 'true'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and Push Docker Image
if: steps.version-check.outputs.trigger_build == 'true'
run: |
docker buildx build \
--platform linux/amd64,linux/arm64,linux/arm/v7 \
-t ${{ env.IMAGE_NAME }}:${{ env.TAG }} \
-f grass.dockerfile \
. --push
- name: Commit and push updated version file
if: steps.version-check.outputs.trigger_build == 'true'
run: |
git config --global user.name 'GitHub Actions'
git config --global user.email '[email protected]'
git add "${{ env.VERSION_FILE_PATH }}"
git commit -m "Update version file to ${{ env.latest_version }}"
git push
- name: Create and push tag
if: steps.version-check.outputs.trigger_build == 'true'
run: |
git config --global user.name 'GitHub Actions'
git config --global user.email '[email protected]'
git tag -a "${{ env.latest_version }}" -m "Update to ${{ env.latest_version }}"
git push origin "${{ env.latest_version }}"