Skip to content

Commit

Permalink
Merge pull request #68 from dan-atack/63-production-docker-images-bui…
Browse files Browse the repository at this point in the history
…lt-externally

Updated build script to use env vars and added new Github Actions wor…
  • Loading branch information
dan-atack authored Oct 16, 2023
2 parents d7cddab + b667f5c commit 344830d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/Build_Image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Build Docker Image

# Only trigger a build when a merge/commit is made to the master branch
on:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
env:
SMARS_ENVIRONMENT: 'production'
DOMAIN_NAME: 'freesmars.com'
DOCKER_REPO: 'danatack/smars'
steps:
- uses: actions/checkout@v2
- uses: actions/docker/build-push-action@v2
with:
node-version: '14'
- run: echo "starting build"
- run: bash startBuild.sh
6 changes: 3 additions & 3 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3923,8 +3923,8 @@ Exit Criteria:
- [DONE] Docker Hub repo is set up to store the game's Docker image files
- [DONE] Image can be build, tagged and pushed from any of the local VM environments
- [DONE] Image can be pulled from Docker Hub to any of the game's AWS environments (dev, staging or production)
- Game can be deployed/updated on the cloud using an image pulled from Docker Hub
- GitHub Actions workflow is updated to generate a new Docker image whenever a PR is merged
- [DONE] Game can be deployed/updated on the cloud using an image pulled from Docker Hub
- GitHub Actions workflow is created to generate a new Docker production image whenever a PR is merged

1. From the local dev environment, build a Docker image from the master branch and try pushing it to the Docker Hub account. Write down all steps involved once the process is completed. Ensure that the local dev environment's .env file contains values for DOMAIN_NAME and SMARS_ENVIRONMENT since those will be 'baked in' to the resulting image. Tag the build as 'dev-1.1.2' and then push it to the Docker Hub repo.

Expand All @@ -3938,7 +3938,7 @@ Exit Criteria:

4. Update the docker-compose file to add the ENVIRONMENT and DOMAIN_NAME environment variables under the 'environment' heading for the backend service, since the backend will need them (not actually sure about the DOMAIN_NAME variable but let's include it too). This means that we also need to keep the creation of the .env file as part of the Terraform configure_instance template file.

### 5. Before proceeding any further, merge the changes already made in this chapter to the master branch, and then use the local build script to build a new image for the production environment. Then, log into the production EC2 instance, do a git pull to update its docker compose file, pull the latest production image, and then run `docker compose down` and `docker compose up -d` to reboot the production stack with the latest image. If successful, delete the older image and also run the builder prune command to free up space on this machine's volume.
5. Before proceeding any further, merge the changes already made in this chapter to the master branch, and then use the local build script to build a new image for the production environment. Then, log into the production EC2 instance, do a git pull to update its docker compose file, pull the latest production image, and then run `docker compose down` and `docker compose up -d` to reboot the production stack with the latest image. If successful, delete the older image and also run the builder prune command to free up space on this machine's volume.

### 4. Once the game can be demonstrated to run on an externally-built docker image, the next step will be to produce that image via GitHub Actions. Start by creating a new yaml file in the github/workflows directory called Build_Docker_Image, and copying the code from the Basic_CI file to imitate its structure.

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Once the stack has started, you should be able to play the game by going to http

SMARS_ENVIRONMENT=<environment (e.g. 'dev' 'staging' or 'prod' )>
DOMAIN_NAME=<domain name (e.g. test.freesmars.com)>
DOCKER_REPO=<repo name (e.g. danatack/smars )>

- Run the build script:
`bash startBuild.sh`
Expand Down
11 changes: 6 additions & 5 deletions startBuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
export $(cat .env | xargs)
# Read the version info from the frontend's constants file
version=$(grep "RELEASE_VERSION" ./frontend/src/constants.ts | awk -F'"' '{print $2}')
repo=${DOCKER_REPO}
if [ -z $version ]; then
echo "Version information not found. Please check that the frontend/src/constants.ts file is in the correct format and try again."
exit 1
else
echo "Tagging build as version $version-${SMARS_ENVIRONMENT}"
fi
tag=$version-${SMARS_ENVIRONMENT}
echo "Building image danatack/smars:$tag"
echo "Building image $repo:$tag"
# Build the image and tag with version number and build environment name
docker build --build-arg SMARS_ENVIRONMENT=${SMARS_ENVIRONMENT} --build-arg DOMAIN_NAME=${DOMAIN_NAME} -t danatack/smars:$version-${SMARS_ENVIRONMENT} .
docker build --build-arg SMARS_ENVIRONMENT=${SMARS_ENVIRONMENT} --build-arg DOMAIN_NAME=${DOMAIN_NAME} -t $repo:$version-${SMARS_ENVIRONMENT} .
# Apply 'latest' tag
docker tag danatack/smars:$tag danatack/smars:latest
docker tag $repo:$tag $repo:latest
# Push image with both the version tag and the 'latest' tag
docker push danatack/smars:$tag
docker push danatack/smars:latest
docker push $repo:$tag
docker push $repo:latest

0 comments on commit 344830d

Please sign in to comment.