Skip to content

Update hw25-ci-cd.yml #27

Update hw25-ci-cd.yml

Update hw25-ci-cd.yml #27

Workflow file for this run

name: workflow.yml
on:
push:
branches:
- 'hw25-ci-cd/**' # Test purpose
jobs:
version:
name: Version
runs-on: ubuntu-latest
outputs:
fullSemVer: ${{ steps.version.outputs.semVer }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install GitVersion
uses: gittools/actions/gitversion/setup@v1.1.1
with:
versionSpec: '5.x'
- name: Determine Version
id: version
uses: gittools/actions/gitversion/execute@v1.1.1
unit-tests:
name: Unit Tests
if: false # Remove this line to enable the job
needs:
- version
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.x'
- name: Restore Dependencies
working-directory: './hw25-ci-cd/src'
run: dotnet restore
- name: Run Unit Tests
working-directory: './hw25-ci-cd/src'
run: dotnet test --filter FullyQualifiedName\!~IntegrationTests
integration-tests:
name: Integration Tests
if: false # Remove this line to enable the job
needs:
- version
- unit-tests
runs-on: ubuntu-latest
services:
mongo:
image: mongo:7.0.11
ports:
- 27017:27017
options: >-
--health-cmd "echo 'db.runCommand("ping").ok' | mongosh --quiet"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.x'
- name: Restore Dependencies
working-directory: './hw25-ci-cd/src'
run: dotnet restore
- name: Run Integration Tests
working-directory: './hw25-ci-cd/src'
run: dotnet test --filter FullyQualifiedName\!~UnitTests
build-and-push-docker-image:
name: Build and Push Docker Image
needs:
- version
# - unit-tests
# - integration-tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build Docker Image
env:
Version: ${{ needs.version.outputs.fullSemVer }}
working-directory: './hw25-ci-cd/src/webapi'
run: docker build --build-arg APP_VERSION=${{ env.Version }} -t hw25-webapi:${{ env.Version }} .
- name: Tag Docker Image
env:
Version: ${{ needs.version.outputs.fullSemVer }}
run: docker tag hw25-webapi:${{ env.Version }} ${{ secrets.DOCKERHUB_USERNAME }}/hw25-webapi:${{ env.Version }}
- name: Push Docker Image
env:
Version: ${{ needs.version.outputs.fullSemVer }}
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/hw25-webapi:${{ env.Version }}
deploy-to-staging:
name: Deploy to Staging
needs:
- build-and-push-docker-image
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Deploy to Staging
env:
Version: ${{ needs.version.outputs.fullSemVer }}
PRIVATE_KEY: ${{ secrets.AWS_PRIVATE_KEY }}
HOSTNAME: ${{secrets.AWS_HOST}}
USERNAME: ${{secrets.AWS_USERNAME}}
run: |
echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
ssh -o StrictHostKeyChecking=no -i private_key ${USERNAME}@${HOSTNAME} '
docker stop hw25-webapi || true
docker rm hw25-webapi || true
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/hw25-webapi:${Version}
docker run -d --name hw25-webapi -p 8080:8080 ${{ secrets.DOCKERHUB_USERNAME }}/hw25-webapi:${Version}
'