> ## Documentation Index
> Fetch the complete documentation index at: https://docs.phidata.com/llms.txt
> Use this file to discover all available pages before exploring further.

# CI/CD

Phidata templates come pre-configured with [Github Actions](https://docs.github.com/en/actions) for CI/CD. We can

1. [Test and Validate on every PR](#test-and-validate-on-every-pr)
2. [Build Docker Images with Github Releases](#build-docker-images-with-github-releases)
3. [Build ECR Images with Github Releases](#build-ecr-images-with-github-releases)

## Test and Validate on every PR

Whenever a PR is opened against the `main` branch, a validate script runs that ensures

1. The changes are formatted using ruff
2. All unit-tests pass
3. The changes don't have any typing or linting errors.

Checkout the `.github/workflows/validate.yml` file for more information.

<img src="https://mintcdn.com/phidata/Sh-Zufd34Q4ws-t-/images/validate-cicd.png?fit=max&auto=format&n=Sh-Zufd34Q4ws-t-&q=85&s=3e8b29f8bbaf915c5f39ad9f1e2027a3" alt="validate-cicd" width="940" height="353" data-path="images/validate-cicd.png" />

## Build Docker Images with Github Releases

If you're using [Dockerhub](https://hub.docker.com/) for images, you can buld and push the images throug a Github Release. This action is defined in the `.github/workflows/docker-images.yml` file.

1. Create a [Docker Access Token](https://hub.docker.com/settings/security) for Github Actions

<img src="https://mintcdn.com/phidata/Sh-Zufd34Q4ws-t-/images/docker-access-token.png?fit=max&auto=format&n=Sh-Zufd34Q4ws-t-&q=85&s=466dd4a0d841783a64fd756065ae0e06" alt="docker-access-token" width="742" height="568" data-path="images/docker-access-token.png" />

2. Create secret variables `DOCKERHUB_REPO`, `DOCKERHUB_TOKEN` and `DOCKERHUB_USERNAME` in your github repo. These variables are used by the action in `.github/workflows/docker-images.yml`

<img src="https://mintcdn.com/phidata/Sh-Zufd34Q4ws-t-/images/github-actions-docker-secrets.png?fit=max&auto=format&n=Sh-Zufd34Q4ws-t-&q=85&s=a938982861305fc4ded9ae7a5dd086d9" alt="github-actions-docker-secrets" width="1143" height="822" data-path="images/github-actions-docker-secrets.png" />

3. Run workflow using a Github Release

This workflow is configured to run when a release is created. Create a new release using:

<Note>
  Confirm the image name in the `.github/workflows/docker-images.yml` file before running
</Note>

<CodeGroup>
  ```bash Mac theme={null}
  gh release create v0.1.0 --title "v0.1.0" -n ""
  ```

  ```bash Windows theme={null}
  gh release create v0.1.0 --title "v0.1.0" -n ""
  ```
</CodeGroup>

<img src="https://mintcdn.com/phidata/Sh-Zufd34Q4ws-t-/images/github-actions-build-docker.png?fit=max&auto=format&n=Sh-Zufd34Q4ws-t-&q=85&s=4514f122f9f7d1c0e9bccfd3dcf8c332" alt="github-actions-build-docker" width="1042" height="732" data-path="images/github-actions-build-docker.png" />

<Note>
  You can also run the workflow using `gh workflow run`
</Note>

## Build ECR Images with Github Releases

If you're using ECR for images, you can buld and push the images through a Github Release. This action is defined in the `.github/workflows/ecr-images.yml` file and uses the new OpenID Connect (OIDC) approach to request the access token, without using IAM access keys.

We will follow this [guide](https://aws.amazon.com/blogs/security/use-iam-roles-to-connect-github-actions-to-actions-in-aws/) to create an IAM role which will be used by the github action.

1. Open the IAM console.
2. In the left navigation menu, choose Identity providers.
3. In the Identity providers pane, choose Add provider.
4. For Provider type, choose OpenID Connect.
5. For Provider URL, enter the URL of the GitHub OIDC IdP: [https://token.actions.githubusercontent.com](https://token.actions.githubusercontent.com)
6. Get thumbprint to verify the server certificate
7. For Audience, enter sts.amazonaws.com.

Verify the information matches the screenshot below and Add provider

<img src="https://mintcdn.com/phidata/Sh-Zufd34Q4ws-t-/images/github-oidc-provider.png?fit=max&auto=format&n=Sh-Zufd34Q4ws-t-&q=85&s=53eff0fec42e9d8be4873d607e2c8f44" alt="github-oidc-provider" width="1125" height="799" data-path="images/github-oidc-provider.png" />

8. Assign a Role to the provider.

<img src="https://mintcdn.com/phidata/Sh-Zufd34Q4ws-t-/images/github-oidc-provider-assign-role.png?fit=max&auto=format&n=Sh-Zufd34Q4ws-t-&q=85&s=62de0c5da6b64c0621ce511d741e91a0" alt="github-oidc-provider-assign-role" width="1347" height="587" data-path="images/github-oidc-provider-assign-role.png" />

9. Create a new role.

<img src="https://mintcdn.com/phidata/Sh-Zufd34Q4ws-t-/images/github-oidc-provider-create-new-role.png?fit=max&auto=format&n=Sh-Zufd34Q4ws-t-&q=85&s=a1c3247a8564d4cea52034a58e43ab42" alt="github-oidc-provider-create-new-role" width="604" height="278" data-path="images/github-oidc-provider-create-new-role.png" />

10. Confirm that Web identity is already selected as the trusted entity and the Identity provider field is populated with the IdP. In the Audience list, select sts.amazonaws.com, and then select Next.

<img src="https://mintcdn.com/phidata/Sh-Zufd34Q4ws-t-/images/github-oidc-provider-trusted-entity.png?fit=max&auto=format&n=Sh-Zufd34Q4ws-t-&q=85&s=558c07f4118a91288d8101a605319efb" alt="github-oidc-provider-trusted-entity" width="1300" height="934" data-path="images/github-oidc-provider-trusted-entity.png" />

11. Add the `AmazonEC2ContainerRegistryPowerUser` permission to this role.

12. Create the role with the name `GithubActionsRole`.

13. Find the role `GithubActionsRole` and copy the ARN.

<img src="https://mintcdn.com/phidata/Sh-Zufd34Q4ws-t-/images/github-oidc-role.png?fit=max&auto=format&n=Sh-Zufd34Q4ws-t-&q=85&s=3fd890170e48b2cc584aba1a5a67975e" alt="github-oidc-role" width="1389" height="710" data-path="images/github-oidc-role.png" />

14. Create the ECR Repositories: `llm` and `jupyter-llm` which are built by the workflow.

<img src="https://mintcdn.com/phidata/Sh-Zufd34Q4ws-t-/images/create-ecr-image.png?fit=max&auto=format&n=Sh-Zufd34Q4ws-t-&q=85&s=2419f506cc34e389db00d23a59476b2a" alt="create-ecr-image" width="1389" height="408" data-path="images/create-ecr-image.png" />

15. Update the workflow with the `GithubActionsRole` ARN and ECR Repository.

```yaml .github/workflows/ecr-images.yml theme={null}
name: Build ECR Images

on:
  release:
    types: [published]

permissions:
  # For AWS OIDC Token access as per https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services#updating-your-github-actions-workflow
  id-token: write # This is required for requesting the JWT
  contents: read # This is required for actions/checkout

env:
  ECR_REPO: [YOUR_ECR_REPO]
  # Create role using https://aws.amazon.com/blogs/security/use-iam-roles-to-connect-github-actions-to-actions-in-aws/
  AWS_ROLE: [GITHUB_ACTIONS_ROLE_ARN]
  AWS_REGION: us-east-1
```

16. Update the `docker-images` workflow to **NOT** run on a release

```yaml .github/workflows/docker-images.yml theme={null}
name: Build Docker Images

on: workflow_dispatch
```

17. Run workflow using a Github Release

<CodeGroup>
  ```bash Mac theme={null}
  gh release create v0.2.0 --title "v0.2.0" -n ""
  ```

  ```bash Windows theme={null}
  gh release create v0.2.0 --title "v0.2.0" -n ""
  ```
</CodeGroup>

<img src="https://mintcdn.com/phidata/Sh-Zufd34Q4ws-t-/images/github-actions-build-ecr.png?fit=max&auto=format&n=Sh-Zufd34Q4ws-t-&q=85&s=fc6823aec8f01105245ca9f5d80d54c4" alt="github-actions-build-ecr" width="1389" height="710" data-path="images/github-actions-build-ecr.png" />

<Note>
  You can also run the workflow using `gh workflow run`
</Note>
