-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor deployment + change AWS account #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bf694cb
Refactor deployment + change AWS account
nolan1999 c216351
Adapt integration tests to avoid S3 collisions
nolan1999 d5ad68d
no need for weird checkout
nolan1999 b66f804
Update .github/workflows/publish-version-ecr.yaml
nolan1999 2a5c870
correct ref
nolan1999 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| name: Publish version to AWS ECR | ||
|
|
||
| on: | ||
| workflow_dispatch: # manual trigger to publish prod/dev version | ||
| workflow_run: # trigger on GH version to publish prod version | ||
| workflows: ["Publish version to GitHub"] | ||
| types: | ||
| - completed | ||
| branches: | ||
| - main | ||
|
|
||
| jobs: | ||
| build-and-push: | ||
| name: Build and push Docker image | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v3 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Set version | ||
| id: set-version | ||
| run: | | ||
| if [ "${{ github.event_name }}" == "workflow_run" ]; then | ||
| POETRY_VERSION=$(grep -E '^requires-poetry = ' pyproject.toml | sed -E 's/requires-poetry = "(.*)"/\1/') | ||
| pip install poetry==$POETRY_VERSION | ||
| PROD=true | ||
| VERSION=$(poetry version -s) | ||
| REF=refs/tags/$VERSION | ||
| else | ||
| REF=$GITHUB_REF | ||
| if [[ "$GITHUB_REF" == refs/tags/* ]]; then | ||
| PROD=true | ||
| VERSION=${GITHUB_REF#refs/tags/} | ||
| else | ||
| PROD=false | ||
| VERSION=dev-${GITHUB_REF#refs/heads/}-${GITHUB_SHA::7} | ||
| fi | ||
| fi | ||
| echo "PROD=$PROD" >> $GITHUB_OUTPUT | ||
| echo "VERSION=$VERSION" >> $GITHUB_OUTPUT | ||
| echo "REF=$REF" >> $GITHUB_OUTPUT | ||
|
|
||
| # on main, we do not want necessarily the latest commit, but the one that was tagged | ||
| - name: Checkout code | ||
| uses: actions/checkout@v3 | ||
| with: | ||
| ref: ${{ steps.set-version.outputs.REF }} | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Get Python version | ||
| id: get-python-version | ||
| run: | | ||
| pip install toml | ||
| PYTHON_VERSION=$(python -c 'import scripts.vars; scripts.vars.get_python_version()') | ||
| echo "PYTHON_VERSION=$PYTHON_VERSION" >> $GITHUB_OUTPUT | ||
|
|
||
| - name: Configure AWS credentials | ||
| uses: aws-actions/configure-aws-credentials@v2 | ||
| with: | ||
| aws-access-key-id: ${{ secrets.ECR_AWS_ACCESS_KEY_ID }} | ||
| aws-secret-access-key: ${{ secrets.ECR_AWS_SECRET_ACCESS_KEY }} | ||
| aws-region: us-east-1 # required for Public ECR | ||
|
|
||
| - name: Login to AWS Public ECR | ||
| uses: aws-actions/amazon-ecr-login@v1 | ||
| with: | ||
| registry-type: public | ||
|
|
||
| - name: Build and push Docker image | ||
| id: build-and-push | ||
| env: | ||
| VERSION: ${{ steps.set-version.outputs.VERSION }} | ||
| PROD: ${{ steps.set-version.outputs.PROD }} | ||
| ECR_REGISTRY: public.ecr.aws/w2b7b8c0 | ||
| ECR_REPOSITORY: decode-cloud/user-api | ||
| PYTHON_VERSION: ${{ steps.get-python-version.outputs.PYTHON_VERSION }} | ||
| run: | | ||
| IMAGE_REF=$ECR_REGISTRY/$ECR_REPOSITORY:$VERSION | ||
| echo "IMAGE_REF=$IMAGE_REF" >> $GITHUB_OUTPUT | ||
| if docker manifest inspect $IMAGE_REF > /dev/null 2>&1; then | ||
| NEW_IMAGE=false | ||
| echo "Image $IMAGE_REF already exists, nothing pushed" >> $GITHUB_STEP_SUMMARY | ||
| else | ||
| NEW_IMAGE=true | ||
| docker build --build-arg PYTHON_VERSION=$PYTHON_VERSION -t $IMAGE_REF . | ||
| docker push $IMAGE_REF | ||
| echo "## 🚀 Published Docker Image: $IMAGE_REF" >> $GITHUB_STEP_SUMMARY | ||
| if [[ $PROD == "true" ]]; then | ||
| SET_LATEST=true | ||
| LATEST_EXISTS=$(docker manifest inspect $ECR_REGISTRY/$ECR_REPOSITORY:latest > /dev/null 2>&1 && echo "true" || echo "false") | ||
| if [[ $LATEST_EXISTS == "true" ]]; then | ||
| LATEST_LABELS=$(docker manifest inspect $ECR_REGISTRY/$ECR_REPOSITORY:latest | grep -o '"org.opencontainers.image.version":"[^"]*"' | cut -d'"' -f4 || echo "") | ||
| if printf '%s\n%s\n' "$LATEST_LABELS" "$VERSION" | sort -V | head -n1 | grep -q "^$VERSION$"; then | ||
| SET_LATEST=false | ||
| fi | ||
| fi | ||
| if [[ $SET_LATEST == "true" ]]; then | ||
| docker tag $IMAGE_REF $ECR_REGISTRY/$ECR_REPOSITORY:latest | ||
| docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest | ||
| echo "Also tagged as: \`$ECR_REGISTRY/$ECR_REPOSITORY:latest\`" >> $GITHUB_STEP_SUMMARY | ||
| fi | ||
| fi | ||
| fi | ||
| echo "NEW_IMAGE=$NEW_IMAGE" >> $GITHUB_OUTPUT | ||
|
|
||
| - name: Add to GH release | ||
| if: steps.build-and-push.outputs.NEW_IMAGE == 'true' && steps.set-version.outputs.PROD == 'true' | ||
| uses: tubone24/update_release@v1.0 | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| TAG_NAME: ${{ steps.set-version.outputs.VERSION }} | ||
| with: | ||
| body: "**Published image (AWS ECR Public):** `${{ steps.build-and-push.outputs.IMAGE_REF }}`" | ||
| is_append_body: true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| name: Publish version to GitHub | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
|
|
||
| jobs: | ||
| create-version: | ||
| name: Create GH version and tag | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v3 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Set version | ||
| id: set-version | ||
| run: | | ||
| POETRY_VERSION=$(grep -E '^requires-poetry = ' pyproject.toml | sed -E 's/requires-poetry = "(.*)"/\1/') | ||
| pip install poetry==$POETRY_VERSION | ||
| VERSION=$(poetry version -s) | ||
| echo "VERSION=$VERSION" >> $GITHUB_OUTPUT | ||
|
|
||
| - name: Check if release exists | ||
| id: check-release | ||
| env: | ||
| VERSION: ${{ steps.set-version.outputs.VERSION }} | ||
| run: | | ||
| git fetch --tags | ||
| if [ -n "$(git tag -l "$VERSION")" ]; then | ||
| echo "## ⚠️ Tag $VERSION already exists in git. Skipping publish." >> $GITHUB_STEP_SUMMARY | ||
| echo "skip_publish=true" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "skip_publish=false" >> $GITHUB_OUTPUT | ||
| fi | ||
|
|
||
| - name: Create and push annotated git tag | ||
| if: steps.check-release.outputs.skip_publish != 'true' | ||
| env: | ||
| VERSION: ${{ steps.set-version.outputs.VERSION }} | ||
| run: | | ||
| git config user.name "github-actions" | ||
| git config user.email "github-actions@github.com" | ||
| git tag -a "$VERSION" -m "Release $VERSION" | ||
| git push origin "$VERSION" | ||
|
|
||
| - name: Create GitHub release | ||
| if: steps.check-release.outputs.skip_publish != 'true' | ||
| uses: softprops/action-gh-release@v1 | ||
| with: | ||
| tag_name: ${{ steps.set-version.outputs.VERSION }} | ||
| name: Release ${{ steps.set-version.outputs.VERSION }} | ||
| generate_release_notes: true |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.