diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..cfb28bc --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +API_KEY= +CORS_ALLOWED_ORIGINS= +ENV= \ No newline at end of file diff --git a/.github/workflows/azure_webapp.yml b/.github/workflows/azure_webapp.yml new file mode 100644 index 0000000..0000b7d --- /dev/null +++ b/.github/workflows/azure_webapp.yml @@ -0,0 +1,30 @@ +name: Azure Webapp deploy + +on: + pull_request: + branches: + - develop + push: + branches: + - develop + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - uses: azure/docker-login@v1 + with: +# login-server: mycontainer.azurecr.io + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_PASSWORD }} + - run: | + docker build . -t mbuchoff/hackathon_230909:${{ github.sha }} + docker push mbuchoff/hackathon_230909:${{ github.sha }} + + - uses: azure/webapps-deploy@v2 + with: + app-name: 'app-hackathon' + publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }} + images: 'mbuchoff/hackathon_230909:${{ github.sha }}' \ No newline at end of file diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index b0ae42c..3486d70 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -1,14 +1,6 @@ name: Build, Test and Deploy backend -on: - # opening a pull request to master and develop branch will be a trigger - pull_request: - branches: - - develop - # any code pushed to master and develop branch will also be a trigger - push: - branches: - - develop +on: workflow_dispatch jobs: build-test: @@ -40,5 +32,4 @@ jobs: key: ${{ secrets.SSH_PRIVATE_KEY }} passphrase: ${{ secrets.SSH_PASSPHRASE }} script: | - cd ~/.scripts ./docker-ec2-deploy.sh \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 6d0c002..04ff343 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,10 +2,9 @@ version: "3" services: hackathon-api-go: - build: - context: . - dockerfile: Dockerfile + container_name: hackathon-api-go + build: . + restart: always + # env_file: .env ports: - "9999:9999" - volumes: - - .:/app diff --git a/docker-ec2-deploy.sh b/docker-ec2-deploy.sh new file mode 100644 index 0000000..5ce802d --- /dev/null +++ b/docker-ec2-deploy.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +TARGET='develop' + +cd ~/api || exit + +ACTION_COLOR='\033[1;90m' +NO_COLOR='\033[0m' + +echo -e ${ACTION_COLOR} Checking if we are on the target branch +BRANCH=$(git rev-parse --abbrev-ref HEAD) +if [ "$BRANCH" != ${TARGET} ] +then + exit 0 +fi + +# Checking if the repository is up to date. + +git fetch +HEAD_HASH=$(git rev-parse HEAD) +UPSTREAM_HASH=$(git rev-parse ${TARGET}@{upstream}) + +if [ "$HEAD_HASH" == "$UPSTREAM_HASH" ] +then + echo -e "${FINISHED}"The current branch is up to date with origin/${TARGET}."${NO_COLOR}" + exit 0 +fi + +# If there are new changes, we pull these changes. + +git pull origin develop; + +# We can now build and start the containers + +docker compose up -d --build + +exit 0; \ No newline at end of file