|
1 | | -name: Publish image and deploy to staging |
| 1 | +name: Build image from main and deploy to staging |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
5 | 5 | branches: |
6 | 6 | - main |
7 | 7 |
|
8 | 8 | jobs: |
9 | | - publish: |
| 9 | + prepare-variables: |
10 | 10 | runs-on: ubuntu-24.04 |
| 11 | + timeout-minutes: 5 |
11 | 12 | permissions: |
12 | | - id-token: write # needed by aws-actions/configure-aws-credentials |
13 | 13 | contents: read |
14 | | - env: |
15 | | - AWS_REGION: eu-west-1 |
| 14 | + |
| 15 | + outputs: |
| 16 | + image_tag: ${{ steps.prepare-variables.outputs.IMAGE_TAG }} |
| 17 | + ecr_tags: ${{ steps.prepare-variables.outputs.ECR_TAGS }} |
16 | 18 |
|
17 | 19 | steps: |
18 | | - - name: Checkout repository |
19 | | - uses: actions/checkout@v5 |
20 | | - |
21 | | - - name: Set up QEMU |
22 | | - uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3.6.0 |
23 | | - with: |
24 | | - platforms: arm64 |
| 20 | + # Required by prepare-variables |
| 21 | + - uses: actions/checkout@v6 |
25 | 22 |
|
26 | | - - name: Set up Docker Buildx |
27 | | - uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1 |
28 | | - with: |
29 | | - install: true |
| 23 | + - name: Prepare variables |
| 24 | + id: prepare-variables |
| 25 | + env: |
| 26 | + # github.ref_name is the plain (no prefix) branch or tag name that |
| 27 | + # triggered the workflow (e.g. "main", "v1.2.3"). |
| 28 | + # We are passing it through env to prevent script injection via crafted |
| 29 | + # branch names (e.g. a branch named `; rm -rf /` would be interpolated |
| 30 | + # directly into the shell script if using ${{ }} inline). |
| 31 | + REF_NAME: ${{ github.ref_name }} |
| 32 | + run: | |
| 33 | + set -x |
30 | 34 |
|
31 | | - - name: Configure AWS credentials |
32 | | - uses: aws-actions/configure-aws-credentials@a03048d87541d1d9fcf2ecf528a4a65ba9bd7838 # v5.0.0 |
33 | | - with: |
34 | | - role-to-assume: arn:aws:iam::${{ secrets.AWS_STAGING_ACCOUNT_ID }}:role/${{ secrets.AWS_APPS_SALEOR_MCP_STAGING_CICD_ROLE_NAME }} |
35 | | - aws-region: ${{ env.AWS_REGION }} |
| 35 | + image_tag_unique="${REF_NAME}-$(git rev-parse --short HEAD)" |
| 36 | + ecr_tags="${image_tag_unique},${REF_NAME}-latest" |
36 | 37 |
|
37 | | - - id: ecr-login |
38 | | - name: Login to Amazon ECR |
39 | | - uses: aws-actions/amazon-ecr-login@062b18b96a7aff071d4dc91bc00c4c1a7945b076 # v2.0.1 |
40 | | - with: |
41 | | - registries: ${{ secrets.AWS_ECR_ACCOUNT }} |
| 38 | + echo "IMAGE_TAG=${image_tag_unique}" >> $GITHUB_OUTPUT |
| 39 | + echo "ECR_TAGS=${ecr_tags}" >> $GITHUB_OUTPUT |
42 | 40 |
|
43 | | - - name: Evaluate image tags |
44 | | - env: |
45 | | - IMAGE_REPOSITORY: ${{ steps.ecr-login.outputs.registry }}/${{ secrets.ECR_REPOSITORY }} |
46 | | - BRANCH_IMAGE_TAG: ${{ github.ref_name }} |
47 | | - run: | |
48 | | - UNIQUE_IMAGE_TAG=${BRANCH_IMAGE_TAG}-$(git rev-parse --short HEAD) |
49 | 41 |
|
50 | | - IMAGE_TAGS=${IMAGE_REPOSITORY}:${BRANCH_IMAGE_TAG},${IMAGE_REPOSITORY}:${UNIQUE_IMAGE_TAG} |
| 42 | + build-push: |
| 43 | + needs: [prepare-variables] |
| 44 | + uses: saleor/saleor-internal-actions/.github/workflows/build-push-image-multi-platform.yaml@9c7a814c011945f26f0aa1191fc9c62de45477c2 # v1.7.0 |
51 | 45 |
|
52 | | - echo "UNIQUE_IMAGE_TAG=${UNIQUE_IMAGE_TAG}" >> $GITHUB_ENV |
53 | | - echo "IMAGE_TAGS=${IMAGE_TAGS}" >> $GITHUB_ENV |
| 46 | + permissions: |
| 47 | + contents: read |
| 48 | + id-token: write # needed for AWS/ECR login |
| 49 | + packages: write # needed for GHCR (not used, but required permission) |
54 | 50 |
|
55 | | - - name: Build and push |
56 | | - timeout-minutes: 20 |
57 | | - uses: docker/build-push-action@v4 |
| 51 | + with: |
| 52 | + tags: ${{ needs.prepare-variables.outputs.ecr_tags }} |
| 53 | + enable-aws-ecr: true |
| 54 | + aws-ecr-region: eu-west-1 |
| 55 | + |
| 56 | + secrets: |
| 57 | + oci-full-repository: ${{ secrets.AWS_ECR_ACCOUNT }}.dkr.ecr.eu-west-1.amazonaws.com/${{ secrets.ECR_REPOSITORY }} |
| 58 | + aws-ecr-role-to-assume: arn:aws:iam::${{ secrets.AWS_STAGING_ACCOUNT_ID }}:role/${{ secrets.AWS_APPS_SALEOR_MCP_STAGING_CICD_ROLE_NAME }} |
| 59 | + aws-ecr-registries: ${{ secrets.AWS_ECR_ACCOUNT }} |
| 60 | + |
| 61 | + deploy: |
| 62 | + needs: [prepare-variables, build-push] |
| 63 | + runs-on: ubuntu-24.04 |
| 64 | + permissions: {} |
| 65 | + |
| 66 | + steps: |
| 67 | + - name: Get Token |
| 68 | + id: get-token |
| 69 | + uses: saleor/saleor-internal-actions/request-vault-token@6a0fa7c073b3857a11d414f25a149065fe5a0fcf # v1.4.0 |
58 | 70 | with: |
59 | | - context: . |
60 | | - platforms: linux/amd64,linux/arm64 |
61 | | - push: true |
62 | | - tags: ${{ env.IMAGE_TAGS }} |
63 | | - cache-from: type=gha,scope=buildkit-master |
64 | | - cache-to: type=gha,scope=buildkit-master |
| 71 | + vault-url: ${{ secrets.VAULT_URL }} |
| 72 | + vault-jwt: ${{ secrets.VAULT_JWT }} |
65 | 73 |
|
66 | 74 | - name: Trigger staging deployment |
| 75 | + env: |
| 76 | + GITHUB_TOKEN: ${{ steps.get-token.outputs.token }} |
| 77 | + IMAGE_TAG: ${{ needs.prepare-variables.outputs.image_tag }} |
67 | 78 | run: | |
68 | | - export GITHUB_TOKEN=$( \ |
69 | | - curl --request GET --url ${{ secrets.VAULT_URL}} --header "Authorization: JWT ${{ secrets.VAULT_JWT }}" | jq -r .token \ |
70 | | - ) |
71 | | -
|
72 | | - echo "::add-mask::$GITHUB_TOKEN" |
73 | | -
|
74 | 79 | payload=$( |
75 | | - jq --arg image_tag "$UNIQUE_IMAGE_TAG" -n '{ |
| 80 | + jq -n --arg image_tag "$IMAGE_TAG" '{ |
76 | 81 | "event_type": "saleor-mcp-staging", |
77 | 82 | "client_payload": { |
78 | 83 | "image_tag": $image_tag |
79 | 84 | } |
80 | 85 | }' |
81 | 86 | ) |
82 | | -
|
83 | | - gh api /repos/saleor/saleor-cloud-deployments/dispatches --input <(echo "$payload") |
| 87 | + gh api /repos/saleor/saleor-cloud-deployments/dispatches --input - <<< "$payload" |
0 commit comments