Merge pull request #29 from xeptagondev/main #2
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: Carbon Transparency Demo Deployment | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- "main" | |
paths: | |
- backend/** | |
- web/** | |
- .github/workflows/deployment* | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
AWS_DEFAULT_REGION: us-east-1 | |
jobs: | |
changes: | |
name: Carbon Transparency Deploy Pre | |
runs-on: ubuntu-latest | |
outputs: | |
backend-changes: ${{ steps.changes.outputs.backend-changes }} | |
workflows-changes: ${{ steps.changes.outputs.workflows-changes }} | |
frontend-changes: ${{ steps.changes.outputs.frontend-changes }} | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Use Node.js ${{ matrix.node-version }} | |
uses: actions/setup-node@v3 | |
with: | |
node-version: ${{ matrix.node-version }} | |
- name: Determine changed services | |
id: changes | |
run: | | |
CHANGED_FILES=$(git diff --name-only HEAD^ HEAD) | |
if echo "$CHANGED_FILES" | grep -q "backend/"; then | |
echo "Backend changes detected." | |
echo "backend-changes=true" >> $GITHUB_OUTPUT | |
else | |
echo "No Backend changes detected." | |
echo "backend-changes=false" >> $GITHUB_OUTPUT | |
fi | |
if echo echo "$CHANGED_FILES" | grep -q ".github/workflows/"; then | |
echo "Workflow changes detected." | |
echo "workflows-changes=true" >> $GITHUB_OUTPUT | |
else | |
echo "No Workflow changes detected." | |
echo "workflows-changes=false" >> $GITHUB_OUTPUT | |
fi | |
if echo "$CHANGED_FILES" | grep -q "web/"; then | |
echo "Frontend changes detected." | |
echo "frontend-changes=true" >> $GITHUB_OUTPUT | |
else | |
echo "No Frontend changes detected." | |
echo "frontend-changes=false" >> $GITHUB_OUTPUT | |
fi | |
backend-deploy: | |
needs: changes | |
if: needs.changes.outputs.backend-changes == 'true' || needs.changes.outputs.workflows-changes == 'true' | |
name: Carbon Registry Backend Deploy | |
runs-on: ubuntu-latest | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }}-'backend' | |
cancel-in-progress: true | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Use Node.js ${{ matrix.node-version }} | |
uses: actions/setup-node@v3 | |
with: | |
node-version: ${{ matrix.node-version }} | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: us-east-1 | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: Build, tag, and push the backend images to Amazon ECR | |
id: build-backend-image | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
ECR_REPOSITORY: transparency-services | |
IMAGE_TAG: ${{ github.head_ref || github.ref_name }} | |
run: | | |
# Build a docker container and push it to ECR | |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f backend/services/Dockerfile . | |
echo "Pushing image to ECR..." | |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" | |
- name: Deploy backend images to Amazon EC2 | |
id: deploy-backend | |
env: | |
PRIVATE_KEY: ${{ secrets.AWS_SSH_KEY_PRIVATE_DEMO }} | |
HOSTNAME: ${{secrets.HOST_IP_DEMO }} | |
USER_NAME: ec2-user | |
run: | | |
echo "$PRIVATE_KEY" > private_key && chmod 600 private_key | |
ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} ' | |
repos/carbon-transparency/prod_backend_deploy.sh ' | |
frontend-deploy: | |
needs: changes | |
if: needs.changes.outputs.frontend-changes == 'true' || needs.changes.outputs.workflows-changes == 'true' | |
name: Carbon Registry Frontend Deploy | |
runs-on: ubuntu-latest | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }}-'web' | |
cancel-in-progress: true | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Use Node.js ${{ matrix.node-version }} | |
uses: actions/setup-node@v3 | |
with: | |
node-version: ${{ matrix.node-version }} | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: us-east-1 | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: Build, tag, and push the frontend images to Amazon ECR | |
id: build-frontend-image | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
ECR_REPOSITORY: transparency-web | |
IMAGE_TAG: ${{ github.head_ref || github.ref_name }} | |
run: | | |
# Build a docker container and push it to ECR | |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f web/Dockerfile . --build-arg PORT=3030 --build-arg REACT_APP_BACKEND=http://localhost:9000 --build-arg REACT_APP_STAT_URL=http://localhost:9100 --build-arg COUNTRY_NAME="CountryX" --build-arg COUNTRY_FLAG_URL="https://carbon-common-dev.s3.amazonaws.com/flag.png" --build-arg COUNTRY_CODE="NG" --build-arg REACT_APP_MAP_TYPE="Mapbox" --build-arg REACT_APP_GOVERNMENT_MINISTRY:"Ministry Of Environment" --build-arg REACT_APP_MAPBOXGL_ACCESS_TOKEN=${{ secrets.MAPBOXGL_ACCESS_TOKEN }} | |
echo "Pushing image to ECR..." | |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" | |
- name: Deploy frontend images to Amazon EC2 | |
id: deploy-frontend | |
env: | |
PRIVATE_KEY: ${{ secrets.AWS_SSH_KEY_PRIVATE_DEMO }} | |
HOSTNAME: ${{secrets.HOST_IP_DEMO }} | |
USER_NAME: ec2-user | |
run: | | |
echo "$PRIVATE_KEY" > private_key && chmod 600 private_key | |
ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} ' | |
repos/carbon-transparency/prod_frontend_deploy.sh ' |