From f69282f9d105bb7b60ef338b88e065b414174217 Mon Sep 17 00:00:00 2001 From: Brian Clark Date: Tue, 8 Oct 2024 11:51:17 -0400 Subject: [PATCH 1/2] Fix up the compose file - Rename the container images that are built from the services from `signal_documentation-*` to `signal-discovery-*. - Rename the DB container from `signal_documentation-db` to `signal-discovery-db`. This isn't strictly necessary, but it is more consistent. - Some minor comment updates. --- docker-compose.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 480970a..3e11bbc 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,7 +1,7 @@ services: db: image: mysql:latest - container_name: signal_documentation-db + container_name: signal-discovery-db restart: always env_file: - ./.env @@ -16,15 +16,15 @@ services: - "3306:3306" # Production service - "service", "image", and "container_name" should all contain the same - # reference, based on the name of the service. + # reference based on the name of the service. sdwebapp: - image: ${REGISTRY}signal_documentation-sdwebapp${TAG} + image: ${REGISTRY}signal-discovery-sdwebapp${TAG} build: context: . env_file: - ./.env - container_name: signal_documentation-sdwebapp + container_name: signal-discovery-sdwebapp restart: on-failure command: sh -c "python3 /usr/src/signal_documentation/src/manage.py migrate --noinput && @@ -41,11 +41,11 @@ services: # Production service - "service", "image", and "container_name" should all contain the same # reference, based on the name of the service. sdnginx: - image: ${REGISTRY}signal_documentation-sdnginx${TAG} + image: ${REGISTRY}signal-discovery-sdnginx${TAG} build: ./nginx env_file: - ./.env - container_name: signal_documentation-sdnginx + container_name: signal-discovery-sdnginx restart: on-failure volumes: - ./src/staticfiles:/staticfiles From ce30164f479777c1e0a41619673566ca22b5cdfe Mon Sep 17 00:00:00 2001 From: Brian Clark Date: Tue, 8 Oct 2024 11:55:06 -0400 Subject: [PATCH 2/2] Add basics for the CI/CD rig --- .ci.env | 22 +++++++++ .github/workflows/build-and-deploy.yaml | 60 +++++++++++++++++++++++++ .gitignore | 14 ++++++ 3 files changed, 96 insertions(+) create mode 100644 .ci.env create mode 100644 .github/workflows/build-and-deploy.yaml create mode 100644 .gitignore diff --git a/.ci.env b/.ci.env new file mode 100644 index 0000000..9648744 --- /dev/null +++ b/.ci.env @@ -0,0 +1,22 @@ +# This file is used to allow CI to start the compose services. It will typcially +# not need to be modified. + +MYSQL_DATABASE=mysql_database +MYSQL_USER=mysql_user +MYSQL_PASSWORD=mysql_password +MYSQL_PORT=3306 +MYSQL_ROOT_PASSWORD=test123! +MYSQL_HOST=db + +ALLOWED_HOSTS='127.0.0.1,localhost' +CORS_ORIGIN_WHITELIST='http://127.0.0.1:3000,http://localhost:3000' +CSRF_TRUSTED_ORIGINS='http://127.0.0.1:8000,http://localhost:8000' + +SECRET_KEY='secret_key' +DEBUG='True' + +# Add the following to your local .env file. They will be used in the CI process +# and you can largely forget about them, but including them in your .env file +# will act like a safe default and help suppress warnings. +REGISTRY="" +TAG="" diff --git a/.github/workflows/build-and-deploy.yaml b/.github/workflows/build-and-deploy.yaml new file mode 100644 index 0000000..1dd7f6a --- /dev/null +++ b/.github/workflows/build-and-deploy.yaml @@ -0,0 +1,60 @@ +name: 'Build and deploy application containers' +on: + push: +jobs: + build-tag-push-deploy: + runs-on: ubuntu-latest + # CI/CD will run on these branches + if: > + github.ref == 'refs/heads/main' || + github.ref == 'refs/heads/staging' || + github.ref == 'refs/heads/development' + + strategy: + matrix: + # Specify the docker-compose services to build images from. These should match the service + # names in the docker-compose.yml file. + service: [sdwebapp, sdnginx] + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Login to GitHub container registry + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: cmu-delphi-deploy-machine + password: ${{ secrets.CMU_DELPHI_DEPLOY_MACHINE_PAT }} + - name: Create container image tags + id: image-tag + run: | + baseRef="${GITHUB_REF#*/}" + baseRef="${baseRef#*/}" + case "${baseRef}" in + main) + image_tag="latest" + ;; + *) + image_tag="${baseRef//\//_}" # replace `/` with `_` in branch name + ;; + esac + echo "IMAGE_TAG=${image_tag}" >> $GITHUB_OUTPUT + - name: Copy env file + run: | + cp ./.ci.env ./.env + - name: Set up docker-compose + uses: ndeloof/install-compose-action@v0.0.1 + - name: docker-compose build --push + run: | + docker-compose build --push ${{ matrix.service }} + env: + TAG: ":${{ steps.image-tag.outputs.IMAGE_TAG }}" + REGISTRY: "ghcr.io/${{ github.repository_owner }}/" + - name: docker-compose down + run: | + docker-compose down + - name: Trigger smee.io webhook to pull new container images + run: | + curl -H "Authorization: Bearer ${{ secrets.DELPHI_DEPLOY_WEBHOOK_TOKEN }}" \ + -X POST ${{ secrets.DELPHI_DEPLOY_WEBHOOK_URL }} \ + -H "Content-Type: application/x-www-form-urlencoded" \ + -d "repository=ghcr.io/${{ github.repository }}-${{ matrix.service }}&tag=${{ steps.image-tag.outputs.IMAGE_TAG }}" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf30e27 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +.env +.secrets +/.vscode/ +/src/media/ +/src/staticfiles/ +*log +*.mo +*.pyc +htmlcov/ +.coverage +.coverage.* +coverage.xml +*.cover +celerybeat-schedule.*