Changed host #33
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, Test, and Deploy Node.js Application to AWS | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
env: | |
AWS_REGION: eu-north-1 # Specifies the AWS region for deployments | |
ECR_REPOSITORY: post-management-microservice # Static repository name | |
permissions: | |
id-token: write # Allows GitHub Actions to interact with AWS via OIDC | |
contents: read # Allows GitHub Actions to checkout the repository | |
jobs: | |
build-and-test: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
node-version: [20.x] # Define Node.js version to test across | |
env: # Define environment variables for the build and test job | |
PORT: ${{ secrets.PORT }} | |
URI: ${{ secrets.URI }} | |
SECRET: ${{ secrets.SECRET }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up Node.js | |
uses: actions/setup-node@v2 | |
with: | |
node-version: ${{ matrix.node-version }} | |
cache: 'npm' | |
- name: Install dependencies | |
run: npm ci | |
- name: Run tests | |
run: npm test # Tests will use the env variables defined above | |
deploy: | |
needs: build-and-test | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v2 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ env.AWS_REGION }} | |
- name: Login to AWS ECR | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: Build, tag, and push image to Amazon ECR | |
env: | |
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }} | |
IMAGE_TAG: latest | |
PORT: ${{ secrets.PORT }} | |
URI: ${{ secrets.URI }} | |
SECRET: ${{ secrets.SECRET }} | |
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} | |
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} | |
run: | | |
docker build --build-arg PORT=$PORT --build-arg URI=$URI --build-arg SECRET=$SECRET --build-arg DOCKER_USERNAME=$DOCKER_USERNAME --build-arg DOCKER_PASSWORD=$DOCKER_PASSWORD -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
- name: Update EC2 instance | |
env: | |
ECR_REGISTRY: 992382603646.dkr.ecr.eu-north-1.amazonaws.com | |
ECR_REPOSITORY: post-management-microservice | |
IMAGE_TAG: latest | |
uses: appleboy/ssh-action@master | |
with: | |
host: ec2-13-53-216-66.eu-north-1.compute.amazonaws.com | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
aws ecr get-login-password --region eu-north-1 | docker login --username AWS --password-stdin 992382603646.dkr.ecr.eu-north-1.amazonaws.com | |
docker pull 992382603646.dkr.ecr.eu-north-1.amazonaws.com/post-management-microservice:latest | |
if docker inspect my-container &> /dev/null; then | |
if [ $(docker inspect -f '{{.State.Running}}' my-container) = "true" ]; then | |
docker stop my-container | |
fi | |
docker rm my-container | |
fi | |
docker run -d --name my-container -p 3000:3000 992382603646.dkr.ecr.eu-north-1.amazonaws.com/post-management-microservice:latest | |