Skip to content

Commit

Permalink
Save digest in build and use for deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
baksetercx committed Jan 22, 2025
1 parent d03de99 commit 1776854
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
38 changes: 25 additions & 13 deletions build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: |
you must first add your GitHub repository to [github-repositories-terraform](https://github.com/3lvia/github-repositories-terraform).
inputs:
name:
description: 'Name of application. This will be used as the image name. For Elvia applications, do not include the namespace.'
description: 'Name of application. This will be used for the image name. For Elvia applications, do not include the namespace.'
required: true
namespace:
description: 'Namespace or system of the application. Required for Elvia applications.'
Expand Down Expand Up @@ -106,12 +106,12 @@ inputs:
required: false

outputs:
image-name:
image-name-tag:
description: 'Name of the Docker image that was built, with tag.'
value: ${{ steps.get-outputs.outputs.image-name }}
image-digest:
value: ${{ steps.get-outputs.outputs.image-name-tag }}
image-name-digest:
description: 'Name of the Docker image that was built, with digest.'
value: ${{ steps.get-outputs.outputs.image-name }}
value: ${{ steps.get-outputs.outputs.image-name-digest }}

runs:
using: 'composite'
Expand Down Expand Up @@ -168,7 +168,7 @@ runs:
- name: Install 3lv CLI
uses: 3lvia/cli/setup@trunk
with:
version: '0.28.1' # TODO: remove this (which will get latest version) when 3lv CLI is stable
version: 'feat/use-full-image-deploy' # TODO: remove this (which will get latest version) when 3lv CLI is stable

- name: Install Cosign if not using Elvia runner
if: ${{ !startsWith(runner.name, 'elvia-runner-') && inputs.sign-image == 'true' }}
Expand Down Expand Up @@ -217,17 +217,29 @@ runs:
shell: bash
id: get-outputs
run: |
IMAGE_NAME=$(cat /tmp/3lv-cli-output/image-name)
IMAGE_NAME_TAG=$(cat /tmp/3lv-cli-output/image-name)
IMAGE_NAME_DIGEST="${IMAGE_NAME_TAG%%:*}@$(docker manifest inspect -v "$IMAGE_NAME_TAG" | jq -r '.Descriptor.digest')"
echo "image-name=$IMAGE_NAME" >> "$GITHUB_OUTPUT"
echo "image-digest=$(docker inspect --format='{{index .RepoDigests 0}}' $IMAGE_NAME)" >> "$GITHUB_OUTPUT"
echo "image-name-tag=$IMAGE_NAME_TAG" >> "$GITHUB_OUTPUT"
echo "image-name-digest=$IMAGE_NAME_DIGEST" >> "$GITHUB_OUTPUT"
echo "$IMAGE_NAME_TAG" > /tmp/build-info-image-name-tag
echo "$IMAGE_NAME_DIGEST" > /tmp/build-info-image-name-digest
- name: Upload build information to artifact
uses: actions/upload-artifact@v4
continue-on-error: true # ignore error since we can always use default taag
with:
name: 'build-info-${{ inputs.name }}-${{ inputs.namespace }}'
path: '/tmp/build-info-*'
retention-days: 3

- name: Sign image with Cosign using GitHub OIDC token
if: ${{ inputs.sign-image == 'true' }}
shell: bash
run: cosign sign -y --oidc-provider='github-actions' "$IMAGE_DIGEST"
run: cosign sign -y --oidc-provider='github-actions' "$IMAGE_NAME_DIGEST"
env:
IMAGE_DIGEST: ${{ steps.get-outputs.outputs.image-digest }}
IMAGE_DIGEST: ${{ steps.get-outputs.outputs.image-name-digest }}

- name: Verify image signatue
if: ${{ inputs.sign-image == 'true' }}
Expand All @@ -236,10 +248,10 @@ runs:
cosign verify \
--certificate-identity "$CERTIFICATE_IDENTITY" \
--certificate-oidc-issuer 'https://token.actions.githubusercontent.com' \
"$IMAGE_DIGEST" | jq
"$IMAGE_NAME_DIGEST" | jq
env:
CERTIFICATE_IDENTITY: 'https://github.com/${{ github.workflow_ref }}'
IMAGE_DIGEST: ${{ steps.get-outputs.outputs.image-digest }}
IMAGE_NAME_DIGEST: ${{ steps.get-outputs.outputs.image-name-digest }}

- name: Upload Trivy scan results to GitHub Advanced Security
if: ${{ inputs.trivy-upload-report == 'true' && !cancelled() }}
Expand Down
21 changes: 18 additions & 3 deletions deploy/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,20 @@ runs:
- name: Install 3lv CLI
uses: 3lvia/cli/setup@trunk
with:
version: '0.28.1' # TODO: remove this (which will get latest version) when 3lv CLI is stable
version: 'feat/use-full-image-deploy' # TODO: remove this (which will get latest version) when 3lv CLI is stable

- name: Download artifact with build information
uses: actions/download-artifact@v4
continue-on-error: true # ignore error since we can always use default taag
with:
name: 'build-info-${{ inputs.name }}-${{ inputs.namespace }}'

- name: Get image digest and tag from build information
shell: bash
continue-on-error: true # ignore error since we can always use default taag
run: |
echo "IMAGE_NAME_DIGEST=$(cat build-info-image-name-digest)" >> "$GITHUB_ENV"
echo "IMAGE_NAME_TAG=$(cat build-info-image-name-tag)" >> "$GITHUB_ENV"
- name: Deploy
shell: bash
Expand All @@ -139,19 +152,21 @@ runs:
--system-name '${{ inputs.namespace }}' \
--helm-values-file "$HELM_VALUES_FILE" \
--environment '${{ inputs.environment }}' \
--image "$IMAGE" \
--workload-type '${{ inputs.workload-type }}' \
--runtime-cloud-provider '${{ inputs.runtime-cloud-provider }}' \
--image-tag "$IMAGE_TAG" \
--add-deployment-annotation \
--grafana-url "$GRAFANA_URL" \
--grafana-api-key "$GRAFANA_API_KEY" \
--run-id '${{ github.run_id }}' \
'${{ inputs.name }}'
env:
HELM_VALUES_FILE: ${{ inputs.helm-values-path == '' && inputs.helm-values-file || inputs.helm-values-path }}
IMAGE_TAG: ${{ inputs.override-image-tag == '' && format('{0}-{1}', github.sha, github.run_number) || inputs.override-image-tag }}
# Order of precedence: digest if not empty, tag if not empty, finally default to '{sha}-{run_number}'
IMAGE: ${{ env.IMAGE_NAME_DIGEST != '' && env.IMAGE_NAME_DIGEST || (env.IMAGE_NAME_TAG != '' && env.IMAGE_NAME_TAG || format('{0}-{1}', github.sha, github.run_number)) }}
# Pass optional inputs as environment variables, since they can be empty.
# The CLI does not accept empty strings passed to the flags, e.g. `--gke-project-id ''` will cause an error.
3LV_IMAGE_DIGEST: ${{ env.IMAGE_DIGEST }}
3LV_AZURE_TENANT_ID: ${{ inputs.AZURE_TENANT_ID }}
3LV_AZURE_CLIENT_ID: ${{ inputs.AZURE_CLIENT_ID }}
3LV_AZURE_FEDERATED_TOKEN: ${{ steps.get-federated-token.outputs.token }}
Expand Down

0 comments on commit 1776854

Please sign in to comment.