Release and Publish Docker image #11
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: Build, Test, Release, and Publish | |
run-name: Release and Publish Docker image | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
test-app: | |
name: Test App | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Install Docker Compose | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y docker-compose | |
- name: Install dependencies | |
run: npm install | |
- name: Run tests | |
run: npm test | |
build-docker-prod: | |
name: Build Docker Image | |
runs-on: ubuntu-latest | |
needs: test-app | |
outputs: | |
version: ${{ steps.get_version.outputs.version }} | |
repo_owner: ${{ steps.convert_repo_owner.outputs.repo_owner }} | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Convert repository owner to lowercase | |
id: convert_repo_owner | |
run: | | |
repo_owner=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]') | |
echo "repo_owner=$repo_owner" >> $GITHUB_ENV | |
echo "::set-output name=repo_owner::$repo_owner" | |
- name: Get version from package.json | |
id: get_version | |
run: | | |
version=$(jq -r .version package.json) | |
echo "version=$version" >> $GITHUB_ENV | |
echo "::set-output name=version::$version" | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.PERSONAL_ACCESS_TOKEN }} | |
- name: Build and push Docker image with version tag | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
push: true | |
tags: | | |
ghcr.io/${{ steps.convert_repo_owner.outputs.repo_owner }}/patrigma:v${{ steps.get_version.outputs.version }} | |
ghcr.io/${{ steps.convert_repo_owner.outputs.repo_owner }}/patrigma:latest | |
labels: "version=${{ steps.get_version.outputs.version }},commit=${{ github.sha }}" | |
create_release: | |
name: Create Release | |
runs-on: ubuntu-latest | |
needs: build-docker-prod | |
outputs: | |
version: ${{ needs.build-docker-prod.outputs.version }} | |
steps: | |
- name: Check if release exists | |
id: check_release | |
run: | | |
RELEASE_URL="https://api.github.com/repos/${{ github.repository }}/releases/tags/v${{ needs.build-docker-prod.outputs.version }}" | |
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" $RELEASE_URL) | |
if [ "$RESPONSE" == "200" ]; then | |
echo "Release already exists." | |
echo "exists=true" >> $GITHUB_ENV | |
else | |
echo "Release does not exist." | |
echo "exists=false" >> $GITHUB_ENV | |
fi | |
- name: Delete existing release if it exists | |
if: env.exists == 'true' | |
run: | | |
RELEASE_ID=$(curl -s -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/releases/tags/v${{ needs.build-docker-prod.outputs.version }}" | jq -r '.id') | |
curl -s -X DELETE -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/releases/$RELEASE_ID" | |
- name: Delete existing tag if it exists | |
if: env.exists == 'true' | |
run: | | |
TAG_URL="https://api.github.com/repos/${{ github.repository }}/git/refs/tags/v${{ needs.build-docker-prod.outputs.version }}" | |
curl -s -X DELETE -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" $TAG_URL | |
- name: Create GitHub Release | |
if: env.exists == 'false' || env.exists == 'true' | |
id: create_release | |
uses: actions/create-release@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }} | |
with: | |
tag_name: "v${{ needs.build-docker-prod.outputs.version }}" | |
release_name: "Release v${{ needs.build-docker-prod.outputs.version }}" | |
draft: false | |
prerelease: false | |
body: "Docker image for this release: https://ghcr.io/${{ needs.build-docker-prod.outputs.repo_owner }}/patrigma:v${{ needs.build-docker-prod.outputs.version }}" | |
notify: | |
runs-on: ubuntu-latest | |
needs: create_release | |
steps: | |
- name: Notify Discord on success | |
if: success() | |
uses: containrrr/shoutrrr-action@v1 | |
with: | |
url: ${{ secrets.NOTIFICATION_URL }} | |
title: "Build Succeeded for Release v${{ needs.create_release.outputs.version }}" | |
message: | | |
The build for release v${{ needs.create_release.outputs.version }} was successful. | |
Commit message: ${{ github.event.head_commit.message }} | |
Commit SHA: ${{ github.sha }} | |
The Docker image has been published successfully. | |
- name: Notify Discord on failure | |
if: failure() | |
uses: containrrr/shoutrrr-action@v1 | |
with: | |
url: ${{ secrets.NOTIFICATION_URL }} | |
title: "Build Failed for Release v${{ needs.create_release.outputs.version }}" | |
message: | | |
The build for release v${{ needs.create_release.outputs.version }} has failed. | |
Commit message: ${{ github.event.head_commit.message }} | |
Commit SHA: ${{ github.sha }} | |
Check the build logs for details. |