fake-smtp-server build #85
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: fake-smtp-server build | |
on: | |
schedule: | |
- cron: "0 0 * * 0" | |
push: | |
branches: | |
- "**" | |
tags: | |
- "v*.*.*" | |
pull_request: | |
branches: [ "main" ] | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
timeout-minutes: 30 # Set your desired maximum timeout here | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Run curl command to get latest version | |
id: curl_output | |
run: | | |
set -x | |
response=$(curl -s "https://api.github.com/repos/sj26/mailcatcher/releases/latest") | |
# Parse and extract tag_name from the JSON response | |
tag_name=$(echo $response | jq -r '.tag_name') | |
# Remove the leading 'v' from tag_name | |
echo "latest_version=${tag_name#v}" >> $GITHUB_OUTPUT | |
- name: Display latest version | |
run: | | |
echo "Latest Version: ${{ steps.curl_output.outputs.latest_version }}" | |
- name: Read version from file | |
id: read_version | |
run: | | |
echo "version=$(cat version.txt)" >> $GITHUB_OUTPUT | |
- name: Display version from file | |
run: | | |
echo "Version from File: ${{ steps.read_version.outputs.version }}" | |
- name: Check if versions are different | |
id: check_versions | |
run: | | |
if [ "${{ steps.curl_output.outputs.latest_version }}" != "${{ steps.read_version.outputs.version }}" ]; then | |
echo "Versions are different, running Docker build..." | |
git checkout -b version-update-${{ github.run_number }} | |
echo "${{ steps.curl_output.outputs.latest_version }}" > version.txt | |
echo "build_docker=true" >> $GITHUB_OUTPUT | |
else | |
echo "Versions are the same, skipping Docker build." | |
echo "build_docker=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Commit and push changes | |
if: steps.check_versions.outputs.build_docker == 'true' | |
run: | | |
git add version.txt | |
git config --global user.email "[email protected]" | |
git config --global user.name "GitHub Actions Bot" | |
git commit -m "Update version to ${{ steps.curl_output.outputs.latest_version }}" | |
git push origin version-update-${{ github.run_number }} | |
- name: Create pull request | |
if: steps.check_versions.outputs.build_docker == 'true' | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const pr = await github.rest.pulls.create({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
title: `Update version to ${{ steps.curl_output.outputs.latest_version }}`, | |
body: 'Automated pull request to update version.', | |
head: `version-update-${context.runNumber}`, | |
base: 'main' | |
}); | |
- name: Docker meta | |
if: steps.check_versions.outputs.build_docker == 'false' | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
# list of Docker images to use as base name for tags | |
images: | | |
haravich/fake-smtp-server | |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
# generate Docker tags based on the following events/attributes | |
tags: | | |
type=schedule,pattern={{date 'YYYYMMDD'}} | |
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }} | |
type=ref,event=branch | |
type=semver,pattern={{version}} | |
type=sha,prefix=main-,enable=${{ github.ref == format('refs/heads/{0}', 'main') }} | |
- name: Set up QEMU | |
if: steps.check_versions.outputs.build_docker == 'false' | |
uses: docker/setup-qemu-action@v2 | |
- name: Set up Docker Buildx | |
if: steps.check_versions.outputs.build_docker == 'false' | |
uses: docker/setup-buildx-action@v2 | |
- name: Login to Docker Hub | |
if: github.event_name != 'pull_request' && steps.check_versions.outputs.build_docker == 'false' | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Login to GHCR | |
if: github.event_name != 'pull_request' && steps.check_versions.outputs.build_docker == 'false' | |
uses: docker/login-action@v2 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push | |
if: github.event_name != 'pull_request' && steps.check_versions.outputs.build_docker == 'false' | |
uses: docker/build-push-action@v4 | |
with: | |
platforms: linux/amd64,linux/arm64/v8 | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
build-args: VERSION=${{ steps.curl_output.outputs.latest_version }} |