|
| 1 | +name: Publish nginx serve image |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - develop |
| 7 | + - project/* |
| 8 | + |
| 9 | +permissions: |
| 10 | + packages: write |
| 11 | + |
| 12 | + |
| 13 | +jobs: |
| 14 | + publish_image: |
| 15 | + name: Publish Docker Image |
| 16 | + runs-on: ubuntu-latest |
| 17 | + |
| 18 | + outputs: |
| 19 | + docker_image_name: ${{ steps.prep.outputs.tagged_image_name }} |
| 20 | + docker_image_tag: ${{ steps.prep.outputs.tag }} |
| 21 | + docker_image: ${{ steps.prep.outputs.tagged_image }} |
| 22 | + |
| 23 | + steps: |
| 24 | + - uses: actions/checkout@main |
| 25 | + |
| 26 | + - name: Login to GitHub Container Registry |
| 27 | + uses: docker/login-action@v3 |
| 28 | + with: |
| 29 | + registry: ghcr.io |
| 30 | + username: ${{ github.actor }} |
| 31 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 32 | + |
| 33 | + - name: 🐳 Prepare Docker |
| 34 | + id: prep |
| 35 | + env: |
| 36 | + IMAGE_NAME: ghcr.io/${{ github.repository }} |
| 37 | + run: | |
| 38 | + BRANCH_NAME=$(echo $GITHUB_REF_NAME | sed 's|:|-|' | tr '[:upper:]' '[:lower:]' | sed 's/_/-/g' | cut -c1-100 | sed 's/-*$//') |
| 39 | +
|
| 40 | + # XXX: Check if there is a slash in the BRANCH_NAME eg: project/add-docker |
| 41 | + if [[ "$BRANCH_NAME" == *"/"* ]]; then |
| 42 | + # XXX: Change the docker image package to -alpha |
| 43 | + IMAGE_NAME="$IMAGE_NAME-alpha" |
| 44 | + TAG="$(echo "$BRANCH_NAME" | sed 's|/|-|g').$(echo $GITHUB_SHA | head -c7)" |
| 45 | + else |
| 46 | + TAG="$BRANCH_NAME.$(echo $GITHUB_SHA | head -c7)" |
| 47 | + fi |
| 48 | +
|
| 49 | + IMAGE_NAME=$(echo $IMAGE_NAME | tr '[:upper:]' '[:lower:]') |
| 50 | + echo "tagged_image_name=${IMAGE_NAME}" >> $GITHUB_OUTPUT |
| 51 | + echo "tag=${TAG}" >> $GITHUB_OUTPUT |
| 52 | + echo "tagged_image=${IMAGE_NAME}:${TAG}" >> $GITHUB_OUTPUT |
| 53 | + echo "::notice::Tagged docker image: ${IMAGE_NAME}:${TAG}" |
| 54 | +
|
| 55 | + - name: 🐳 Set up Docker Buildx |
| 56 | + id: buildx |
| 57 | + uses: docker/setup-buildx-action@v3 |
| 58 | + |
| 59 | + - name: 🐳 Cache Docker layers |
| 60 | + uses: actions/cache@v4 |
| 61 | + with: |
| 62 | + path: /tmp/.buildx-cache |
| 63 | + key: ${{ runner.os }}-buildx-${{ github.ref }} |
| 64 | + restore-keys: | |
| 65 | + ${{ runner.os }}-buildx-refs/develop |
| 66 | + ${{ runner.os }}-buildx- |
| 67 | +
|
| 68 | + - name: 🐳 Docker build |
| 69 | + uses: docker/build-push-action@v6 |
| 70 | + with: |
| 71 | + context: . |
| 72 | + builder: ${{ steps.buildx.outputs.name }} |
| 73 | + file: nginx-serve/Dockerfile |
| 74 | + target: nginx-serve |
| 75 | + load: true |
| 76 | + push: true |
| 77 | + tags: ${{ steps.prep.outputs.tagged_image }} |
| 78 | + cache-from: type=local,src=/tmp/.buildx-cache |
| 79 | + cache-to: type=local,dest=/tmp/.buildx-cache-new |
| 80 | + build-args: | |
| 81 | + "APP_SENTRY_TRACES_SAMPLE_RATE=0.8" |
| 82 | + "APP_SENTRY_REPLAYS_SESSION_SAMPLE_RATE=0.8" |
| 83 | + "APP_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE=0.8" |
| 84 | +
|
| 85 | + - name: 🐳 Move docker cache |
| 86 | + run: | |
| 87 | + rm -rf /tmp/.buildx-cache |
| 88 | + mv /tmp/.buildx-cache-new /tmp/.buildx-cache |
| 89 | +
|
| 90 | + publish_helm: |
| 91 | + name: Publish Helm |
| 92 | + needs: publish_image |
| 93 | + runs-on: ubuntu-latest |
| 94 | + |
| 95 | + steps: |
| 96 | + - name: Checkout code |
| 97 | + uses: actions/checkout@v4 |
| 98 | + |
| 99 | + - name: Login to GitHub Container Registry |
| 100 | + uses: docker/login-action@v3 |
| 101 | + with: |
| 102 | + registry: ghcr.io |
| 103 | + username: ${{ github.actor }} |
| 104 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 105 | + |
| 106 | + - name: Install Helm |
| 107 | + uses: azure/setup-helm@v3 |
| 108 | + |
| 109 | + - name: Tag docker image in Helm Chart values.yaml |
| 110 | + env: |
| 111 | + IMAGE_NAME: ${{ needs.publish_image.outputs.docker_image_name }} |
| 112 | + IMAGE_TAG: ${{ needs.publish_image.outputs.docker_image_tag }} |
| 113 | + run: | |
| 114 | + # Update values.yaml with latest docker image |
| 115 | + sed -i "s|SET-BY-CICD-IMAGE|$IMAGE_NAME|" nginx-serve/helm/values.yaml |
| 116 | + sed -i "s/SET-BY-CICD-TAG/$IMAGE_TAG/" nginx-serve/helm/values.yaml |
| 117 | +
|
| 118 | + - name: Package Helm Chart |
| 119 | + id: set-variables |
| 120 | + run: | |
| 121 | + # XXX: Check if there is a slash in the BRANCH_NAME eg: project/add-docker |
| 122 | + if [[ "$GITHUB_REF_NAME" == *"/"* ]]; then |
| 123 | + # XXX: Change the helm chart to <chart-name>-alpha |
| 124 | + sed -i 's/^name: \(.*\)/name: \1-alpha/' nginx-serve/helm/Chart.yaml |
| 125 | + fi |
| 126 | +
|
| 127 | + SHA_SHORT=$(git rev-parse --short HEAD) |
| 128 | + sed -i "s/SET-BY-CICD/$SHA_SHORT/g" nginx-serve/helm/Chart.yaml |
| 129 | + helm package ./nginx-serve/helm -d .helm-charts |
| 130 | +
|
| 131 | + - name: Push Helm Chart |
| 132 | + env: |
| 133 | + IMAGE: ${{ needs.publish_image.outputs.docker_image }} |
| 134 | + OCI_REPO: oci://ghcr.io/${{ github.repository }} |
| 135 | + run: | |
| 136 | + OCI_REPO=$(echo $OCI_REPO | tr '[:upper:]' '[:lower:]') |
| 137 | + PACKAGE_FILE=$(ls .helm-charts/*.tgz | head -n 1) |
| 138 | + echo "# Helm Chart" >> $GITHUB_STEP_SUMMARY |
| 139 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 140 | + echo "Tagged Image: **$IMAGE**" >> $GITHUB_STEP_SUMMARY |
| 141 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 142 | + echo "Helm push output" >> $GITHUB_STEP_SUMMARY |
| 143 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 144 | + echo '```bash' >> $GITHUB_STEP_SUMMARY |
| 145 | + helm push "$PACKAGE_FILE" $OCI_REPO >> $GITHUB_STEP_SUMMARY |
| 146 | + echo '```' >> $GITHUB_STEP_SUMMARY |
0 commit comments