Docker Build and Push #146
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/speedshare' # The name of the image to be built and pushed | |
TAG: 'latest' | |
MAX_DELAY: 60 # Maximum delay in seconds | |
VERSION_FILE_PATH: '.githubworkflows.current_version' # Path to the version file | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Randomized Delay | |
run: | | |
sleep $((RANDOM % $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://api.speedshare.app/download/linux/cli/$ARCH -O SpeedShareCLI | |
chmod +x SpeedShareCLI | |
./SpeedShareCLI version > latest_version.txt | |
LATEST_VERSION=$(cat latest_version.txt | awk -F': ' '/[Vv]ersion:/ {gsub(/^ +| +$|│/,"",$2); print $2}' | sed 's/\x1b\[[0-9;]*m//g') | |
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 "Can not 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" ]; 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_2 }} | |
password: ${{ secrets.DOCKER_PASSWORD_2 }} | |
- name: Build and Push Docker Image | |
if: steps.version-check.outputs.trigger_build == 'true' | |
run: | | |
docker buildx build \ | |
--platform linux/amd64,linux/arm64 \ | |
-t ${{ env.IMAGE_NAME}}:${{ env.TAG }} \ | |
--push . | |
- name: Commit and push updates version file | |
if: steps.version-check.outputs.trigger_build == 'true' | |
run: | | |
git config --global user.name 'GitHub Actions' | |
git config --global user.email 'mrcolorr@users.noreply.github.com' | |
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 'mrcolorr@users.noreply.github.com' | |
git tag -a ${{ env.latest_version }} -m "Update to ${{ env.latest_version }}" | |
git push origin ${{ env.latest_version }} | |