From 5503216ad4a0230ac533042e4a69806590fc2a5a Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 3 Feb 2021 17:08:06 +0100 Subject: [PATCH] - first steps towards docker image deployment & github autotagging --- .github/workflows/publish.yml | 244 ++++++++++++++++++++++++++++++++++ backend/Dockerfile | 5 +- docker-compose.yml | 4 +- scripts/docker_push.sh | 2 +- 4 files changed, 249 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 000000000..030a9feef --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,244 @@ +name: ocelot.social publish CI + +on: + push: + branches: + - master + +jobs: + ############################################################################## + # JOB: PREPARE ##################################################### + ############################################################################## + prepare: + name: Prepare + runs-on: ubuntu-latest + # needs: [nothing] + steps: + ########################################################################## + # CHECKOUT CODE ########################################################## + ########################################################################## + - name: Checkout code + uses: actions/checkout@v2 + ########################################################################## + # TODO: DO STUFF ??? ##################################################### + ########################################################################## + - name: Check translation files + run: | + scripts/translations/sort.sh + scripts/translations/missing-keys.sh + + ############################################################################## + # JOB: DOCKER BUILD COMMUNITY NEO4J ########################################## + ############################################################################## + build_production_neo4j: + name: Docker Build Production - Neo4J + runs-on: ubuntu-latest + needs: [prepare] + env: + - VERSION: $(yarn run version) + - BUILD_DATE: $(date -u +'%Y-%m-%dT%H:%M:%SZ') + - BUILD_VERSION: ${VERSION}.${GITHUB_RUN_ID} + - BUILD_COMMIT: $(git rev-parse --short HEAD) + steps: + ########################################################################## + # CHECKOUT CODE ########################################################## + ########################################################################## + - name: Checkout code + uses: actions/checkout@v2 + ########################################################################## + # NEO4J ################################################################## + ########################################################################## + - name: Neo4J | Build `community` image + run: | + docker build --target community -t "ocelotsocialnetwork/neo4j:community" -t "ocelotsocialnetwork/neo4j:${VERSION}" -t "ocelotsocialnetwork/neo4j:${BUILD_VERSION}" neo4j/ + docker save "ocelotsocialnetwork/neo4j:community" > /tmp/neo4j.tar + - name: Upload Artifact + uses: actions/upload-artifact@v2 + with: + name: docker-neo4j-community + path: /tmp/neo4j.tar + + ############################################################################## + # JOB: DOCKER BUILD Production BACKEND ####################################### + ############################################################################## + build_production_backend: + name: Docker Build Production - Backend + runs-on: ubuntu-latest + needs: [prepare] + env: + - VERSION: $(yarn run version) + - BUILD_DATE: $(date -u +'%Y-%m-%dT%H:%M:%SZ') + - BUILD_VERSION: ${VERSION}.${GITHUB_RUN_ID} + - BUILD_COMMIT: $(git rev-parse --short HEAD) + steps: + ########################################################################## + # CHECKOUT CODE ########################################################## + ########################################################################## + - name: Checkout code + uses: actions/checkout@v2 + ########################################################################## + # BUILD BACKEND DOCKER IMAGE (production) ################################ + ########################################################################## + - name: backend | Build `production` image + run: | + docker build --target production -t "ocelotsocialnetwork/backend:latest" -t "ocelotsocialnetwork/backend:${VERSION}" -t "ocelotsocialnetwork/backend:${BUILD_VERSION}" backend/ + docker save "ocelotsocialnetwork/backend:latest" > /tmp/backend.tar + - name: Upload Artifact + uses: actions/upload-artifact@v2 + with: + name: docker-backend-production + path: /tmp/backend.tar + + ############################################################################## + # JOB: DOCKER BUILD PRODUCTION WEBAPP ######################################## + ############################################################################## + build_production_webapp: + name: Docker Build Production - WebApp + runs-on: ubuntu-latest + needs: [prepare] + env: + - VERSION: $(yarn run version) + - BUILD_DATE: $(date -u +'%Y-%m-%dT%H:%M:%SZ') + - BUILD_VERSION: ${VERSION}.${GITHUB_RUN_ID} + - BUILD_COMMIT: $(git rev-parse --short HEAD) + steps: + ########################################################################## + # CHECKOUT CODE ########################################################## + ########################################################################## + - name: Checkout code + uses: actions/checkout@v2 + ########################################################################## + # BUILD WEBAPP DOCKER IMAGE (build) ###################################### + ########################################################################## + - name: webapp | Build `production` image + run: | + docker build --target production -t "ocelotsocialnetwork/webapp:latest" -t "ocelotsocialnetwork/webapp:${VERSION}" -t "ocelotsocialnetwork/webapp:${BUILD_VERSION}" webapp/ + docker save "ocelotsocialnetwork/webapp:latest" > /tmp/webapp.tar + - name: Upload Artifact + uses: actions/upload-artifact@v2 + with: + name: docker-webapp-production + path: /tmp/webapp.tar + + ############################################################################## + # JOB: DOCKER BUILD PRODUCTION MAINTENANCE ################################### + ############################################################################## + build_production_maintenance: + name: Docker Build Production - Maintenance + runs-on: ubuntu-latest + needs: [prepare] + env: + - VERSION: $(yarn run version) + - BUILD_DATE: $(date -u +'%Y-%m-%dT%H:%M:%SZ') + - BUILD_VERSION: ${VERSION}.${GITHUB_RUN_ID} + - BUILD_COMMIT: $(git rev-parse --short HEAD) + steps: + ########################################################################## + # CHECKOUT CODE ########################################################## + ########################################################################## + - name: Checkout code + uses: actions/checkout@v2 + ########################################################################## + # BUILD MAINTENANCE DOCKER IMAGE (build) ################################# + ########################################################################## + - name: maintenance | Build `production` image + run: | + docker build --target production -t "ocelotsocialnetwork/maintenance:latest" -t "ocelotsocialnetwork/maintenance:${VERSION}" -t "ocelotsocialnetwork/maintenance:${BUILD_VERSION}" webapp/ -f webapp/Dockerfile.maintenance + docker save "ocelotsocialnetwork/maintenance:latest" > /tmp/maintenance.tar + - name: Upload Artifact + uses: actions/upload-artifact@v2 + with: + name: docker-maintenance-production + path: /tmp/maintenance.tar + + ############################################################################## + # JOB: UPLOAD TO DOCKERHUB ################################################### + ############################################################################## + upload_to_dockerhub: + name: Upload to Dockerhub + runs-on: ubuntu-latest + needs: [build_production_neo4j,build_production_backend,build_production_webapp,build_production_maintenance] + steps: + ########################################################################## + # CHECKOUT CODE ########################################################## + ########################################################################## + - name: Checkout code + uses: actions/checkout@v2 + ########################################################################## + # DOWNLOAD DOCKER IMAGES ################################################# + ########################################################################## + - name: Download Docker Image (Neo4J) + uses: actions/download-artifact@v2 + with: + name: docker-neo4j-community + path: /tmp + - name: Load Docker Image + run: docker load < /tmp/neo4j.tar + - name: Download Docker Image (Backend) + uses: actions/download-artifact@v2 + with: + name: docker-backend-production + path: /tmp + - name: Load Docker Image + run: docker load < /tmp/backend.tar + - name: Download Docker Image (WebApp) + uses: actions/download-artifact@v2 + with: + name: docker-webapp-production + path: /tmp + - name: Load Docker Image + run: docker load < /tmp/webapp.tar + - name: Download Docker Image (Maintenance) + uses: actions/download-artifact@v2 + with: + name: docker-maintenance-production + path: /tmp + - name: Load Docker Image + run: docker load < /tmp/maintenance.tar + ########################################################################## + # Upload ################################################################# + ########################################################################## + - name: login to dockerhub + # TODO: Handle secrets + run: echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin + - name: Push neo4j + run: docker push ocelotsocialnetwork/neo4j --all-tags + - name: Push backend + run: docker push ocelotsocialnetwork/backend --all-tags + - name: Push webapp + run: docker push ocelotsocialnetwork/webapp --all-tags + - name: Push maintenance + run: docker push ocelotsocialnetwork/maintenance --all-tags + + ############################################################################## + # JOB: GITHUB TAG LATEST VERSION ############################################# + ############################################################################## + github_tag: + name: Tag latest version on Github + runs-on: ubuntu-latest + needs: [upload_to_dockerhub] + env: + - VERSION: $(yarn run version) + - BUILD_DATE: $(date -u +'%Y-%m-%dT%H:%M:%SZ') + - BUILD_VERSION: ${VERSION}.${GITHUB_RUN_ID} + - BUILD_COMMIT: $(git rev-parse --short HEAD) + steps: + # Push tag to GitHub if package.json version's tag is not tagged + - name: package-version + run: node -p -e '`PACKAGE_VERSION=${require("./package.json").version}`' >> $GITHUB_ENV + - name: package-version-to-git-tag + uses: pkgdeps/git-tag-action@v2 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + github_repo: ${{ github.repository }} + version: ${{ env.PACKAGE_VERSION }} + git_commit_sha: ${{ github.sha }} + git_tag_prefix: "v" + - name: package-version-to-git-tag + build number + uses: pkgdeps/git-tag-action@v2 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + github_repo: ${{ github.repository }} + version: ${{ env.BUILD_VERSION }} + git_commit_sha: ${{ github.sha }} + git_tag_prefix: "b" \ No newline at end of file diff --git a/backend/Dockerfile b/backend/Dockerfile index ba14e6586..837fa68c8 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -8,9 +8,8 @@ FROM node:12.19.0-alpine3.10 as base ENV DOCKER_WORKDIR="/app" ## We Cannot do `$(date -u +'%Y-%m-%dT%H:%M:%SZ')` here so we use unix timestamp=0 ENV BUILD_DATE="1970-01-01T00:00:00.00Z" -## We cannot do $(yarn run version) here so we default to 0.0.0 -## TODO: Missing Build number - do that once we have a CI which actually generates it -ENV BUILD_VERSION="0.0.0" +## We cannot do $(yarn run version).${BUILD_NUMBER} here so we default to 0.0.0.0 +ENV BUILD_VERSION="0.0.0.0" ## We cannot do `$(git rev-parse --short HEAD)` here so we default to 0000000 ENV BUILD_COMMIT="0000000" ## SET NODE_ENV diff --git a/docker-compose.yml b/docker-compose.yml index b3d034621..392447f61 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,7 +29,7 @@ services: - BUILD_DATE - BUILD_VERSION - BUILD_COMMIT - - NODE_ENV="development" + - NODE_ENV="production" # Application only envs - HOST=0.0.0.0 # This is nuxt specific, alternative value is HOST=webapp - GRAPHQL_URI=http://backend:4000 @@ -60,7 +60,7 @@ services: - BUILD_DATE - BUILD_VERSION - BUILD_COMMIT - - NODE_ENV="development" + - NODE_ENV="production" # Application only envs - DEBUG=false - NEO4J_URI=bolt://neo4j:7687 diff --git a/scripts/docker_push.sh b/scripts/docker_push.sh index 90e0fb8c1..816c5c4f0 100755 --- a/scripts/docker_push.sh +++ b/scripts/docker_push.sh @@ -11,7 +11,7 @@ tags=($major $major.$minor $major.$minor.$patch) # docker build --build-arg BUILD_COMMIT=$BUILD_COMMIT --target production -t ocelotsocialnetwork/backend:latest $ROOT_DIR/backend # docker build --build-arg BUILD_COMMIT=$BUILD_COMMIT --target production -t ocelotsocialnetwork/webapp:latest $ROOT_DIR/webapp # docker build --build-arg BUILD_COMMIT=$BUILD_COMMIT -t ocelotsocialnetwork/neo4j:latest $ROOT_DIR/neo4j -docker build -t ocelotsocialnetwork/maintenance:latest $ROOT_DIR/webapp/ -f $ROOT_DIR/webapp/Dockerfile.maintenance +# docker build -t ocelotsocialnetwork/maintenance:latest $ROOT_DIR/webapp/ -f $ROOT_DIR/webapp/Dockerfile.maintenance echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin