Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 53 additions & 22 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,89 @@ on:
workflows: ["tag-on-merge"]
types: [completed]

env:
IMAGE_NAME: planara-projects

jobs:
detect-release:
resolve-release:
if: ${{ github.event.workflow_run.conclusion == 'success' }}

runs-on: ubuntu-latest

permissions:
contents: read
pull-requests: read

outputs:
should_deploy: ${{ steps.release.outputs.should_deploy }}
tag: ${{ steps.release.outputs.tag }}
version: ${{ steps.release.outputs.version }}
sha: ${{ steps.commit.outputs.sha }}
short_sha: ${{ steps.commit.outputs.short_sha }}
sha: ${{ steps.release.outputs.sha }}
short_sha: ${{ steps.release.outputs.short_sha }}

steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
fetch-depth: 0

- name: Fetch tags
run: git fetch --force --tags

- name: Set commit tags
id: commit
run: |
SHA="${{ github.event.workflow_run.head_sha }}"
echo "sha=$SHA" >> $GITHUB_OUTPUT
echo "short_sha=${SHA::7}" >> $GITHUB_OUTPUT

- name: Detect release tag on current commit
- name: Resolve release
id: release
env:
GH_TOKEN: ${{ github.token }}
run: |
TAG="$(git tag --points-at HEAD --list 'v*' --sort=-v:refname | head -n 1)"
PR_NUMBER="${{ github.event.workflow_run.pull_requests[0].number }}"

if [ -z "$PR_NUMBER" ]; then
echo "No PR number found in workflow_run payload. Skip docker image deploy."
echo "should_deploy=false" >> $GITHUB_OUTPUT
echo "tag=" >> $GITHUB_OUTPUT
echo "version=" >> $GITHUB_OUTPUT
echo "sha=" >> $GITHUB_OUTPUT
echo "short_sha=" >> $GITHUB_OUTPUT
exit 0
fi

MERGE_SHA="$(gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}" --jq '.merge_commit_sha')"

if [ -z "$MERGE_SHA" ] || [ "$MERGE_SHA" = "null" ]; then
echo "No merge_commit_sha found for PR #$PR_NUMBER. Skip docker image deploy."
echo "should_deploy=false" >> $GITHUB_OUTPUT
echo "tag=" >> $GITHUB_OUTPUT
echo "version=" >> $GITHUB_OUTPUT
echo "sha=" >> $GITHUB_OUTPUT
echo "short_sha=" >> $GITHUB_OUTPUT
exit 0
fi

echo "PR number: $PR_NUMBER"
echo "PR merge commit sha: $MERGE_SHA"

TAG="$(git tag --points-at "$MERGE_SHA" --list 'v*' --sort=-v:refname | head -n 1)"

if [ -z "$TAG" ]; then
echo "No v* tag found on this commit. Skip docker image deploy."
echo "No v* tag found on PR merge commit $MERGE_SHA. Skip docker image deploy."
echo "should_deploy=false" >> $GITHUB_OUTPUT
echo "tag=" >> $GITHUB_OUTPUT
echo "version=" >> $GITHUB_OUTPUT
echo "sha=$MERGE_SHA" >> $GITHUB_OUTPUT
echo "short_sha=${MERGE_SHA::7}" >> $GITHUB_OUTPUT
exit 0
fi

echo "Release tag found: $TAG"

echo "should_deploy=true" >> $GITHUB_OUTPUT
echo "tag=$TAG" >> $GITHUB_OUTPUT
echo "version=${TAG#v}" >> $GITHUB_OUTPUT
echo "sha=$MERGE_SHA" >> $GITHUB_OUTPUT
echo "short_sha=${MERGE_SHA::7}" >> $GITHUB_OUTPUT

push-projects-api:
needs: detect-release
if: ${{ needs.detect-release.outputs.should_deploy == 'true' }}
push-image:
needs: resolve-release
if: ${{ needs.resolve-release.outputs.should_deploy == 'true' }}

runs-on: ubuntu-latest

Expand All @@ -67,14 +98,14 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.detect-release.outputs.sha }}
ref: ${{ needs.resolve-release.outputs.tag }}
fetch-depth: 0

- name: Set image name
id: vars
run: |
OWNER_LC=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
echo "image=ghcr.io/${OWNER_LC}/planara-projects" >> $GITHUB_OUTPUT
echo "image=ghcr.io/${OWNER_LC}/${IMAGE_NAME}" >> $GITHUB_OUTPUT

- name: Log in to GHCR
uses: docker/login-action@v3
Expand All @@ -98,6 +129,6 @@ jobs:
platforms: linux/amd64,linux/arm64
tags: |
${{ steps.vars.outputs.image }}:latest
${{ steps.vars.outputs.image }}:${{ needs.detect-release.outputs.version }}
${{ steps.vars.outputs.image }}:${{ needs.detect-release.outputs.sha }}
${{ steps.vars.outputs.image }}:sha-${{ needs.detect-release.outputs.short_sha }}
${{ steps.vars.outputs.image }}:${{ needs.resolve-release.outputs.version }}
${{ steps.vars.outputs.image }}:${{ needs.resolve-release.outputs.sha }}
${{ steps.vars.outputs.image }}:sha-${{ needs.resolve-release.outputs.short_sha }}
Loading