Skip to content

Merge pull request #558 from codeforjapan/fix-debates-application-con… #322

Merge pull request #558 from codeforjapan/fix-debates-application-con…

Merge pull request #558 from codeforjapan/fix-debates-application-con… #322

Workflow file for this run

name: Deploy to ecs
on:
push:
branches:
- develop
- master
tags:
- 'v*'
env:
AWS_ROLE_ARN: arn:aws:iam::887442827229:role/GithubActions_decidim-cfj-cdk-deploy
jobs:
create-app-env:
runs-on: ubuntu-latest
timeout-minutes: 60
permissions:
actions: write
contents: read
id-token: write
outputs:
image-tag: ${{ steps.output-app-env.outputs.image-tag }}
eb-environment-name: ${{ steps.output-app-env.outputs.eb-environment-name }}
steps:
- uses: actions/checkout@v3
- name: Set env to staging
id: set-env-staging
if: endsWith(github.ref, 'heads/develop')
run: |
echo "IMAGE_TAG_PREFIX=staging" >> $GITHUB_ENV
echo "EB_ENVIRONMENT_NAME=staging" >> $GITHUB_ENV
- name: Set env to production
id: set-env-production
if: endsWith(github.ref, 'heads/master') || contains(github.ref, 'tags/v')
run: |
echo "IMAGE_TAG_PREFIX=prd-v0265" >> $GITHUB_ENV
echo "EB_ENVIRONMENT_NAME=prd-v0265" >> $GITHUB_ENV
- name: Output App Env
id: output-app-env
run: |
echo "image-tag=${IMAGE_TAG_PREFIX}-${GITHUB_SHA::8}" >> $GITHUB_OUTPUT
echo "eb-environment-name=${EB_ENVIRONMENT_NAME}" >> $GITHUB_OUTPUT
build:
runs-on: ubuntu-latest
timeout-minutes: 1200
needs: create-app-env
permissions:
actions: write
contents: read
id-token: write
steps:
- uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1-node16
with:
role-to-assume: ${{ env.AWS_ROLE_ARN }}
aws-region: ap-northeast-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to Amazon ECR
uses: docker/build-push-action@v2
if: endsWith(github.ref, 'heads/master') || endsWith(github.ref, 'heads/develop')
id: build-image
with:
push: true
tags: ${{ steps.login-ecr.outputs.registry }}/${{ secrets.AWS_ECR_REPO_NAME }}:${{ needs.create-app-env.outputs.image-tag }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
- name: Move cache
if: endsWith(github.ref, 'heads/master') || endsWith(github.ref, 'heads/develop')
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
deploy:
runs-on: ubuntu-latest
timeout-minutes: 1800
if: endsWith(github.ref, 'heads/develop') || contains(github.ref, 'tags/v')
permissions:
actions: write
contents: read
id-token: write
needs:
- create-app-env
- build
steps:
- uses: actions/checkout@v3
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1-node16
with:
role-to-assume: ${{ env.AWS_ROLE_ARN }}
aws-region: ap-northeast-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Check if ECR Image exists with tag
if: contains(github.ref, 'tags/v')
env:
IMAGE_TAG: ${{ needs.create-app-env.outputs.image-tag }}
ECR_REPOSITORY: ${{ secrets.AWS_ECR_REPO_NAME }}
run: |
EXIT_CODE=0
aws ecr describe-images --repository-name=$ECR_REPOSITORY --image-ids=imageTag=$IMAGE_TAG 2> /dev/null || EXIT_CODE=$?
if [[ $EXIT_CODE != 0 ]]; then
echo "${IMAGE_TAG} image tag not found"
exit 1
fi
- name: Checkout decidim-cfj cdk
uses: actions/checkout@v3
with:
repository: codeforjapan/decidim-cfj-cdk
path: decidim-cfj-cdk
- name: Setup Node
uses: actions/setup-node@v1
with:
node-version: '16'
- name: Cache node modules
id: cache-npm
uses: actions/cache@v3
env:
cache-name: cache-node-modules
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install dependencies
run: npm install
working-directory: decidim-cfj-cdk
- name: Install dependencies
run: npm install -g aws-cdk
working-directory: decidim-cfj-cdk
- name: cdk deploy
run: cdk -c stage=$EB_ENVIRONMENT_NAME -c tag=$IMAGE_TAG deploy --all --require-approval never
working-directory: decidim-cfj-cdk
env:
AWS_DEFAULT_REGION: 'ap-northeast-1'
EB_ENVIRONMENT_NAME: ${{ needs.create-app-env.outputs.eb-environment-name }}
IMAGE_TAG: ${{ needs.create-app-env.outputs.image-tag }}