|
| 1 | + |
| 2 | + Sample illustration of deployment on to *OCI Container instance* using OCI DevOps. |
| 3 | +------------ |
| 4 | + |
| 5 | +Objective |
| 6 | +---- |
| 7 | + |
| 8 | +- Using OCI Build pipeline build a container image for a sample python application. |
| 9 | +- Use the shell stage within the deployment pipeline to deploy to the container instance. |
| 10 | + |
| 11 | +Specific instructions to download only this sample. |
| 12 | +--- |
| 13 | + |
| 14 | +``` |
| 15 | + $ git init oci-devops-deploy-containerinstance |
| 16 | + $ cd oci-devops-deploy-containerinstance |
| 17 | + $ git remote add origin https://github.com/oracle-devrel/oci-devops-examples |
| 18 | + $ git config core. sparsecheckout true |
| 19 | + $ echo "oci-deployment-examples/oci-devops-deploy-containerinstance/*">>.git/info/sparse-checkout |
| 20 | + $ git pull --depth=1 origin main |
| 21 | +
|
| 22 | +``` |
| 23 | + |
| 24 | +Procedure |
| 25 | +--- |
| 26 | + |
| 27 | +### OCI Identity setup. |
| 28 | + |
| 29 | +- Create OCI Dynamic group and add the below rules. |
| 30 | + |
| 31 | +```java |
| 32 | +ALL {resource.type = 'devopsbuildpipeline', resource.compartment.id = 'OCID OF YOUR COMPARTMENT'} |
| 33 | +ALL {resource.type = 'devopsdeploypipeline', resource.compartment.id = 'OCID OF YOUR COMPARTMENT'} |
| 34 | +``` |
| 35 | + |
| 36 | +- Create OCI policy and add the below statements. |
| 37 | + |
| 38 | +```java |
| 39 | +Allow dynamic-group <NAME OF THE DYNAMIC GROUP> to read secret-family in compartment <NAME OF THE COMPARTMENT> |
| 40 | +Allow dynamic-group <NAME OF THE DYNAMIC GROUP> to manage virtual-network-family in compartment <NAME OF THE COMPARTMENT> |
| 41 | +Allow dynamic-group <NAME OF THE DYNAMIC GROUP> to manage ons-topics in compartment <NAME OF THE COMPARTMENT> |
| 42 | +Allow dynamic-group <NAME OF THE DYNAMIC GROUP> to manage objects in compartment <NAME OF THE COMPARTMENT> |
| 43 | +Allow dynamic-group <NAME OF THE DYNAMIC GROUP> to manage all-artifacts in compartment <NAME OF THE COMPARTMENT> |
| 44 | +Allow dynamic-group <NAME OF THE DYNAMIC GROUP> to manage repos in compartment <NAME OF THE COMPARTMENT> |
| 45 | +Allow dynamic-group <NAME OF THE DYNAMIC GROUP> to manage compute-container-family in compartment <NAME OF THE COMPARTMENT> |
| 46 | +``` |
| 47 | + |
| 48 | +### OCI Artifact repo setup |
| 49 | + |
| 50 | +- Create an OCI Artifact registry repo - https://docs.oracle.com/en-us/iaas/Content/artifacts/create-repo.htm#create-repo |
| 51 | +- Uncheck the immutable option, this is to have a mutable command spec configuration. |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +### OCI Container registry setup |
| 56 | + |
| 57 | +- Create an OCI container registry repo - https://docs.oracle.com/en-us/iaas/Content/Registry/Tasks/registrycreatingarepository.htm#Creating_a_Repository |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +### OCI VCN Setup |
| 62 | + |
| 63 | +- Create a VCN - https://docs.oracle.com/en-us/iaas/Content/Network/Tasks/managingVCNs.htm |
| 64 | +- We have created this sample using a `public subnet`, feel free to use a private or public subnet as far as the IP address can connect via HTTP from your workstation. |
| 65 | +- With in the subnet ,add security rules to allow traffic http (port 80) and https (port 443). |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +### OCI Vault setup |
| 70 | + |
| 71 | +- Create an OCI Vault and master key - https://docs.oracle.com/en-us/iaas/Content/KeyManagement/home.htm |
| 72 | +- We will be using an object storage backend to ensure a consistent state across the container instance, to ensure the same IP address for a better user experience. To provide access create a `customer secret key` - https://docs.oracle.com/en-us/iaas/Content/Identity/Tasks/managingcredentials.htm#Working2 |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +- Add the key id and key as secrets with OCI Vault. |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +- Make a note of these secrets OCID |
| 81 | + |
| 82 | +### OCI Object storage bucket. |
| 83 | + |
| 84 | +- Create an OCI Object storage bucket, the same will be used to store terraform config and state files. |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +### OCI DevOps setup |
| 91 | + |
| 92 | +- Create an `OCI Notification topic` - https://docs.oracle.com/en-us/iaas/Content/Notification/Tasks/create-topic.htm#top |
| 93 | +- Create a DevOps project and associate it with the notification. |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +- Enable `logs` for the project. |
| 98 | + |
| 99 | +- Create a code repo. - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_repo.htm#create_repo |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +- Push the content to the code repo over HTTPS or ssh protocol. |
| 104 | + |
| 105 | +- Create a DevOps artifact of type `Docker image`, this is for static image reference. Use type as `Container Image repository` and path as `PATH To container repo:${STATIC_IMAGE_TAG}`.Enable `Allow parameterization`. |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +- Create a DevOps artifact of type `Docker image`, this for dynamic image reference. Use type as `Container Image repository` and path as `PATH To container repo:${BUILDRUN_HAS}`.Enable `Allow parameterization`. |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +- Create a DevOps artifact of type `General artifact` for the build pipeline to push shell stage instructions to the artifact registry repo. Select the type as `General artifact` and the artifact source as `Artifact registry repository` |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +- Use `Select` and associate with the artifact registry repo. |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +- Use artifact location as `Set custom location` and add the path as `commad_spec.yaml` and version as `0.0`.Enable the `Allow parameterization` option. |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +- Create a DevOps artifact of type `Command specification` for the shell stage to use the artifact. We will be using the definitions created in the previous step but with a different artifact type. Select the type as `Command specification` and the artifact source as `Artifact registry repository` |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +- Use `Select` and associate with the artifact registry repo. |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +- Use artifact location as `Set custom location` and add the path as `commad_spec.yaml` and version as `0.0`.Enable the `Allow parameterization` option. |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +- Create a `Build pipeline` - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_buildpipeline.htm#create_buildpipeline |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +- Add below keys and values for the build pipeline, as parameters. |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +- BUCKET_NAME = Object storage bucket name |
| 149 | +- NAMESPACE_NAME = Object storage namespace |
| 150 | +- SUBNET_OCID = OCID of the subnet to use |
| 151 | +- IMAGE_STATIC_TAG = a string (Example new), it's a tag we will be using to update the container image. |
| 152 | +- CONTAINER_REGISTRY_URL = Absolute path of the container registry repo. |
| 153 | +- CONTAINERINSTANCE_DISPLAY_NAME = Display name for the container instance |
| 154 | +- AD = Availability domain for the container instance. |
| 155 | +- COMPARTMENT_OCID = OCI Compartment OCID |
| 156 | +- REGION = OCI Region |
| 157 | + |
| 158 | +- Within the build pipeline add a stage of type `Managed Build`.Associate with the code repo as the `Primary code repository`. |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +- Add another stage of type `Deliver Artifacts` within the build pipeline. Associate the stage with both the docker image and general artifacts created. Associate both the docker image artifact with the result artifact name as `python_app_dockerimage` and the general artifact with the outputArtifact name `command_spec`. This configuration is based on the [build_spec.yaml](build_spec.yaml) file. |
| 163 | + |
| 164 | +```java |
| 165 | +outputArtifacts: |
| 166 | + - name: python_app_dockerimage |
| 167 | + type: DOCKER_IMAGE |
| 168 | + location: python_fast_app:latest |
| 169 | + |
| 170 | + - name: command_spec |
| 171 | + type: BINARY |
| 172 | + location: ${OCI_PRIMARY_SOURCE_DIR}/command_spec.yaml |
| 173 | +``` |
| 174 | + |
| 175 | + |
| 176 | +- Create a deployment pipeline - https://docs.oracle.com/en-us/iaas/Content/devops/using/deployment_pipelines.htm |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | +- Within the deployment pipeline add a stage of type `shell.` Associate with the command specification artifacts. |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | +- Select the appropriate container instance configuration. |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | +- Select the appropriate virtual cloud network and subnet. |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | +- Switch back to `build a pipeline` and add a stage of type `Trigger deployment`. Associate with the `deployment pipeline`.Ensure to enable the option `Send build pipelines Parameters`. |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | +- the complete build pipeline stage will be as below. |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | +- Create an OCI Trigger and associate the code repo and the build pipeline created - https://docs.oracle.com/en-us/iaas/Content/devops/using/trigger_build.htm |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | +### Test the solution |
| 205 | + |
| 206 | +- Update file [readme.md](README.md) and push back to code repo. Once the repo gets updated, it will trigger a build pipeline. |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | +- Wait for all the build stages to complete. |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | +- Follow deployments |
| 215 | + |
| 216 | + |
| 217 | +- It will take a while for the deployment pipeline to complete. Once done fetch the `IP address` to access the application from the deployment logs. |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | +- We are restarting the container instance on every deployment to |
| 222 | + - Ensure a constant URL for the end user. |
| 223 | + - As there is no native image update for the container, on every restart it will pick the latest image pushed to the container repo. |
| 224 | +- Use the application IP and access the container. |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | +### Tail end |
| 229 | + |
| 230 | +- Refer deployment logs via pipeline or container instances will help to troubleshoot any errors. |
| 231 | +- The shell stage intern uses container instances with two containers and it will be auto-deleted once the deployment completes. |
| 232 | + |
| 233 | + |
| 234 | + |
| 235 | +- The terraform state is managed by the object storage, once it's removed or any alteration, the terraform actions will also be changed accordingly. |
| 236 | + |
| 237 | +### Cleanup of the resources. |
| 238 | + |
| 239 | +- Delete OCI devops project using `cascade deletion` option. |
| 240 | +- Remove the content and bucket of OCI Object storage. |
| 241 | +- Delete the OCI Container instance. |
| 242 | +- Remove the artifacts and delete artifact and container registry repos. |
| 243 | +- Clean up the devops logs /policies /dynamic group as accordingly. |
| 244 | + |
| 245 | + |
| 246 | +Read more |
| 247 | +---- |
| 248 | + |
| 249 | +- OCI DevOps - https://docs.oracle.com/en-us/iaas/Content/devops/using/home.htm. |
| 250 | +- OCI Reference architectures - https://docs.oracle.com/solutions/ |
| 251 | +- OCI DevOps samples - https://github.com/oracle-devrel/oci-devops-examples |
| 252 | +- OCI DevOps shell stages - https://docs.oracle.com/en-us/iaas/Content/devops/using/shell_stage.htm |
| 253 | + Contributors |
| 254 | + =========== |
| 255 | + |
| 256 | +- Author: Rahul M R. |
| 257 | +- Collaborators : NA |
| 258 | +- Last release: March 2023 |
| 259 | + |
| 260 | +### Back to examples. |
| 261 | +---- |
| 262 | +- 🍿 [Back to OCI Devops Deployment sample](./../README.md) |
| 263 | +- 🏝️ [Back to OCI Devops sample](./../../README.md)** |
| 264 | + |
0 commit comments