Build and Deploy #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 and Deploy | |
on: | |
workflow_dispatch: | |
inputs: | |
environment: | |
type: choice | |
description: Select the environment | |
required: true | |
options: | |
- Dev | |
- Staging | |
- Production | |
jobs: | |
build: | |
runs-on: ubuntu-20.04 | |
environment: ${{ github.event.inputs.environment }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Install .NET Core | |
uses: actions/setup-dotnet@v4 | |
with: | |
dotnet-version: 8.0.x | |
- name: Restore & Build .Net Core solution | |
run: | | |
dotnet build source/DevSecOpsOne.Api/DevSecOpsOne.Api.csproj | |
- name: Transform Configuration Vars | |
id: set-vars | |
run: | | |
current_branch="${GITHUB_REF#refs/heads/}" | |
aws_region="ap-southeast-1" | |
environment="${{ github.event.inputs.environment }}" | |
app_setting_env="Development" | |
docker_file_path="" | |
service_name="DevSecOpsOne" | |
accessKey="${{ secrets.DEV_AWS_ACCESS_KEY }}" | |
secretKey="${{ secrets.DEV_AWS_SECRET_KEY }}" | |
echo "access_key=${accessKey}" >> $GITHUB_OUTPUT | |
echo "secret_key=${secretKey}" >> $GITHUB_OUTPUT | |
echo "aws_region=${aws_region}" >> $GITHUB_OUTPUT | |
echo "environment=${environment}" >> $GITHUB_OUTPUT | |
echo "app_setting_env=${app_setting_env}" >> $GITHUB_OUTPUT | |
echo "current_branch=${current_branch}" >> $GITHUB_OUTPUT | |
echo "service_name=${service_name}" >> $GITHUB_OUTPUT | |
- name: Setup AWS Creds | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-access-key-id: ${{ steps.set-vars.outputs.access_key }} | |
aws-secret-access-key: ${{ steps.set-vars.outputs.secret_key }} | |
aws-region: ${{ steps.set-vars.outputs.aws_region }} | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v2 | |
- name: Create ECR Repo for DevSecOpsOne-Api Image if not exists | |
id: ecr-repo-devsecopsone-api | |
uses: "trungcaot/create-ecr-repository-action@v1" | |
with: | |
region: ${{ steps.set-vars.outputs.region }} | |
aws_region: ${{ steps.set-vars.outputs.aws_region }} | |
environment: ${{ steps.set-vars.outputs.environment }} | |
service_name: "${{ steps.set-vars.outputs.service_name }}-api" | |
# Generate Docker image tags | |
- name: generate tags | |
id: tag-devsecopsone-api | |
uses: docker/metadata-action@v4 | |
with: | |
images: ${{ steps.login-ecr.outputs.registry }}/${{ steps.ecr-repo-devsecopsone-api.outputs.repository-name }} | |
tags: | | |
type=raw,value=${{ steps.set-vars.outputs.environment }} | |
type=ref,event=branch,suffix=-{{ sha }},priority=1 | |
type=ref,event=pr | |
type=ref,event=tag | |
type=raw,value=latest | |
# Build and push Docker images | |
- uses: docker/setup-buildx-action@v2 | |
- name: Build and push | |
uses: docker/build-push-action@v3 | |
with: | |
push: true | |
provenance: false | |
cache-from: type=gha | |
context: ./${{steps.set-vars.outputs.docker_file_path}} | |
cache-to: type=gha,mode=max | |
tags: ${{ steps.tag-devsecopsone-api.outputs.tags }} | |
labels: ${{ steps.tag-devsecopsone-api.outputs.labels }} | |
file: ./${{ steps.set-vars.outputs.docker_file_path }}/source/DevSecOpsOne.Api/Dockerfile | |
build-args: | | |
ENV=${{ steps.set-vars.outputs.app_setting_env }} |