Skip to content

Fix retagging issue #138

Fix retagging issue

Fix retagging issue #138

Workflow file for this run

name: Build and Deploy for Dev, QA, and Stable
on:
workflow_dispatch:
inputs:
RELEASE:
required: false
type: string
description: "The name of the release you want to deploy to stable."
pull_request:
types: [opened, reopened, synchronize]
branches:
- dev
pull_request_target:
types: [closed]
branches:
- dev
push:
tags:
- "*"
release:
types: [created]
env:
ARTIFACT_NAME: actions-demo
jobs:
build_test_and_deploy_dev:
runs-on: ubuntu-latest
if: |
github.event_name == 'pull_request' && github.base_ref == 'dev' ||
(github.event_name == 'pull_request_target' && github.event.action == 'closed' && github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'dev')
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Get Short SHA
run: |
echo "SHA_SHORT=$(echo $GITHUB_SHA | cut -c1-7)" >> $GITHUB_ENV
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Run tests
run: |
echo "Fake running some test"
- name: Build Docker Image
uses: docker/build-push-action@v6
with:
context: .
push: false
tags: |
kennyd3d/${{ env.ARTIFACT_NAME }}:${{ env.SHA_SHORT }}
- name: Push Docker Image if merged
if: ${{ github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'dev' }}
run: |
docker push kennyd3d/${{ env.ARTIFACT_NAME }}:${{ env.SHA_SHORT }}
echo "Docker image pushed with tag ${{ env.SHA_SHORT }}"
echo "# 🎊 Successfully pushed the tag ${{ env.SHA_SHORT }} to Artifactory" >> $GITHUB_STEP_SUMMARY
- name: Deploy to k8
if: ${{ github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'dev' }}
run: |
echo "Pretending to deploy to k8"
# Build test and deploy to qa namespace when a release tag is created
build_test_and_deploy_qa:
runs-on: ubuntu-latest
if: github.event_name == 'release' && github.event.action == 'created'
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Get 7-digit SHA from the release
run: |
echo "Commit SHA: ${GITHUB_SHA::7}"
echo "SHA_SHORT=${GITHUB_SHA::7}" >> $GITHUB_ENV
- name: Check if release tag's SHA is in artifactory
run: |
TAG_NAME=${GITHUB_REF#refs/tags/}
SHA_SHORT=$(git rev-parse --short=7 ${TAG_NAME})
echo "SHA_SHORT=${SHA_SHORT}" >> $GITHUB_ENV
echo "$SHA_SHORT"
if ! docker pull kennyd3d/actions-demo:$SHA_SHORT; then
echo "❌ Failed to pull Docker image kennyd3d/actions-demo:${SHA_SHORT}" >> $GITHUB_STEP_SUMMARY
exit 1
fi
- name: Retag and push Docker Image with Release Name
run: |
echo "Retagging image tag to: ${{ github.event.release.name }}"
echo "Artifact Name: ${{ env.ARTIFACT_NAME }}"
echo "Short SHA: ${{ env.SHA_SHORT }}"
echo "Tagging image..."
docker tag kennyd3d/${{ env.ARTIFACT_NAME }}:${{ env.SHA_SHORT }} kennyd3d/${{ env.ARTIFACT_NAME }}:${{ github.event.release.name }}
echo "Listing Docker images..."
docker images
echo "Pushing image..."
docker push kennyd3d/${{ env.ARTIFACT_NAME }}:${{ github.event.release.name }}
- name: Deploy to Stable k8
run: |
echo "Test to deploy to Stable k8 using image kennyd3d/${{ env.ARTIFACT_NAME }}:${{ github.event.release.name }}"
# Manually deploy the release given the release name
deploy_stable:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'workflow_dispatch' }}
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Check if release is in artifactory
run: |
if docker manifest inspect ${{ github.event.release.name }} > /dev/null 2>&1; then
echo echo '# 🎊 Successfully Deployed to Stable' >> $GITHUB_STEP_SUMMARY
else
echo echo '# ❌ Failed Stable Deployment: Release not found in artifactory' >> $GITHUB_STEP_SUMMARY
exit 1
fi
- name: Deploy to Stable k8
run: |
echo "Test to deploy to Stable k8 using image kennyd3d/${{ env.ARTIFACT_NAME }}:${{ github.event.release.name }}"