Add Lab 22 and Lab 23 #1
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: CICD Workflow | ||
on: | ||
push: | ||
branches: | ||
- feature/* # CI for feature branches | ||
- develop # CI/CD for develop branch | ||
- lab22/entireCICDGithub | ||
workflow_dispatch: | ||
inputs: | ||
environment: | ||
description: 'Deployment environment (qa, staging, prod). Note: dev environment is auto-deployed.' | ||
required: true | ||
type: choice | ||
options: | ||
- qa | ||
- staging | ||
- prod | ||
image_version: | ||
description: 'Docker image version to deploy (e.g., dev-latest, qa-latest, release-YYYYMMDD)' | ||
required: true | ||
type: string | ||
env: | ||
DOCKER_IMAGE_NAME: devopsdaydayup-docker-image # Docker image name | ||
REGISTRY: ghcr.io/chance2021 # GitHub Container Registry URL | ||
jobs: | ||
# 1. CI for Feature Branch | ||
feature-ci: | ||
if: github.ref_name startsWith('feature/') | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
- name: Set up Java | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: '17' | ||
- name: Run Unit Tests with JUnit | ||
run: | | ||
mkdir -p test-results | ||
java -cp ./build/classes/java/test:./build/libs/* org.junit.runner.JUnitCore com.example.MyTestSuite | ||
- name: Publish JUnit Test Results | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: junit-test-results | ||
path: test-results | ||
- name: Build Docker Image | ||
run: docker build -t ${{ env.REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}:${{ github.sha }} . | ||
# 2. CI/CD for Develop Branch | ||
develop-cicd: | ||
if: github.ref_name == 'develop' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
- name: Set up Java | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: '17' | ||
- name: Run Unit Tests with JUnit | ||
run: | | ||
mkdir -p test-results | ||
java -cp ./build/classes/java/test:./build/libs/* org.junit.runner.JUnitCore com.example.MyTestSuite | ||
- name: Publish JUnit Test Results | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: junit-test-results | ||
path: test-results | ||
- name: Build Docker Image | ||
run: docker build -t ${{ env.REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}:dev-latest . | ||
- name: Push Docker Image to GitHub Package | ||
run: | | ||
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ${{ env.REGISTRY }} -u ${{ github.actor }} --password-stdin | ||
docker push ${{ env.REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}:dev-latest | ||
# 3. Deployment Workflow | ||
deploy: | ||
if: ${{ github.event_name == 'workflow_dispatch' }} | ||
runs-on: ubuntu-latest | ||
environment: | ||
name: ${{ github.event.inputs.environment }} | ||
url: ${{ github.event.inputs.environment == 'qa' && 'https://qa.example.com' || github.event.inputs.environment == 'staging' && 'https://staging.example.com' || github.event.inputs.environment == 'prod' && 'https://prod.example.com' }} | ||
protection_rules: | ||
- required_approvers: ${{ github.event.inputs.environment == 'qa' && 'qa_deployer' || github.event.inputs.environment == 'staging' && 'staging_deployer' || github.event.inputs.environment == 'prod' && 'prod_deployer' }} | ||
steps: | ||
- name: Pull Docker Image | ||
run: | | ||
docker pull ${{ env.REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}:${{ github.event.inputs.image_version }} | ||
docker tag ${{ env.REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}:${{ github.event.inputs.image_version }} ${{ env.REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}:${{ github.event.inputs.environment }}-latest | ||
- name: Push Tagged Image | ||
run: | | ||
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ${{ env.REGISTRY }} -u ${{ github.actor }} --password-stdin | ||
docker push ${{ env.REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}:${{ github.event.inputs.environment }}-latest | ||
- name: Deploy to ${{ github.event.inputs.environment }} Environment | ||
run: | | ||
az functionapp config container set \ | ||
--name ${{ secrets[github.event.inputs.environment | upper | append("_FUNCTION_APP_NAME")] }} \ | ||
--resource-group ${{ secrets[github.event.inputs.environment | upper | append("_RESOURCE_GROUP")] }} \ | ||
--docker-custom-image-name ${{ env.REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}:${{ github.event.inputs.environment }}-latest |