Build, Test, & Deploy for Dev #162
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 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 | |
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 7 digit 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\n" >> $GITHUB_STEP_SUMMARY | |
echo "## β οΈ Check that you are using the tag ${{ env.SHA_SHORT }} when/if creating a new tag for QA deployment β οΈ\n" >> $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" | |
echo "# Dev Deployment was a Success β " >> $GITHUB_STEP_SUMMARY | |
# 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" | |
- name: Retag and push Docker Image with Release Name | |
run: | | |
docker pull kennyd3d/${{ env.ARTIFACT_NAME }}:${{ env.SHA_SHORT }} | |
echo "Retagging image tag to: ${{ github.event.release.name }}" | |
echo "Artifact Name: ${{ env.ARTIFACT_NAME }}" | |
echo "Short SHA: ${{ env.SHA_SHORT }}" | |
echo "Listing Docker images..." | |
docker images | |
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 }}" | |
- name: Give possible failure hint | |
if: failure() | |
run: | | |
echo "# β QA Failed to Deploy: Did you make sure you tagged the same commit that merged to Dev? π€" >> $GITHUB_STEP_SUMMARY | |
- name: Run success message on successful deployment | |
if: success() | |
run: | | |
echo "# π Successfully deployed ${{ github.event.release.name }} to QA β " >> $GITHUB_STEP_SUMMARY | |
# 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 exists in remote repo | |
run: | | |
IMAGE=kennyd3d/${{ env.ARTIFACT_NAME }}:${{ github.event.release.name }} | |
if docker manifest inspect $IMAGE > /dev/null 2>&1; then | |
echo '# π Successfully Deployed to Stable' >> $GITHUB_STEP_SUMMARY | |
else | |
echo '# β Failed Stable Deployment: $IMAGE 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 }}" | |
- name: Give possible failure hint | |
if: failure() | |
run: | | |
echo "## Stable Failed to Deploy: Did you type the correct release name? π€" >> $GITHUB_STEP_SUMMARY | |
- name: Run success message on successful deployment | |
if: success() | |
run: | | |
echo "# π Successfully deployed ${{ github.event.release.name }} to Stable β " >> $GITHUB_STEP_SUMMARY |