Merge pull request #5 from PolkadotEducation/artur-ses #4
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: CI/CD workflow | |
on: | |
pull_request: | |
branches: | |
- develop | |
- main | |
push: | |
branches: | |
- develop | |
- main | |
permissions: | |
id-token: write | |
contents: read | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
environment: ${{ github.event.pull_request.base.ref || github.ref_name }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Setup | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 20.x | |
- name: Tests | |
run: yarn install --frozen-lockfile && yarn test | |
- name: AWS Setup | |
uses: aws-actions/configure-aws-credentials@v3 | |
with: | |
role-to-assume: ${{ secrets.AWS_OIDC_ARN }} | |
role-session-name: samplerolesession | |
aws-region: ${{ vars.AWS_REGION }} | |
- name: AWS ECR Login | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: Environment | |
run: | | |
touch .env | |
echo "NODE_ENV=${{ vars.NODE_ENV }}" >> .env | |
echo "SERVER_HOST=${{ vars.HOST }}" >> .env | |
echo "SERVER_PORT=${{ vars.PORT }}" >> .env | |
echo "JWT_SECRET=${{ vars.JWT_SECRET }}" >> .env | |
echo "MONGODB_URI=${{ vars.MONGODB_URI }}" >> .env | |
echo "AWS_SES_REGION=${{ vars.AWS_REGION }}" >> .env | |
echo "AWS_SES_ID=${{ secrets.AWS_SES_ID }}" >> .env | |
echo "AWS_SES_SECRET=${{ secrets.AWS_SES_SECRET }}" >> .env | |
echo "AWS_SES_SOURCE=${{ vars.APAWS_SES_SOURCEP_URL }}" >> .env | |
echo "APP_URL=${{ vars.APP_URL }}" >> .env | |
echo "" >> .env | |
- name: Format and Lint | |
run: yarn format && yarn lint | |
- name: Test | |
run: yarn test | |
- name: Docker Build | |
id: build-image | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
STAGE: ${{ vars.STAGE }} | |
run: docker build -t $ECR_REGISTRY/api:$STAGE . | |
- name: Docker Artifact | |
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.merged == true) | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
STAGE: ${{ vars.STAGE }} | |
run: docker save $ECR_REGISTRY/api:$STAGE -o buildArtifact.tar | |
- name: Docker Push | |
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.merged == true) | |
uses: actions/upload-artifact@v4 | |
with: | |
name: api-image | |
path: ./buildArtifact.tar | |
retention-days: 1 | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build | |
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.merged == true) | |
environment: ${{ github.event.pull_request.base.ref || github.ref_name }} | |
steps: | |
- name: Artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: api-image | |
path: ./ | |
- name: AWS Setup | |
uses: aws-actions/configure-aws-credentials@v3 | |
with: | |
role-to-assume: ${{ secrets.AWS_OIDC_ARN }} | |
role-session-name: samplerolesession | |
aws-region: ${{ vars.AWS_REGION }} | |
- name: AWS ECR Login | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: AWS ECR Push | |
id: push-image | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
STAGE: ${{ vars.STAGE }} | |
run: | | |
docker load -i buildArtifact.tar | |
docker push $ECR_REGISTRY/api:$STAGE | |
echo "image=$ECR_REGISTRY/api:$STAGE" >> $GITHUB_OUTPUT | |
- name: AWS ECR Check image | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
STAGE: ${{ vars.STAGE }} | |
run: | | |
aws ecr describe-images --repository-name api --image-ids imageTag=$STAGE --region $AWS_REGION | |
- name: AWS ECS Deploy | |
env: | |
AWS_REGION: ${{ vars.AWS_REGION }} | |
CLUSTER: ${{ vars.AWS_ECS_CLUSTER }} | |
SERVICE: ${{ vars.AWS_ECS_SERVICE }} | |
TASK: ${{ vars.AWS_ECS_TASK }} | |
run: | | |
aws ecs update-service --cluster $CLUSTER --service $SERVICE --task-definition $TASK --force-new-deployment |